gitlab-ce:14.9.3-ce.0
在每个项目中,使用名为的YAML文件配置GitLab CI/CD流水线。
一条流水线可以包含若干个阶段, 一个阶段可以包含若干个作业。
作业是具体要执行的任务,命令脚本语句的集合;
Runner是每个作业的执行节点 ;每个作业可以根据标签选择不同的构建节点;
变更.gitlab-ci.yml文件后, 可以通过Visualize
对CI文件中的定义进行可视化;
通过Lint可以检测当前CI文件是否存在语法错误;若存在语法错误可以根据提示进行修正;
一条流水线包含很多个作业,每个作业的运行日志可以在Jobs
界面看到。
源文档
stages:
- build
- test
- deploy
ciinit:
tags:
- build
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- build
stage: .post
script:
- echo "Pipeline end job"
如果两个或者多个作业,指向同一个阶段名称,则该阶段下的所有作业都并行运行;如果不能并行运行,需要检查runner的配置文件中的concurrent
值, 要大于1。
自己测试过程
.pre和.post
2个阶段不受其控制:.pre和.post
2个阶段测试:编写如下代码,并提交运行:
stages: #对stages的编排
- build
- test
- deploy
ciinit:
tags:
- build
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- build
stage: .post
script:
- echo "Pipeline end job"
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
build1:
tags:
- maven
stage: build
script:
- echo "Do your build here"
test:
tags:
- maven
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
deploy:
tags:
- maven
stage: deploy
script:
- echo "Do your deploy here"
预览:
提交运行:
可以看到,符合预期,.pre
先运行,.post
最后运行:
测试结束。
实践:gitlab-ruuner上作业并行设置
-2022.5.6
如果两个或者多个作业,指向同一个阶段名称,则该阶段下的所有作业都并行运行;如果不能并行运行,需要检查runner的配置文件中的
concurrent
值, 要大于1。
test阶段
stages: #对stages的编排
- build
- test
- deploy
ciinit:
tags:
- build
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- build
stage: .post
script:
- echo "Pipeline end job"
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
build1:
tags:
- maven
stage: build
script:
- echo "Do your build here"
test:
tags:
- maven
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 10
test1:
tags:
- maven
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 10
deploy:
tags:
- maven
stage: deploy
script:
- echo "Do your deploy here"
提交并观察运行状态:
可以看到,此时Test阶段的2个作业不是并行运行的,这是为什么呢?
concurrent(并行)
配置[root@gitlab-runner ~]#cat /etc/gitlab-runner/config.toml #这里可以看到当前的
[root@gitlab-runner ~]#vim /etc/gitlab-runner/config.toml
concurrent = 10
#修改完保存退出就好,自动生效
此时就可以看到Test阶段2个作业同时运行了,符合预期!
变量可以分为全局变量和局部变量;全局变量是整个流水线可以用的,局部变量是仅在作业中生效的;
variables:
DEPLOY_ENV: "dev"
deploy_job:
stage: deploy
tags:
- maven
variables:
DEPLOY_ENV: "test"
script:
- echo ${DEPLOY_ENV}
实践:局部变量优先级高于全局变量
-0222.5.6
stages: #对stages的编排
- build
- test
- deploy
variables:
DEPLOY_ENV: "dev"
deploy_job:
stage: deploy
tags:
- maven
variables:
DEPLOY_ENV: "test"
script:
- echo ${DEPLOY_ENV}
ciinit:
tags:
- build
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- build
stage: .post
script:
- echo "Pipeline end job"
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
build1:
tags:
- maven
stage: build
script:
- echo "Do your build here"
test:
tags:
- maven
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 10
test1:
tags:
- maven
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 10
deploy:
tags:
- maven
stage: deploy
script:
- echo "Do your deploy here"
看下deploy_job
的运行日志:
可以看到输出为test,验证局部变量优先级更改。
测试结束。
定义一个作业的时候,一般定义哪些关键字呢? 作业在哪个runner运行? 作业属于流水线中的哪个阶段? 这个作业要做什么?
variables:
BUILD_RUNNER: k8s
## job名称
cibuild:
tags:
- build
- ${BUILD_RUNNER} #注意:在14版本以前,tags这里是不支持使用环境变量的!
- devops
stage: build
before_script: #像这个before_script既可以在pipeline里定义,也可以在job里定义!
- echo "job before_script......."
script:
- echo "job script....."
after_script:
- echo "job after_script......."
参数解析:
语法关键字 | 作用 | 备注 |
---|---|---|
variables | 定义作业中的环境变量; | |
tags | 根据标签选择运行作业的构建节点; | 多个标签, 则匹配具有所有标签的构建节点;GitLab14.1版本后, 标签的值可以使用变量;GitLab14.3版本后, 标签数量必须小于50; |
stage | 指定当前作业所属的阶段名称; | |
before_script | 作业在运行前执行的Shell命令行; | |
script | 作业在运行中执行的Shell命令行; | 每个作业至少要包含一个script; |
after_script | 作业在运行后执行的Shell命令行; |
实践:将全部作业里的tags内容用变量替换 & before_script和after_script 测试
stages: #对stages的编排
- build
- test
- deploy
variables:
DEPLOY_ENV: "dev"
RUNNER_TAG: "maven"
deploy_job:
stage: deploy
tags:
- ${RUNNER_TAG}
variables:
DEPLOY_ENV: "test"
script:
- echo ${DEPLOY_ENV}
ciinit:
tags:
- ${RUNNER_TAG}
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- ${RUNNER_TAG}
stage: .post
script:
- echo "Pipeline end job"
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
build1:
tags:
- ${RUNNER_TAG}
stage: build
script:
- echo "Do your build here"
test:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 3
test1:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 3
deploy:
tags:
- ${RUNNER_TAG}
stage: deploy
script:
- echo "Do your deploy here"
提交运行:
需要注意:如果before_script和after_script
定义在pipeline里,则每个作业里都会运行这2个脚本;如果是定义在某个job里,则只会在其job里运行!
测试结束。
源文档
cibuild:
tags:
- build
stage: build
script:
- echo1 "job script....." ## 报错:命令找不到
添加后:
cibuild:
tags:
- build
stage: build
script:
- echo1 "job script....."
allow_failure: true
测试:allow_failure
-2022.5.6
stages: #对stages的编排
- build
- test
- deploy
variables:
DEPLOY_ENV: "dev"
RUNNER_TAG: "maven"
deploy_job:
stage: deploy
tags:
- ${RUNNER_TAG}
variables:
DEPLOY_ENV: "test"
script:
- echo ${DEPLOY_ENV}
ciinit:
tags:
- ${RUNNER_TAG}
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- ${RUNNER_TAG}
stage: .post
script:
- echo "Pipeline end job"
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
build1:
tags:
- ${RUNNER_TAG}
stage: build
script:
- echo "Do your build here"
test:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- ech
- sleep 3
allow_failure: true
test1:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 3
deploy:
tags:
- ${RUNNER_TAG}
stage: deploy
script:
- echo "Do your deploy here"
符合预期效果,测试结束。
源文档
根据上游作业的状态决定
cibuild:
tags:
- build
stage: build
before_script:
- echo1 "job before_script......."
script:
- echo "job script....."
after_script:
- echo "job after_script......."
allow_failure: false
citest1:
tags:
- build
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
when: on_success #只有上面作业成功后才会运行。
测试when的手动执行
-20222.5.6
stages: #对stages的编排
- build
- test
- deploy
variables:
DEPLOY_ENV: "dev"
RUNNER_TAG: "maven"
deploy_job:
stage: deploy
tags:
- ${RUNNER_TAG}
variables:
DEPLOY_ENV: "test"
script:
- echo ${DEPLOY_ENV}
ciinit:
tags:
- ${RUNNER_TAG}
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- ${RUNNER_TAG}
stage: .post
script:
- echo "Pipeline end job"
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
build1:
tags:
- ${RUNNER_TAG}
stage: build
script:
- echo "Do your build here"
test:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- ech
- sleep 3
allow_failure: true
test1:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 3
deploy:
tags:
- ${RUNNER_TAG}
stage: deploy
script:
- echo "Do your deploy here"
when: manual
可以看到这里需要手动执行操作:(审批环节)
测试结束。
测试when 延迟执行
-2022.5.6
完整代码如下:
stages: #对stages的编排
- build
- test
- deploy
variables:
DEPLOY_ENV: "dev"
RUNNER_TAG: "maven"
deploy_job:
stage: deploy
tags:
- ${RUNNER_TAG}
variables:
DEPLOY_ENV: "test"
script:
- echo ${DEPLOY_ENV}
ciinit:
tags:
- ${RUNNER_TAG}
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- ${RUNNER_TAG}
stage: .post
script:
- echo "Pipeline end job"
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
build1:
tags:
- ${RUNNER_TAG}
stage: build
script:
- echo "Do your build here"
test:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- ech
- sleep 3
allow_failure: true
test1:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 3
when: delayed
start_in: '5'
deploy:
tags:
- ${RUNNER_TAG}
stage: deploy
script:
- echo "Do your deploy here"
when: manual
测试结束。
build:
script: build.sh
timeout: 3 hours 30 minutes
test:
script: rspec
timeout: 3h 30m
cibuild:
tags:
- build
stage: build
retry: 2
before_script:
- echo1 "job before_script......."
script:
- echo "job script....."
after_script:
- echo "job after_script......."
allow_failure: false
根据特定的错误匹配:
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 :调度失败。
data_integrity_failure :结构完整性问题。
####
max :最大重试次数 when :重试失败的错误类型
cibuild:
tags:
- build
stage: build
retry:
max: 2
when:
- script_failure
before_script:
- echo1 "job before_script......."
script:
- echo "job script....."
after_script:
- echo "job after_script......."
allow_failure: false
源文档
cibuild:
tags:
- build
stage: build
script:
- echo "mvn clean package"
- sleep 30
citest1:
tags:
- build
stage: test
needs: ["cibuild"]
script:
- echo "mvn test"
测试needs
-2022.5.6
完整代码如下:
stages: #对stages的编排
- build
- test
- deploy
variables:
DEPLOY_ENV: "dev"
RUNNER_TAG: "maven"
deploy_job:
stage: deploy
tags:
- ${RUNNER_TAG}
variables:
DEPLOY_ENV: "test"
script:
- echo ${DEPLOY_ENV}
ciinit:
tags:
- ${RUNNER_TAG}
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- ${RUNNER_TAG}
stage: .post
script:
- echo "Pipeline end job"
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
build1:
tags:
- ${RUNNER_TAG}
stage: build
script:
- echo "Do your build here"
test:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- ech
- sleep 10
allow_failure: true
test1:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 3
when: delayed
start_in: '5'
needs: #注意:当needs和上面延迟在一起时,needs优先级更高!
- test
deploy:
tags:
- ${RUNNER_TAG}
stage: deploy
script:
- echo "Do your deploy here"
when: manual
测试结束。
源文档
demojobs:
stage: test
parallel: 3
script: mvn test
测试parallel
stages: #对stages的编排
- build
- test
- deploy
variables:
DEPLOY_ENV: "dev"
RUNNER_TAG: "maven"
deploy_job:
stage: deploy
tags:
- ${RUNNER_TAG}
variables:
DEPLOY_ENV: "test"
script:
- echo ${DEPLOY_ENV}
ciinit:
tags:
- ${RUNNER_TAG}
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- ${RUNNER_TAG}
stage: .post
script:
- echo "Pipeline end job"
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
build1:
tags:
- ${RUNNER_TAG}
stage: build
script:
- echo "Do your build here"
parallel: 5
test:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- ech
- sleep 10
allow_failure: true
test1:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 3
when: delayed
start_in: '5'
needs:
- test
deploy:
tags:
- ${RUNNER_TAG}
stage: deploy
script:
- echo "Do your deploy here"
when: manual
测试结束。
源文档
citest1 config key may not be used with rules
: when.
根据条件(变量)判断: IF
variables:
DOMAIN: example.com
codescan:
stage: build
tags:
- build
rules:
- if: '$DOMAIN == "example.com"'
when: manual
- when: on_success
script:
- echo "codescan"
- sleep 5;
#parallel: 5
根据文件判断: changes
exists
#exists
citest1:
tags:
- build
stage: test
rules:
- exists:
- Dockerfile
when: manual
script:
- echo "Do a test here"
- echo "For example run a test suite"
#variables
variables:
ENV_TYPE: "dev"
cddeploy:
tags:
- build
stage: deploy
rules:
- if: $CI_COMMIT_REF_NAME == "master"
variables:
ENV_TYPE: "prod"
script:
- echo "Deploy env ${ENV_TYPE}"
测试根据条件(变量)判断
stages: #对stages的编排
- build
- test
- deploy
variables:
DEPLOY_ENV: "dev"
RUNNER_TAG: "maven"
deploy_job:
rules:
- if: '$DEPLOY_ENV == "dev"'
when: manual
- when: on_success
stage: deploy
tags:
- ${RUNNER_TAG}
variables:
DEPLOY_ENV2: "test"
script:
- echo ${DEPLOY_ENV}
ciinit:
tags:
- ${RUNNER_TAG}
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- ${RUNNER_TAG}
stage: .post
script:
- echo "Pipeline end job"
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
build1:
tags:
- ${RUNNER_TAG}
stage: build
script:
- echo "Do your build here"
parallel: 5
test:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "love you"
- sleep 10
allow_failure: true
test1:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 3
when: delayed
start_in: '5'
needs:
- test
deploy:
tags:
- ${RUNNER_TAG}
stage: deploy
script:
- echo "Do your deploy here"
when: manual
测试结束。
测试根据文件判断: changes
stages: #对stages的编排
- build
- test
- deploy
variables:
DEPLOY_ENV: "dev"
RUNNER_TAG: "maven"
deploy_job:
rules:
- if: '$DEPLOY_ENV == "dev"'
when: manual
- when: on_success
stage: deploy
tags:
- ${RUNNER_TAG}
variables:
DEPLOY_ENV2: "test"
script:
- echo ${DEPLOY_ENV}
ciinit:
tags:
- ${RUNNER_TAG}
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- ${RUNNER_TAG}
stage: .post
script:
- echo "Pipeline end job"
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
build1:
tags:
- ${RUNNER_TAG}
stage: build
script:
- echo "Do your build here"
parallel: 5
rules:
- changes:
- Dockerfile
when: manual
test:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "love you"
- sleep 10
allow_failure: true
test1:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 3
when: delayed
start_in: '5'
needs:
- test
deploy:
tags:
- ${RUNNER_TAG}
stage: deploy
script:
- echo "Do your deploy here"
when: manual
提交,然后此时再修改下刚才那个Dockerfile文件,并提交,观察下效果:
应该是和gitlab-runner上的git版本有关系了:。。。
源码升级gi到2版本以上就可以了:
https://blog.csdn.net/weixin_39246554/article/details/124628706?spm=1001.2014.3001.5502
测试
stages: #对stages的编排
- build
- test
- deploy
variables:
DEPLOY_ENV: "dev"
RUNNER_TAG: "maven"
deploy_job:
rules:
- if: '$DEPLOY_ENV == "dev"'
when: manual
- when: on_success
stage: deploy
tags:
- ${RUNNER_TAG}
variables:
DEPLOY_ENV2: "test"
script:
- echo ${DEPLOY_ENV}
ciinit:
tags:
- ${RUNNER_TAG}
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- ${RUNNER_TAG}
stage: .post
script:
- echo "Pipeline end job"
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
build1:
tags:
- ${RUNNER_TAG}
stage: build
script:
- echo "Do your build here"
parallel: 5
rules:
- changes:
- /demo/**
when: manual
test:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "love you"
- sleep 10
allow_failure: true
test1:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 3
when: delayed
start_in: '5'
needs:
- test
deploy:
tags:
- ${RUNNER_TAG}
stage: deploy
script:
- echo "Do your deploy here"
when: manual
发现build阶段不见了:。。,这里再次运行下流水线看看
可以看到此时就有效果了:
可以看到也是可以实现的。
测试结束。
源文档
控制管道是否创建和运行。根据条件(变量)判断: IF
if 定义变量条件;
variables 重新定义变量的值;
when
if: ‘$CI_PIPELINE_SOURCE == “merge_request_event”’ | 合并请求时运行流水线; |
---|---|
if: ‘$CI_PIPELINE_SOURCE == “push”’ | 提交代码运行流水线; |
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "push"
when: never
自己测试过程
stages: #对stages的编排
- build
- test
- deploy
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "push"
when: never
- when: always
variables:
DEPLOY_ENV: "dev"
RUNNER_TAG: "maven"
deploy_job:
rules:
- if: '$DEPLOY_ENV == "dev"'
when: manual
- when: on_success
stage: deploy
tags:
- ${RUNNER_TAG}
variables:
DEPLOY_ENV2: "test"
script:
- echo ${DEPLOY_ENV}
ciinit:
tags:
- ${RUNNER_TAG}
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- ${RUNNER_TAG}
stage: .post
script:
- echo "Pipeline end job"
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
build1:
tags:
- ${RUNNER_TAG}
stage: build
script:
- echo "Do your build here"
parallel: 5
rules:
- changes:
- /demo/**
when: manual
test:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "love you"
- sleep 10
allow_failure: true
test1:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 3
when: delayed
start_in: '5'
needs:
- test
deploy:
tags:
- ${RUNNER_TAG}
stage: deploy
script:
- echo "Do your deploy here"
when: manual
测试结束。
[ci skip]
或者 [skip ci]
;## 跳过
git push -o ci.skip
## 传递变量
git push -o ci.variable="MAX_RETRIES=10" -o ci.variable="MAX_TIME=600"
源文档
strategy: 默认情况下,一旦创建了下游管道,trigger作业就会以success状态完成。要强制等待下游管道完成,使用 strategy: depend
。
触发项目管道:
triggers:
stage: deploy
trigger:
project: devops/devops-maven-service
branch: main
strategy: depend ## 状态同步
触发子管道:
#这种场景适合一个项目里有多个子模块
triggers:
stage: deploy
trigger:
include:
- local: /ci/java-pipeline.yml
strategy: depend ## 状态同步
## 不同项目
triggers:
stage: deploy
trigger:
include:
- project: 'devops/my-pipeline-lib'
ref: 'main'
file: '/cd/java-pipeline.yml'
实践:触发项目管道
.gitlab-ci.yml
文件,然后写入之前的简短代码devops4-app-ui
stages: #对stages的编排
- build
- test
- deploy
variables:
DEPLOY_ENV: "dev"
RUNNER_TAG: "maven"
deploy_job:
stage: deploy
tags:
- ${RUNNER_TAG}
variables:
DEPLOY_ENV: "test"
script:
- echo ${DEPLOY_ENV}
ciinit:
tags:
- ${RUNNER_TAG}
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- ${RUNNER_TAG}
stage: .post
script:
- echo "Pipeline end job"
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
build1:
tags:
- ${RUNNER_TAG}
stage: build
script:
- echo "Do your build here"
- sleep 10
test:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 5
deploy:
tags:
- ${RUNNER_TAG}
stage: deploy
script:
- echo "Do your deploy here"
提交后,可以看到正在运行了:
devops4/devops4-app-service
项目的.gitlab-ci.yml
里编写代码:# workflow: #test workflow.test1
# rules:
# - if: '$CI_PIPELINE_SOURCE == "push"'
# when: never
# - when: always
stages: #对stages的编排
- build
- test
- deploy
variables:
DEPLOY_ENV: "dev"
RUNNER_TAG: "maven"
deploy_job:
rules:
- if: '$DEPLOY_ENV == "dev"'
when: manual
- when: on_success
stage: deploy
tags:
- ${RUNNER_TAG}
variables:
DEPLOY_ENV2: "test"
script:
- echo ${DEPLOY_ENV}
ciinit:
tags:
- ${RUNNER_TAG}
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- ${RUNNER_TAG}
stage: .post
script:
- echo "Pipeline end job"
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
build:
stage: build
trigger:
project: devops4/devops4-app-ui
branch: main
strategy: depend #状态同步
build1:
tags:
- ${RUNNER_TAG}
stage: build
script:
- echo "Do your build here"
parallel: 5
rules:
- changes:
- /demo/**
when: manual
test:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "love you"
- sleep 10
allow_failure: true
test1:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 3
when: delayed
start_in: '5'
needs:
- test
deploy:
tags:
- ${RUNNER_TAG}
stage: deploy
script:
- echo "Do your deploy here"
when: manual
此时,我们到devops4/devops4-app-ui项目下一条管道在运行了,符合预期效果!
这里不需要进入到这个目录,直接点击这个build阶段就能调到触发项目的位置了:
测试结束。
实践:仅能通过其他Pipeline触发
-2022.5.6
CI_PIPELINE_SOURCE
属性值是什么来到devops4-app-ui
工程下,编辑代码如下:
stages: #对stages的编排
- build
- test
- deploy
variables:
DEPLOY_ENV: "dev"
RUNNER_TAG: "maven"
deploy_job:
stage: deploy
tags:
- ${RUNNER_TAG}
variables:
DEPLOY_ENV: "test"
script:
- echo ${DEPLOY_ENV}
ciinit:
tags:
- ${RUNNER_TAG}
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- ${RUNNER_TAG}
stage: .post
script:
- echo "Pipeline end job"
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
build1:
tags:
- ${RUNNER_TAG}
stage: build
script:
- echo "Do your build here"
- echo $CI_PIPELINE_SOURCE
- sleep 3
test:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 3
deploy:
tags:
- ${RUNNER_TAG}
stage: deploy
script:
- echo "Do your deploy here"
此时,直接在自己Editor进行提交,并观察现象:
可以看到$CI_PIPELINE_SOURCE
值为push
。
此时,来到devops4-app-serice
工程下,进行提交,并再次观察devops4-app-ui
里变量的值:
# workflow: #test workflow.test1
# rules:
# - if: '$CI_PIPELINE_SOURCE == "push"'
# when: never
# - when: always
stages: #对stages的编排
- build
- test
- deploy
variables:
DEPLOY_ENV: "dev"
RUNNER_TAG: "maven"
deploy_job:
rules:
- if: '$DEPLOY_ENV == "dev"'
when: manual
- when: on_success
stage: deploy
tags:
- ${RUNNER_TAG}
variables:
DEPLOY_ENV2: "test"
script:
- echo ${DEPLOY_ENV}
ciinit:
tags:
- ${RUNNER_TAG}
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- ${RUNNER_TAG}
stage: .post
script:
- echo "Pipeline end job"
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
build:
stage: build
trigger:
project: devops4/devops4-app-ui
branch: main
strategy: depend #状态同步
build1:
tags:
- ${RUNNER_TAG}
stage: build
script:
- echo "Do your build here"
parallel: 5
rules:
- changes:
- /demo/**
when: manual
test:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "love you"
- sleep 10
allow_failure: true
test1:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 3
when: delayed
start_in: '5'
needs:
- test
deploy:
tags:
- ${RUNNER_TAG}
stage: deploy
script:
- echo "Do your deploy here"
when: manual
我们可以看到是pipeline
:
devops4-app-ui
的代码workflow:
rules:
- if: '$CI_PIPELINE_SOURCE == "pipeline"'
when: always
- when: never
stages: #对stages的编排
- build
- test
- deploy
variables:
DEPLOY_ENV: "dev"
RUNNER_TAG: "maven"
deploy_job:
stage: deploy
tags:
- ${RUNNER_TAG}
variables:
DEPLOY_ENV: "test"
script:
- echo ${DEPLOY_ENV}
ciinit:
tags:
- ${RUNNER_TAG}
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- ${RUNNER_TAG}
stage: .post
script:
- echo "Pipeline end job"
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
build1:
tags:
- ${RUNNER_TAG}
stage: build
script:
- echo "Do your build here"
- echo $CI_PIPELINE_SOURCE
- sleep 3
test:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 3
deploy:
tags:
- ${RUNNER_TAG}
stage: deploy
script:
- echo "Do your deploy here"
发现,提交并不能运行这条流水线。
devops4-app-serice
流水线,发现就可以正常触发devops4-app-ui
管道了:测试结束。
实践:触发子管道
-2022.5.6
devops4-monrepo-service
devops4-monrepo-service工程
module1/ci.yml
module2/ci.yml
.gitlab-ci.yml
ci.yml文件内容为简单的一个流水线:
stages: #对stages的编排
- build
- test
- deploy
variables:
DEPLOY_ENV: "dev"
RUNNER_TAG: "maven"
deploy_job:
stage: deploy
tags:
- ${RUNNER_TAG}
variables:
DEPLOY_ENV: "test"
script:
- echo ${DEPLOY_ENV}
ciinit:
tags:
- ${RUNNER_TAG}
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- ${RUNNER_TAG}
stage: .post
script:
- echo "Pipeline end job"
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
build1:
tags:
- ${RUNNER_TAG}
stage: build
script:
- echo "Do your build here"
- sleep 3
test:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 3
deploy:
tags:
- ${RUNNER_TAG}
stage: deploy
script:
- echo "Do your deploy here"
.gitlab-ci.yml
文件内容如下:
注意下:这里的job下是没有tags的!
stages: #对stages的编排
- build
build-module1:
stage: build
trigger:
include:
- local: module1/ci.yml
strategy: depend
build-module2:
stage: build
trigger:
include:
- local: module2/ci.yml
strategy: depend
然后提交触发构建,查看效果:
会看到同时触发build-module1
和build-module2
作业的运行,及Downstream的运行:
.gitlab-ci.yml
内容,添加rules,如果该工程下的那个module下的文件发生了改变,就只会触发自己的module运行:stages: #对stages的编排
- build
build-module1:
stage: build
trigger:
include:
- local: module1/ci.yml
strategy: depend
rules:
- changes:
- module1/*
when: always
build-module2:
stage: build
trigger:
include:
- local: module2/ci.yml
strategy: depend
rules:
- changes:
- module2/*
when: always
保存提交代码后,发现并不会触发流水线的运行!
符合预期。
测试结束。
预定义变量信息:https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
代码类
作业类:
流水线类:
https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/ci/templates
xtends可以指定多个模板, 相同的参数最后模板会覆盖前面的。例如: 下面两个模板中,继承关系是:
NAME
的值为gitlab)NAME
的值为gitlabCI , 覆盖了.test1中的值).test1: #模板:它不会实际地去运行!
variables:
NAME: "gitlab"
tags:
- build
stage: test
rules:
- if: $CI_COMMIT_BRANCH == "main"
script: echo "mvn test"
.test2:
variables:
NAME: "gitlabCI"
tags:
- build01
stage: test
rspec:
extends:
- .test1
- .test2
script: echo " DevOps"
###### 结果
rspec:
variables:
NAME: "gitlabCI"
tags:
- build
stage: test
rules:
- if: $CI_COMMIT_BRANCH == "main"
script: echo " DevOps"
实践:PipelineTemplate之extends
-2022.5.6
devops4-app-ui
工程里测试在文件最后添加代码如下:
# workflow:
# rules:
# - if: '$CI_PIPELINE_SOURCE == "pipeline"'
# when: always
# - when: never
stages: #对stages的编排
- build
- test
- deploy
variables:
DEPLOY_ENV: "dev"
RUNNER_TAG: "maven"
deploy_job:
stage: deploy
tags:
- ${RUNNER_TAG}
variables:
DEPLOY_ENV: "test"
script:
- echo ${DEPLOY_ENV}
ciinit:
tags:
- ${RUNNER_TAG}
stage: .pre
script:
- echo "Pipeline init first job"
ciend:
tags:
- ${RUNNER_TAG}
stage: .post
script:
- echo "Pipeline end job"
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
build1:
tags:
- ${RUNNER_TAG}
stage: build
script:
- echo "Do your build here"
- echo $CI_PIPELINE_SOURCE
- sleep 3
test:
tags:
- ${RUNNER_TAG}
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- sleep 3
deploy:
tags:
- ${RUNNER_TAG}
stage: deploy
script:
- echo "Do your deploy here"
.test1:
variables:
NAME: "gitlab"
tags:
- build01
stage: test
rules:
- if: $CI_COMMIT_BRANCH == "main"
script: echo "mvn test"
.test2:
variables:
NAME: "gitlabCI"
tags:
- build
stage: test
rspec:
extends:
- .test1
- .test2
script: echo "$NAME"
符合相同的参数最后模板会覆盖前面
概念,测试结束。
include用于在CI/CD 配置中引入外部 YAML 文件。可以将一个长.gitlab-ci.yml
文件分解为多个文件以提高可读性,或减少同一配置在多个地方的重复。
## 本地仓库文件
include:
- local: '/templates/.gitlab-ci-java.yml'
## 其他仓库文件
include:
- project: 'devops/my-project'
ref: main
file:
- '/templates/.gitlab-ci-java.yml'
- '/templates/.tests.yml'
## 远程文件
include:
- remote: 'https://192.168.1.200//-/raw/main/.gitlab-ci.yml'
综合实例:
将模板文件,放到单独的一个仓库中;
build.yml
## build模板
.build:
tags:
- build
stage: build
variables:
BUILD_TOOLS: "maven"
SKIP_TEST: "true"
script:
- if [ "${SKIP_TEST}" == "true" ];then echo "mvn clean package -DskipTests";fi
rules:
- if: $CI_PIPELINE_SOURCE == "push"
when: never
.mavenBuild:
tags:
- build
stage: build
variables:
BUILD_SHELL: "mvn clean package"
script:
- echo "${BUILD_SHELL}"
gitlab-ci.yml
## 引入项目分支中的build.yml
include:
- project: 'devops03/gitlabci-library-service'
ref: "main"
file:
- "/build.yml"
stages:
- build
ciBuild:
tags:
- build
extends:
- .mavenBuild
ciTriggerTest:
tags:
- build
stage: build
script:
- echo ${BUILD_TOOL}
实践:练习Pipeline模板库(测试成功)
-2022.5.6(这个有点难度)
老师文档
ci/ci.yml: ‘devops4/devops4-devops-service 模板库
.build:
variables:
SHELL: "mvn clean build"
tags:
- build
stage: build
script: echo "${SHELL}"
cicd.yml
## 导入仓库文件
include:
- project: 'devops4/devops4-devops-service'
ref: main
file:
- '/ci/ci.yml'
variables:
BUILD_SHELL: "mvn build"
stages:
- build
## 继承模板
build:
extends:
- .build
variables:
SHELL: $BUILD_SHELL
远程CI文件配置:
http://192.168.1.200/devops4/devops4-devops-service/-/raw/main/cicd.yml
自己测试过程
devops4-devops-service
创建如下相关文件:
ci/ci.yml
.build:
variables:
SHELL: "mvn clean build"
tags:
- build
stage: build
script: echo "${SHELL}"
cicd.yml
## 导入仓库文件
include:
- project: 'devops4/devops4-devops-service'
ref: main
file:
- '/ci/ci.yml'
variables:
BUILD_SHELL: "mvn build"
stages:
- build
## 继承模板
build:
extends:
- .build
variables:
SHELL: $BUILD_SHELL
写完后提交。
获取cicd.yml
文件的路径:
http://172.29.9.101/devops4/devops4-devops-service/-/raw/main/cicd.yml
devops4-monrepo-service
在项目>Settings
>CI/CD
>Gerneral pipelines
里把刚才拷贝的那个链接填在这里,保存:
符合预期,实验
我的博客主旨:
个人微信二维码:x2675263825 (舍得), qq:2675263825。
个人微信公众号:《云原生架构师实战》
个人csdn
https://blog.csdn.net/weixin_39246554?spm=1010.2135.3001.5421
个人已开源干货
不服来怼:宇宙中最好用的云笔记 & 其他开源干货:https://www.yuque.com/go/doc/73723298?#
个人博客:(www.onlyyou520.com)
知乎:https://www.zhihu.com/people/foryouone/posts
好了,关于本次实验就到这里了,感谢大家阅读,最后祝大家生活快乐,每天都过的有意义哦,我们下期见!