一、基本语法
- tags: 指定gitlab-runner
- allow_failure:允许当前作业失败
- when:指定执行条件
- timeout:设置超时时间
- retry:设置失败重执行
- parallel:设置并行执行
二、实例演示
yaml文件如下:
before_script:
- echo "script in global before script..."
after_script:
- echo "script in global after script..."
variables:
USERNAME: redrose2100
PASSWORD: admin123
stages:
- build
- test
- release
- deploy
- verify
setup:
stage: .pre
script:
- echo "script in pre..."
tags:
- docker_in_docker_demo
teardown:
stage: .post
script:
- echo "script in post..."
tags:
- docker_in_docker_demo
build:
stage: build
before_script:
- echo "before script in build..."
- echo $USERNAME
- echo $PASSWORD
script:
- echo "script in build..."
after_script:
- echo "after script in build..."
tags:
- docker_in_docker_demo
test:
stage: test
before_script:
- echo "before script in test..."
script:
- echo "script in test..."
- error
tags:
- docker_in_docker_demo
allow_failure: true
parallel: 5
release:
stage: release
script:
- echo "script in release..."
after_script:
- echo "after in release..."
tags:
- docker_in_docker_demo
when: delayed
start_in: "10"
deploy:
stage: deploy
script:
- echo "script in deploy..."
tags:
- docker_in_docker_demo
when: manual
verify:
stage: verify
script: error
retry: 2
tags:
- docker_in_docker_demo
verify-2:
stage: verify
script: error
retry:
max: 2
when:
- script_failure
tags:
- docker_in_docker_demo
timeout: 3s
三、流水线语法详细解读
3.1 tags
tags用于指定gitlab-runner,tags后面的值为gitlab-runner的tag值,如下表示指定build作业使用docker_in_docker_demo标签的gitlab-runner执行
build:
stage: build
before_script:
- echo "before script in build..."
- echo $USERNAME
- echo $PASSWORD
script:
- echo "script in build..."
after_script:
- echo "after script in build..."
tags:
- docker_in_docker_demo
3.2 when
when可选值有:
- on_succes:前面阶段中的所有作业都成功时才执行作业,默认值
- on_failure:当前面几段出现失败时执行
- always:总是执行作业
- manual:手动执行作业
- delayed:延迟执行作业
如下为指定deploy作业为手工操作,这个在部署环节很有用,一般来说不希望自动去部署到生产环境,此时就可以通过设置手工的方式,当经过一定的测试验证后再手工点击即可完成部署
deploy:
stage: deploy
script:
- echo "script in deploy..."
tags:
- docker_in_docker_demo
when: manual
流水线中效果如下:
deplayed可以配置start_in参数使用,设置延迟执行,如下为延迟10秒执行当前任务
release:
stage: release
script:
- echo "script in release..."
after_script:
- echo "after in release..."
tags:
- docker_in_docker_demo
when: delayed
start_in: "10"
3.3 allow_failure
allow_failure是允许当前作业失败,这个在比如测试脚本开发的过程中等情况下是非常有用的,比如脚本还没有调试稳定,失败不表示产品有问题的时候就可以设置当前作业允许失败
如下表示当前test作业允许失败
test:
stage: test
before_script:
- echo "before script in test..."
script:
- echo "script in test..."
- error
tags:
- docker_in_docker_demo
allow_failure: true
parallel: 5
如下,允许失败的作业如果有失败,则会显示感叹号,这里需要注意的是允许失败的作业即使失败了,结果同样是认为成功的,在配置使用when的时候需要格外注意
3.4 retry
retry可以直接指定重试次数,如下,表示如果失败了继续实行,做多再重试2次
verify:
stage: verify
script: error
retry: 2
tags:
- docker_in_docker_demo
这里点开作业可以看到这里执行了3次
此外,retry还可以通过max指定最大次数,通过when指定重试的条件,when可选的值如下:
- always:在发生任何情况下重试(默认)
- unknown_failure:当失败原因未知时
- script_failure:脚本失败时重试
- api_failure:API失败重试
- stuck_or_timeout_failure:作业卡住或超时
- runner_system_failure:运行系统发生故障
- missing_dependency_failure:如果依赖丢失
- runner_unsupported:Runner不支持
- stale_schedule:无法执行延迟的作业
- job_execution_timeout:脚本超出了为作业设置的最大执行时间
- archived_failure:作业已存档且无法运行
- unmet_prerequisites:作业未能完成先决条件任务
- scheduler_failure:调度程序未能将作业分配给运行scheduler_failure
- data_integrity_failure:检测到结构完整性问题
如:
verify-2:
stage: verify
script: error
retry:
max: 2
when:
- script_failure
tags:
- docker_in_docker_demo
timeout: 3s
效果如下
3.5 timeout
timeout可以用来设置作业超时时间
设置时间的格式比如:
1h 10m # 一个小时10分钟
10m 20s # 10分钟20秒
30s # 30秒
如下:设置超时时间为3s
verify-2:
stage: verify
script: error
retry:
max: 2
when:
- script_failure
tags:
- docker_in_docker_demo
timeout: 3s
3.6 parallel
parallel可以指定一个任务并行运行多次
如下,表示当前任务并行运行5次
test:
stage: test
before_script:
- echo "before script in test..."
script:
- echo "script in test..."
- error
tags:
- docker_in_docker_demo
allow_failure: true
parallel: 5
效果如下