before_script:
- echo "before_script"
after_script:
- echo "after_script"
stages:
- test
# test
test:
stage: test
script:
- echo test
before_script:
# 会复写全局设置
- echo "before_script"
after_script:
- echo "after_script"
stages:
- test
# test
test:
stage: test
# 会复写上面的before_script
before_script:
- echo before_script test
script:
- echo test
stages:
- build
- test
- deploy
stages:
- build
- test
- deploy
job 1:
stage: build
script: make build dependencies
job 2:
stage: build
script: make build artifacts
job 3:
stage: test
script: make test
job 4:
stage: deploy
script: make deploy
#单元测试
unit-test:
# 属于哪个阶段
stage: verify
# 获取对应标签名称的Runners来运行jobs
tags:
# 标签名称
- test-cicd
# 执行脚本
script:
- echo unit-test
:,{,},[,],&,*,#,?,|,-,<,>,=,!
原因:举个例子,当命令中包含冒号(:)时,script需要被包在双引号中,这样YAML解析器才可以正确解析为一个字符串而不是一个键值对(key:value)
job:
# 通过空格 执行多个命令
script: mvn clean test
或者
job:
script:
- mvn
- clean test
表job作业需要执行的命令是"mvn clean test"。
only和except如果都存在在一个job声明中,则同时 only、except 进行过滤(注意,不是忽略 except 条件)
only和except允许使用正则
only和except 允许指定用于过滤forks作业的存储库路径
only 和 except 支持高级策略,以下四个关键字可以使用
job:
# 使用正则
only:
- /^feature-.*$/
# use special keyword
except:
- branches
job:
# 使用特殊的关键字
only:
- tags # tag 分支 commit 之后触发
- triggers # API 触发
- schedules # 每日构建触发
job将会在父仓库gitlab-org/gitlab-ce的非master分支有提交时运行。
job:
only:
- branches@gitlab-org/gitlab-ce
except:
- master@gitlab-org/gitlab-ce
# 定义流水线的各个阶段
stages:
- verify
- build
- dockerpush
- deploy
- cleanup
before_script:
- echo "before_script"
after_script:
- echo "after_script"
#单元测试
unit-test:
stage: verify
tags:
- test-cicd
script:
- echo "unit-test"
#java编译
java-package:
stage: build
tags:
- test-cicd
script:
- echo "java-package"
#push镜像
docker-push:
stage: dockerpush
tags:
- test-cicd
script:
- echo "docker-push"
# 当前面stages中任意一个jobs失败后执行
docker-push2:
stage: dockerpush
tags:
- test-cicd
script:
- echo "docker-push2"
when: on_failure
# 手动执行(GitLab8.10增加)
service-1:
stage: deploy
tags:
- test-cicd
script:
- echo "service-1"
when: manual
# 前面的job成功与否,都会执行该job
cleanup_job:
stage: cleanup
script:
- echo clean up
when: always
下面的这个例子中,java-package和java-package2将会并列进行,如果java-package2失败了,它也不会影响进行中的下一个stage,因为这里有设置了allow_failure: true。
stages:
- verify
- build
- dockerpush
- deploy
- cleanup
before_script:
- pwd
after_script:
- echo after_script
#单元测试
unit-test:
stage: verify
tags:
- test-cicd
script:
- echo unit-test
#java编译
java-package:
stage: build
tags:
- test-cicd
script:
- echo build
#java编译
java-package2:
stage: build
tags:
- test-cicd
script:
# 该shell会导致job执行失败
- execute_script_that_will_fail
# 不影响后面的任务进行
allow_failure: true
#push镜像
docker-push:
stage: dockerpush
tags:
- test-cicd
script:
- echo docker-push
#deploy
service-1:
stage: deploy
tags:
- test-cicd
script:
- echo deploy
when: manual
cleanup_job:
stage: cleanup
tags:
- test-cicd
script:
- echo clean up
when: always
# 定义变量
variables:
# K,V形式定义变量
SOFT_VERSION: '1.0'
TAG_NAME: 'app'
#构建镜像
docker-build:
stage: dockerpush
tags:
- test-cicd
script:
# 使用变量
- docker build -t $TAG_NAME:${SOFT_VERSION}
这两个关键字允许使用一个自定义的 Docker 镜像和一系列的服务,并且可以用于整个 job 周期。
在最简单的格式中,环境关键字可以定义为:
deploy to production:
stage: deploy
script: git push production HEAD:master
environment:
name: production
在上面这个例子中,deploy_to_production作业将会执行部署操作部署到production环境。
如果job都成功完成了,在 部署/环境 页面中将会创建一个请求的按钮,它将指向https://www.baidu.com/
deploy to production:
stage: deploy
script: echo '部署到生产'
environment:
name: production
url: https://www.baidu.com/
review_app:
stage: deploy
script: echo "deploy-app"
environment:
name: review
on_stop: stop_review_app
stop_review_app:
stage: deploy
script: echo "delete-app"
when: manual
environment:
name: review
action: stop
在上面的示例中,设置 review_app 作业用于部署代码到 review 评审环境中,同时在 on_stop 中指定了 stop_review_app 作业。
一旦 review_app 作业成功执行,它将根据 when 下定义的内容触发 stop_review_app 作业。
运行完成后,在 stop_review_app 作业界面需要手动点击 停止当前环境 才能启动 stop_review_app 作业的执行。
stop_review_app 作业执行完成后,会停止 review 评审环境,在 环境 –> 已停止 列表中可以看到 review 评审环境。
review_app:
script: deploy-review-app
environment:
name: review/$CI_COMMIT_REF_NAME
auto_stop_in: 1 day
当review_app工作被执行,环境的生命期被设置为1天。
用于配置部署到Kubernetes集群。
deploy:
stage: deploy
script: make deploy-app
environment:
name: production
kubernetes:
namespace: production
这将设置部署作业,以便使用生产的Kubernetes命名空间部署到生产环境。
deploy as review app:
stage: deploy
script: make deploy
environment:
name: review/$CI_COMMIT_REF_NAME
url: https://$CI_ENVIRONMENT_SLUG.example.com/
缓存二进制文件中以.apk结尾的所有文件和.config文件。
rspec:
script: test
cache:
paths:
- binaries/*.apk
- .config
要启用每个分支缓存:
cache:
key: "$CI_COMMIT_REF_SLUG"
paths:
- binaries/
将目录 docker/ 和文件 pom.xml 打包成工件
stages:
- test
# test
test:
stage: test
script:
- echo test
artifacts:
paths:
- docker/
- pom.xml
要禁用工件传递,请使用空依赖关系定义作业:
job:
stage: build
script: make build
dependencies: []
可以仅为打标记的release发布版本创建工件,这样可以避免临时构建产生大量的存储需求:
default-job:
script:
- mvn test -U
except:
- tags
release-job:
script:
- mvn package -U
artifacts:
paths:
- target/*.war
only:
- tags
上面的示例中,default-job 作业不会在打标记的release发布版本中执行,而 release-job 只会在打标记的release发布版本执行,并且将 target/*.war 打包成工件以供下载。
定义展示包的名称
expose_as 可用于在合并请求用户界面中公开作业工件。
注意以下事项:
test:
script: [ 'echo 1' ]
artifacts:
expose_as: 'artifact 1'
paths: ['path/to/file.txt']
使用此配置,GitLab 会将链接artifact 1 添加到指向 file1.txt 的相关合并请求中。
匹配整个目录:
test:
script: [ 'echo 1' ]
artifacts:
expose_as: 'artifact 1'
paths: ['path/to/directory/']
使用工件名称:
stages:
- test
# test
test:
stage: test
script:
- echo test
artifacts:
name: "$CI_JOB_NAME"
paths:
- docker/
- pom.xml
使用当前分支或tag版本标签名作为工件名称:
stages:
- test
# test
test:
stage: test
script:
- echo test
artifacts:
name: "$CI_COMMIT_REF_NAME"
paths:
- docker/
- pom.xml
同时使用当前作业名称以及当前分支或tag版本标签名作为工件名称
stages:
- test
# test
test:
stage: test
script:
- echo test
artifacts:
name: "$CI_JOB_NAME-$CI_COMMIT_REF_NAME"
paths:
- docker/
- pom.xml
同时使用当前作业阶段名称以及当前分支名称作为工件名称
stages:
- test
# test
test:
stage: test
script:
- echo test
artifacts:
name: "$CI_JOB_STAGE-$CI_COMMIT_REF_NAME"
paths:
- docker/
- pom.xml
如果你使用的 Windows系统的Batch批处理脚本 ,则需要把 $ 替换成 %:
stages:
- test
# test
test:
stage: test
script:
- echo test
artifacts:
name: "%CI_JOB_STAGE%-%CI_COMMIT_REF_NAME%"
paths:
- docker/
- pom.xml
如果你使用的 Windows系统的PowerShell脚本 ,则需要把 $ 替换成 $env::
stages:
- test
# test
test:
stage: test
script:
- echo test
artifacts:
name: "$env:CI_JOB_STAGE-$env:CI_COMMIT_REF_NAME"
paths:
- docker/
- pom.xml
将所有的未跟踪文件打包成工件
artifacts:
untracked: true
将所有的未跟踪文件以及目录 binaries 中文件打包成工件:
artifacts:
untracked: true
paths:
- binaries/
当作业失败时,上传工件
job:
artifacts:
when: on_failure
用于设置工件的过期时间。过期后,不能再访问该工件
可以点击界面上的 保存 保持按钮,永久保存工件。
默认情况下每小时检查一次工件是否过期(通过cron作业)
工件默认有效期是30天
默认时间单位:秒
'42'
'3 mins 4 sec'
'2 hrs 20 min'
'2h20min'
'6 mos 1 day'
'47 yrs 6 mos and 4d'
'3 weeks and 2 days'
设置工件有效期为一周
job:
artifacts:
expire_in: 1 week
下面是从Ruby的RSpec测试工具中收集JUnit XML文件的示例:
rspec:
stage: test
script:
- bundle install
- rspec --format RspecJunitFormatter --out rspec.xml
artifacts:
reports:
junit: rspec.xml
可以在一个作业中指定多个单元测试报告,GitLab会自动将他们转换成一个文件,可以像下面这样表示报告的路径:
job1:
coverage: '/Code coverage:\d+\.\d+%/'
如果在作业日志中输出了"Code coverage:80.2%",我们使用上面的正则表达式就可以获取到代码的覆盖率。然后在作业的右上角处就会显示 Coverage:80.2% 。
test:
script: rspec
retry: 2
下面这个例子只有当运行器系统出现故障时才能最多重试两次
test:
script: rspec
retry:
max: 2
when: runner_system_failure
如果上面例子中出现的是其他故障,那么作业不会重试。
为了针对多种重试情形,我们可以使用矩阵形式罗列出错误情形,如下示例:
test:
script: rspec
retry:
max: 2
when:
- runner_system_failure
- stuck_or_timeout_failure
timeout允许你为一个特定的工作配置一个超时。
build:
script: build.sh
timeout: 3 hours 30 minutes
test:
script: rspec
timeout: 3h 30m
作业级超时可以超过项目级超时,但不能超过Runner特定的超时。
标记一个作业为并行运行
test:
script: rspec
parallel: 5
这个是一个多项目流水线
rspec:
stage: test
script: bundle exec rspec
staging:
stage: deploy
trigger: my/deployment
流水线执行完test任务后就会去执行my/deployment项目的流水线
配置下游流水线式也可以执行分支
rspec:
stage: test
script: bundle exec rspec
staging:
stage: deploy
trigger:
project: my/deployment
branch: stablez
include:
- local: '/templates/.gitlab-ci-template.yml'
include:
- project: 'my-group/my-project'
file: '/templates/.gitlab-ci-template.yml'
你也可以指定ref,默认是项目的HEAD。
include:
- project: 'my-group/my-project'
ref: master
file: '/templates/.gitlab-ci-template.yml'
- project: 'my-group/my-project'
ref: v1.0.0
file: '/templates/.gitlab-ci-template.yml'
- project: 'my-group/my-project'
ref: 787123b47f14b552955ca2786bc9542ae66fee5b # Git SHA
file: '/templates/.gitlab-ci-template.yml'
可用于包含 GitLab 附带的 .gitlab-ci.yml 模板。
# File sourced from GitLab's template collection
include:
- template: Auto-DevOps.gitlab-ci.yml
多个include:template文件。
include:
- template: Android-Fastlane.gitlab-ci.yml
- template: Auto-DevOps.gitlab-ci.yml
include:
- remote: 'https://gitlab.com/awesome-project/raw/master/.gitlab-ci-template.yml'
.tests:
script: rake test
stage: test
only:
refs:
- branches
rspec:
extends: .tests
script: rake rspec
only:
variables:
- $RSPEC
在上面的例子中,rspec作业继承了.test模板作业。
GitLab将根据键值进行反向深度合并。GitLab将
rspec:
script: rake rspec
stage: test
only:
refs:
- branches
variables:
- $RSPEC
脚本:rake test 已被脚本:rake rspec 覆盖。
如果您确实想包含 rake 测试,请参阅 before_script 和 after_script。
例如,如果您有一个本地 included.yml 文件:
.template:
script:
- echo Hello!
然后,在.gitlab-ci.yml中你可以这样使用它。
include: included.yml
useTemplate:
image: alpine
extends: .template
这将运行一个名为useTemplate的作业,运行.template作业中定义的echo Hello!,并使用本地作业中定义的alpine Docker镜像。
下面的例子只是把所有的文件从项目的根部移到public/目录下。
pages:
stage: deploy
script:
- mkdir .temp
- cp -r * .temp
- mv .temp public
artifacts:
paths:
- public
only:
- test
stages:
- stage1
- stage2
- stage3
step-1:
stage: stage1
script:
- echo "Can be canceled."
interruptible: true
step-2:
stage: stage2
script:
- echo "Can not be canceled."
step-3:
stage: stage3
script:
- echo "Because step-2 can not be canceled, this step will never be canceled, even though set as interruptible."
interruptible: true
deploy-to-production:
script: deploy
resource_group: production