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
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
when可选值有:
如下为指定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"
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的时候需要格外注意
retry可以直接指定重试次数,如下,表示如果失败了继续实行,做多再重试2次
verify:
stage: verify
script: error
retry: 2
tags:
- docker_in_docker_demo
此外,retry还可以通过max指定最大次数,通过when指定重试的条件,when可选的值如下:
如:
verify-2:
stage: verify
script: error
retry:
max: 2
when:
- script_failure
tags:
- docker_in_docker_demo
timeout: 3s
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
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