创建 pipeline
- 创建一个 .gitlab-ci.yml 文件,用来定义 pipeline 的格式
.gitlab-ci.yml 文件内容如下(使用默认的 docker image,执行build website 步骤)
build website:
script:
- npm install
- npm install -g gatsby-cli
- gtsby build
使用 image 指定要使用image;使用 artifacts 保留创建的中间文件
build website:
image: node
script:
- npm install
- npm install -g gatsby-cli
- gtsby build
artifacts:
paths:
- ./public
添加 test 步骤; 使用 stages 指定要指定的stage先后顺序
stages:
- build
- test
build website:
stage: build
image: node
script:
- npm install
- npm install -g gatsby-cli
- gtsby build
artifacts:
paths:
- ./public
test artifacts:
image: alpine
stage: test
script:
- grep "Gasby" ./public/index.html
添加并行任务
stages:
- build
- test
build website:
stage: build
image: node
script:
- npm install
- npm install -g gatsby-cli
- gtsby build
artifacts:
paths:
- ./public
test artifacts:
image: alpine
stage: test
script:
- grep "Gasby" ./public/index.html
test website:
image: node
stage: test
script:
- npm install
- npm install -g gatsby-cli
- gatsby serve &
- sleep 3
- curl "http://localhost:9000" | tac | tac | grep -q "Gatsby" ./public/index.html
添加部署步骤
deploy the surge
stage: deploy
script:
- npm install --global surge
- surge --project ./public --domain www.test.com
Environments
添加 environment
deploy staging:
stage: deploy staging
environment:
name: staging
url:http://www.staging.com
script:
- npm install --global surge
- surge -project ./public --domain www.staging.com
deploy production:
stage: deploy production
environment:
name: production
url:http://www.production.com
script:
- npm install --global surge
- surge -project ./public --domain www.production.com
在 project / Operations / Environment 链接下面,可以找到不同环境的部署历史

创建,删除一个动态的环境
在"deploy review" 绑定创建和删除任务
stages:
- build
- test
- deploy review
- deploy staging
- deploy production
- production tests
deploy review:
stage: deploy review
only:
- merge_requests # 只在merge pipeline才执行
environment:
name: review_$CI_COMMIT_REF_NAME
url:http://www.review_$CI_ENVIRONMENT_SLUG.com
on_stop: stop review # 要执行的停止任务
script:
- npm install --global surge
- surge--project ./public --domain www.$CI_ENVIORNMENT_SLOG.com
stop review:
stage: deploy review # 和上面同样的一个任务
only:
- merge_request
variables:
GIT_STRATEGY: none # 不clone当前rep
script:
- echo "Remove review app"
- npm install --global surge
- surge teardown www.$CI_ENVIORNMENT_SLOG.com
when: manual
environment:
name: review_$CI_COMMIT_REF_NAME
action: stop
Variables 变量
两个方法:
- project / Settings / CI/CD - Variables 可以设置一些预定义变量(一般存敏感信息)
- 直接在项目中定义变量
比如:
variables:
- STAGING_DOMAIN: www.staging.com
# 使用
deploy staging:
stage: deploy staging
environment:
name: staging
url:http://$STAGING_DOMAIN
script:
- npm install --global surge
- surge -project ./public --domain STAGING_DOMAIN
设置手动部署
使用 when 条件; allow_failure 是否运行当前失败,其后的任务继续执行
deploy production:
stage: deploy production
environment:
name: production
url:http://www.production.com
when: manual
allow_failure: false # 手动任务默认是不阻塞的
script:
- npm install --global surge
- surge -project ./public --domain www.production.com
设置任务只在特定分支上执行
使用 only 条件
deploy production:
stage: deploy production
environment:
name: production
url:http://www.production.com
only:
- master
when: manual
allow_failure: false # 手动任务默认是不阻塞的
script:
- npm install --global surge
- surge -project ./public --domain www.production.com
before_script, after_script
所有任务,或者某个任务在所有任务执行前后要做的动作
before_script:
- echo "this step will be done before runing the script section
after_script:
- echo "this step will be done after runing the script section"
参考文档
网友评论