一、基础语法
- only:定义哪些分支和标签的git项目会被执行job
- except:定义哪些分支和标签的git项目将不会被job执行
- rules:允许按照顺序评估规则,知道匹配到并未作业动态提供属性
- workflow
注意:only和except逐渐被抛弃,后续推荐的是使用rules
二、实例演示
yaml文件如下:
before_script:
- echo "script in global before script..."
after_script:
- echo "script in global after script..."
variables:
USERNAME: redrose2100
PASSWORD: admin123
workflow:
rules:
- if: '$USERNAME == "redrose2100"'
when: always
- when: never
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
rules:
- if: '$USERNAME == "redrose2100"'
when: manual
- if: '$USERNAME == "redrose2200"'
when: delayed
start_in: "5"
- when: on_success
test:
stage: test
before_script:
- echo "before script in test..."
script:
- echo "script in test..."
tags:
- docker_in_docker_demo
rules:
- changes:
- Dockerfile
when: manual
allow_failure: true
parallel: 5
release:
stage: release
script:
- echo "script in release..."
after_script:
- echo "after in release..."
only:
- tags
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: echo "in verify"
retry: 2
tags:
- docker_in_docker_demo
verify-2:
stage: verify
script: echo "in verify-2"
retry:
max: 2
when:
- script_failure
tags:
- docker_in_docker_demo
timeout: 3s
only:
- /^dev.*$/
except:
- branches
执行结果如下所示:
三、流水线语法详细解读
3.1 only
可用的关键字有,only是指定的执行
如下,将release步骤使用only指定只有打标签的时候才会执行
release:
stage: release
script:
- echo "script in release..."
after_script:
- echo "after in release..."
only:
- tags
tags:
- docker_in_docker_demo
when: delayed
start_in: "10"
当提交代码后,可以看到这里已经没有release步骤了
3.2 except
可用的关键字,except是指定的不执行,和only相反
except可以单独指定不执行的标签或分支,也可以和only一起组合使用,如下,即表示只有以dev开头的分支才会执行
verify-2:
stage: verify
script: echo "in verify-2"
retry:
max: 2
when:
- script_failure
tags:
- docker_in_docker_demo
timeout: 3s
only:
- /^dev.*$/
except:
- branches
这里因为只有main分支,所有不会执行此步骤,如下:
3.3 rules
rules允许按顺序评估单个规则,直到匹配并未作业动态提供属性
注意rules不能与only、except组合使用
rules可用的规则:
- if :如果条件匹配
- changes:指定文件发生变化
- exists:指定文件存在
如下,使用if语句判断变量USERNAME的值,当是redrose2100时就手动执行,当是redrose2200时就延迟执行,其他情况就是默认之前步骤都成功的时候再执行
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
rules:
- if: '$USERNAME == "redrose2100"'
when: manual
- if: '$USERNAME == "redrose2200"'
when: delayed
start_in: "5"
- when: on_success
如下为判断文件是否有修改的,判断Dockerfile是否有修改,如果有修改则手动执行,如果没有修改,则此步骤不执行
test:
stage: test
before_script:
- echo "before script in test..."
script:
- echo "script in test..."
tags:
- docker_in_docker_demo
rules:
- changes:
- Dockerfile
when: manual
allow_failure: true
parallel: 5
3.4 workflow
顶级workflow关键字适用于整个管道,并将确定是否创建管道
如下,当USERNAME的值为redrose2100时,此时会执行所有的Job,当USERNAME的值修改一下比如redrose2200时,此时整个工作流都不会执行,即没有任何操作,这就是workflow的作用
variables:
USERNAME: redrose2100
PASSWORD: admin123
workflow:
rules:
- if: '$USERNAME == "redrose2100"'
when: always
- when: never