GitlabCI学习笔记之四:GitLabRunner pipeline语法之only except rules workflow
1.only & except
参考文档:https://docs.gitlab.com/ee/ci/yaml/#only--except
only和except是两个参数用分支策略来限制jobs构建,后面会逐步被rules替代
only定义哪些分支和标签的git项目将会被job执行。
except定义哪些分支和标签的git项目将不会被job执行
示例
job:
# use regexp
only:
- /^issue-.*$/
# use special keyword
except:
- branches
2.rules
rules
允许按顺序评估单个规则对象的列表,直到一个匹配并为作业动态提供属性. 请注意, rules
不能only/except
与only/except
组合使用。
可用的规则条款包括:
1.if (类似于only:variables )
2.changes ( only:changes相同)
3。exists
示例1:rules:if
如果DOMAIN
的值匹配,则需要手动运行。不匹配则默认匹配到on_success
。 条件判断从上到下,匹配即停止。多条件匹配可以使用&& ||
variables:
DOMAIN: www.example.com
deploy:
stage: deploy
script:
- echo "hello deploy"
- sleep 2;
rules:
- if: '$DOMAIN == "example.com"'
when: manual
- if: '$DOMAIN == "www.example.com"'
when: delayed
start_in: '10'
- when: on_success
示例2:rules:changes
接受文件路径数组。 如果提交中Jenkinsfile
文件发生的变化则为true。
codescan:
stage: codescan
tags:
- build
script:
- echo "codescan"
- sleep 5;
#parallel: 5
rules:
- changes:
- Jenkinsfile
when: manual
- if: '$DOMAIN == "example.com"'
when: on_success
- when: on_success
示例3:rules:exists
接受文件路径数组。当仓库中存在指定的文件时操作。
codescan:
stage: codescan
tags:
- build
script:
- echo "codescan"
- sleep 5;
#parallel: 5
rules:
- exists:
- Jenkinsfile
when: manual
- changes:
- Jenkinsfile
when: on_success
- if: '$DOMAIN == "example.com"'
when: on_success
- when: on_success
示例4:rules:allow_failure
使用allow_failure: true
rules:
在不停止管道本身的情况下允许作业失败或手动作业等待操作.
job:
script: "echo Hello, Rules!"
rules:
- if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"'
when: manual
allow_failure: true
在此示例中,如果第一个规则匹配,则作业将具有以下when: manual
和allow_failure: true
。
3.workflow
顶级workflow:
关键字适用于整个管道,并将确定是否创建管道。when
:可以设置为always
或never
. 如果未提供,则默认值always
。
示例
variables:
DOMAIN: example.com
workflow:
rules:
- if: '$DOMAIN == "example.com"' #如果不匹配则 不执行pipeline
- when: never
4.综合示例
before_script:
- echo "before-script!!"
variables:
DOMAIN: example.com
workflow:
rules:
- if: '$DOMAIN == "example.com"'
when: always
- when: never
stages:
- build
- test
- codescan
- deploy
build:
before_script:
- echo "before-script in job"
stage: build
script:
- echo "mvn clean "
- echo "mvn install"
- ech "$DOMAIN"
after_script:
- echo "after script in buildjob"
rules:
- exists:
- Dockerfile
when: on_success
allow_failure: true
- changes:
- Dockerfile
when: manual
- when: on_failure
unittest:
stage: test
script:
- ech "run test"
when: delayed
start_in: '5'
allow_failure: true
retry:
max: 1
when:
- script_failure
timeout: 1 hours 10 minutes
deploy:
stage: deploy
script:
- echo "hello deploy"
- sleep 2;
rules:
- if: '$DOMAIN == "example.com"'
when: manual
- if: '$DOMAIN == "aexample.com"'
when: delayed
start_in: '5'
- when: on_failure
codescan:
stage: codescan
script:
- echo "codescan"
- sleep 5;
when: on_success
parallel: 5
after_script:
- echo "after-script"
"一劳永逸" 的话,有是有的,而 "一劳永逸" 的事却极少