GitLabCI-CD流水线语法

GitLabCI-CD流水线语法_第1张图片

目录

文章目录

    • 目录
    • 环境
    • 1、GitLabCI Pipeline
      • 1.Pipeline
      • 2.Stages
      • 3.Job
      • 4.Runner
    • 2、Pipeline开发工具
      • 1.可视化编辑器
      • 2.语法检测校验
      • 3.作业运行日志
    • 3、Pipeline核心语法
      • 1.stages 阶段控制
      • 2.variables 环境变量
      • 3.job 作业默认配置
      • 4.job 作业运行控制
        • 1、allow_failure 允许作业失败
        • 2、when 状态控制和运行方式
        • 3、timeout作业运行超时时间
        • 4、retry 作业失败后重试次数
        • 5、needs 作业关联运行
        • 6、parallel 并行运行
        • 7、rules根据变量/文件控制
      • 5.Pipeline运行控制
        • 1.workflow 控制流水线
        • 2.Git 选项跳过流水线
        • 3.trigger 触发下游管道
    • 4、Pipeline环境变量
    • 5、PipelineTemplates实践
      • 1.extends
      • 2.include
    • 关于我
    • 最后

环境

gitlab-ce:14.9.3-ce.0

1、GitLabCI Pipeline

GitLabCI-CD流水线语法_第2张图片

1.Pipeline

在每个项目中,使用名为imgYAML文件配置GitLab CI/CDimg流水线。

GitLabCI-CD流水线语法_第3张图片

2.Stages

一条流水线可以包含若干个阶段, 一个阶段可以包含若干个作业。

GitLabCI-CD流水线语法_第4张图片

3.Job

作业是具体要执行的任务,命令脚本语句的集合;

GitLabCI-CD流水线语法_第5张图片

4.Runner

Runner是每个作业的执行节点 ;每个作业可以根据标签选择不同的构建节点;

GitLabCI-CD流水线语法_第6张图片

2、Pipeline开发工具

1.可视化编辑器

变更.gitlab-ci.yml文件后, 可以通过Visualize对CI文件中的定义进行可视化;

img

2.语法检测校验

通过Lint可以检测当前CI文件是否存在语法错误;若存在语法错误可以根据提示进行修正;

GitLabCI-CD流水线语法_第7张图片

3.作业运行日志

一条流水线包含很多个作业,每个作业的运行日志可以在Jobs界面看到。

GitLabCI-CD流水线语法_第8张图片

3、Pipeline核心语法

1.stages 阶段控制

源文档

  • .pre阶段的作业总是在流水线开始时执行;
  • .post阶段的作业总是在流水线结束时执行;
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"

GitLabCI-CD流水线语法_第9张图片

如果两个或者多个作业,指向同一个阶段名称,则该阶段下的所有作业都并行运行;如果不能并行运行,需要检查runner的配置文件中的concurrent值, 要大于1。

自己测试过程

  • stages里可以定义各个stage执行的前后顺序,但是.pre和.post2个阶段不受其控制:

image-20220506070615859

  • .pre和.post2个阶段测试:

编写如下代码,并提交运行:

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"

预览:

GitLabCI-CD流水线语法_第10张图片

GitLabCI-CD流水线语法_第11张图片

提交运行:

GitLabCI-CD流水线语法_第12张图片

可以看到,符合预期,.pre先运行,.post最后运行:

image-20220506073243946

测试结束。

实践:gitlab-ruuner上作业并行设置-2022.5.6

如果两个或者多个作业,指向同一个阶段名称,则该阶段下的所有作业都并行运行;如果不能并行运行,需要检查runner的配置文件中的concurrent值, 要大于1。

  • 此时,模拟有2个job指向同一个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"

image-20220506074033058

image-20220506074045761

提交并观察运行状态:

GitLabCI-CD流水线语法_第13张图片

可以看到,此时Test阶段的2个作业不是并行运行的,这是为什么呢?

  • 我们来看下gitlab-runner上的关于concurrent(并行)配置
[root@gitlab-runner ~]#cat /etc/gitlab-runner/config.toml #这里可以看到当前的

GitLabCI-CD流水线语法_第14张图片

[root@gitlab-runner ~]#vim /etc/gitlab-runner/config.toml
concurrent = 10 
#修改完保存退出就好,自动生效
  • 再次运行刚才那条pipeline,观察效果

GitLabCI-CD流水线语法_第15张图片

此时就可以看到Test阶段2个作业同时运行了,符合预期!

2.variables 环境变量

变量可以分为全局变量和局部变量;全局变量是整个流水线可以用的,局部变量是仅在作业中生效的;

variables:
  DEPLOY_ENV: "dev"

deploy_job:
  stage: deploy
  tags:
    - maven
  variables:
    DEPLOY_ENV: "test"
  script:
     - echo  ${DEPLOY_ENV}

实践:局部变量优先级高于全局变量-0222.5.6

  • 编写测试代码(完整代码)

GitLabCI-CD流水线语法_第16张图片

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"
  • 提交并观察效果

GitLabCI-CD流水线语法_第17张图片

看下deploy_job的运行日志:

GitLabCI-CD流水线语法_第18张图片

可以看到输出为test,验证局部变量优先级更改。

测试结束。

3.job 作业默认配置

定义一个作业的时候,一般定义哪些关键字呢? 作业在哪个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 测试

  • 先用环境变量替换掉每个作业里的runner tags:
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"

提交运行:

image-20220506081128314

需要注意:如果before_script和after_script定义在pipeline里,则每个作业里都会运行这2个脚本;如果是定义在某个job里,则只会在其job里运行!

GitLabCI-CD流水线语法_第19张图片

GitLabCI-CD流水线语法_第20张图片

测试结束。

4.job 作业运行控制

语法关键字 作用 备注
allow_failure 控制作业状态,是否允许作业失败,默认值为false 。启用后,如果作业运行失败,该作业将在用户界面中显示橙色警告。img 管道将认为作业成功/通过,不会被阻塞。 假设所有其他作业均成功,则该作业的阶段及其管道将显示相同的橙色警告。但是,关联的提交将被标记为"通过",而不会发出警告。
when 根据状态控制作业运行, 当前面作业成功或者失败时运行。 on_success 前面阶段成功时执行(默认值);on_failure 前面阶段失败时执行;always 总是执行;manual 手动执行;delayed 延迟执行;start_in’5’5 seconds30 minutes1 day1 weeknever 永不执行;
retry 作业重新运行,遇到错误重新运行的次数。 值为整数等于或大于0,但小于或等于2异常分类 (一般就是3次)
timeout 作业运行超时时间;
rules 根据特定的变量或文件变更来控制作业运行; ifchangesexists
needs 作业依赖控制; needs: [“作业名称”]
parallel 生成多个作业,并行运行 parallel:5值 2-50之间

1、allow_failure 允许作业失败

源文档

cibuild:
  tags:
    - build
  stage: build
  script:
    - echo1 "job script....."    ## 报错:命令找不到

GitLabCI-CD流水线语法_第21张图片

添加后:

cibuild:
  tags:
    - build
  stage: build
  script:
    - echo1 "job script....."
  allow_failure: true

image-20220506150309562

测试:allow_failure-2022.5.6

  • 编写代码

GitLabCI-CD流水线语法_第22张图片

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"
  • 提交运行,观察效果:

image-20220506082220348

GitLabCI-CD流水线语法_第23张图片

GitLabCI-CD流水线语法_第24张图片

符合预期效果,测试结束。

2、when 状态控制和运行方式

源文档

  • 根据上游作业的状态决定

    • 当前作业是否运行?
    • 运行的方式?(手动/自动/定时)
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

  • 提交并验证

可以看到这里需要手动执行操作:(审批环节)

GitLabCI-CD流水线语法_第25张图片

GitLabCI-CD流水线语法_第26张图片

测试结束。

测试when 延迟执行-2022.5.6

  • 编写代码:

GitLabCI-CD流水线语法_第27张图片

完整代码如下:

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

  • 提交并验证:

GitLabCI-CD流水线语法_第28张图片

测试结束。

3、timeout作业运行超时时间

build:
  script: build.sh
  timeout: 3 hours 30 minutes

test:
  script: rspec
  timeout: 3h 30m

4、retry 作业失败后重试次数

GitLabCI-CD流水线语法_第29张图片

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

5、needs 作业关联运行

源文档

  • needs 指定要依赖的作业名称
cibuild:
  tags:
    - build
  stage: build
  script:
    - echo "mvn clean package"
    - sleep 30 

citest1:
  tags:
    - build
  stage: test
  needs: ["cibuild"]
  script:
    - echo "mvn test"
GitLabCI-CD流水线语法_第30张图片

测试needs-2022.5.6

  • 编写代码:

GitLabCI-CD流水线语法_第31张图片

完整代码如下:

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
  • 提交并验证:

GitLabCI-CD流水线语法_第32张图片

测试结束。

6、parallel 并行运行

源文档

demojobs:
  stage: test
  parallel: 3
  script: mvn test
GitLabCI-CD流水线语法_第33张图片

测试parallel

  • 编写代码:

GitLabCI-CD流水线语法_第34张图片

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

  • 提交并验证:

GitLabCI-CD流水线语法_第35张图片

测试结束。

7、rules根据变量/文件控制

源文档

citest1 config key may not be used with rules: when.

根据条件(变量)判断: IF

GitLabCI-CD流水线语法_第36张图片

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

GitLabCI-CD流水线语法_第37张图片

GitLabCI-CD流水线语法_第38张图片

image-20220506145811040

#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}"  

测试根据条件(变量)判断

  • 编写代码:

GitLabCI-CD流水线语法_第39张图片

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

  • 提交并验证:

GitLabCI-CD流水线语法_第40张图片

测试结束。

测试根据文件判断: changes

  • 先创建一个dockerfile文件并提交:

GitLabCI-CD流水线语法_第41张图片

  • 编写代码:

GitLabCI-CD流水线语法_第42张图片

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文件,并提交,观察下效果:

  • 这里出错了:。。。

GitLabCI-CD流水线语法_第43张图片

GitLabCI-CD流水线语法_第44张图片

image-20220506132410236

GitLabCI-CD流水线语法_第45张图片

GitLabCI-CD流水线语法_第46张图片

应该是和gitlab-runner上的git版本有关系了:。。。

GitLabCI-CD流水线语法_第47张图片

源码升级gi到2版本以上就可以了:

https://blog.csdn.net/weixin_39246554/article/details/124628706?spm=1001.2014.3001.5502

GitLabCI-CD流水线语法_第48张图片

GitLabCI-CD流水线语法_第49张图片

GitLabCI-CD流水线语法_第50张图片

GitLabCI-CD流水线语法_第51张图片

测试

GitLabCI-CD流水线语法_第52张图片

  • 先创建一个demo目录,在底下创建一个Dockerfile并提交

GitLabCI-CD流水线语法_第53张图片

  • 编写代码

GitLabCI-CD流水线语法_第54张图片

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

  • 提交并验证:

GitLabCI-CD流水线语法_第55张图片

发现build阶段不见了:。。,这里再次运行下流水线看看

GitLabCI-CD流水线语法_第56张图片

可以看到此时就有效果了:

GitLabCI-CD流水线语法_第57张图片

GitLabCI-CD流水线语法_第58张图片

可以看到也是可以实现的。

测试结束。

5.Pipeline运行控制

1.workflow 控制流水线

源文档

控制管道是否创建和运行。根据条件(变量)判断: IF

  • if 定义变量条件;

  • variables 重新定义变量的值;

  • when

    • always
    • never
if: ‘$CI_PIPELINE_SOURCE == “merge_request_event”’ 合并请求时运行流水线;
if: ‘$CI_PIPELINE_SOURCE == “push”’ 提交代码运行流水线;
workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "push"
      when: never

自己测试过程

  • 编写代码:

GitLabCI-CD流水线语法_第59张图片

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
  • 提交并观察效果:

GitLabCI-CD流水线语法_第60张图片

测试结束。

2.Git 选项跳过流水线

  • 提交信息中添加关键字 [ci skip] 或者 [skip ci]
  • Git 2.10 更高版本,可以通过以下配置设置CI/CD;
## 跳过
git push -o ci.skip

## 传递变量
git push -o ci.variable="MAX_RETRIES=10" -o ci.variable="MAX_TIME=600"

3.trigger 触发下游管道

源文档

  • 触发项目管道
  • 触发子管道

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'

实践:触发项目管道

  • 先创建一个Project,里面创建.gitlab-ci.yml文件,然后写入之前的简短代码

devops4-app-ui

GitLabCI-CD流水线语法_第61张图片

GitLabCI-CD流水线语法_第62张图片

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"

提交后,可以看到正在运行了:

GitLabCI-CD流水线语法_第63张图片

  • 来到devops4/devops4-app-service项目的.gitlab-ci.yml里编写代码:

GitLabCI-CD流水线语法_第64张图片

# 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阶段就能调到触发项目的位置了:

GitLabCI-CD流水线语法_第65张图片

测试结束。

实践:仅能通过其他Pipeline触发-2022.5.6

GitLabCI-CD流水线语法_第66张图片

  • 需求:仅能通过其他Pipeline触发
  • 先测试下,每次操作的CI_PIPELINE_SOURCE属性值是什么

来到devops4-app-ui工程下,编辑代码如下:

image-20220506210248600

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进行提交,并观察现象:

GitLabCI-CD流水线语法_第67张图片

可以看到$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

GitLabCI-CD流水线语法_第68张图片

  • 此时,再来编辑devops4-app-ui的代码

image-20220506210954793

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"

  • 写完代码后,直接提交,观察效果:

GitLabCI-CD流水线语法_第69张图片

发现,提交并不能运行这条流水线。

  • 我们再运行一次devops4-app-serice流水线,发现就可以正常触发devops4-app-ui管道了:

GitLabCI-CD流水线语法_第70张图片

GitLabCI-CD流水线语法_第71张图片

测试结束。

实践:触发子管道-2022.5.6

  • 新建一个项目:devops4-monrepo-service

GitLabCI-CD流水线语法_第72张图片

  • 在项目下分别创建如下文件
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-module1build-module2作业的运行,及Downstream的运行:

GitLabCI-CD流水线语法_第73张图片

image-20220506222000460

GitLabCI-CD流水线语法_第74张图片

  • 此时更改.gitlab-ci.yml内容,添加rules,如果该工程下的那个module下的文件发生了改变,就只会触发自己的module运行:

GitLabCI-CD流水线语法_第75张图片

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  

保存提交代码后,发现并不会触发流水线的运行!

  • 现在,我们先后修改下module1/ci.yml内容和module2/ci.yml内容,提交并观察,是否只会触发自己模块的流水线?

GitLabCI-CD流水线语法_第76张图片

GitLabCI-CD流水线语法_第77张图片

符合预期。

测试结束。

4、Pipeline环境变量

预定义变量信息:https://docs.gitlab.com/ee/ci/variables/predefined_variables.html

代码类

  • CI_COMMIT_AUTHOR 提交人
  • CI_COMMIT_BRANCH 提交分支
  • CI_COMMIT_MESSAGE
  • CI_COMMIT_REF_NAME
  • CI_COMMIT_SHORT_SHA

作业类:

  • CI_JOB_ID
  • CI_JOB_NAME
  • CI_JOB_STAGE
  • CI_JOB_URL

流水线类:

  • CI_PIPELINE_ID
  • CI_PIPELINE_SOURCE
  • CI_PIPELINE_TRIGGERED
  • CI_PIPELINE_URL

5、PipelineTemplates实践

https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/ci/templates

1.extends

xtends可以指定多个模板, 相同的参数最后模板会覆盖前面的。例如: 下面两个模板中,继承关系是:

  • 继承 .test1 (此时rspec的变量NAME的值为gitlab)
  • 继承 .test2 (此时rspec的变量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工程里测试

在文件最后添加代码如下:

GitLabCI-CD流水线语法_第78张图片

# 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"
  • 提交并观察现象:

GitLabCI-CD流水线语法_第79张图片

GitLabCI-CD流水线语法_第80张图片

符合相同的参数最后模板会覆盖前面概念,测试结束。

2.include

include用于在CI/CD 配置中引入外部 YAML 文件。可以将一个长.gitlab-ci.yml文件分解为多个文件以提高可读性,或减少同一配置在多个地方的重复。

  • local 导入当前仓库中的文件;
  • file 导入当前项目或其他项目库中的文件;
  • remote 导入一个远程的文件(例如:http://xxx.yaml)
  • template 导入GitLab官方提供的模板文件;
## 本地仓库文件
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'

综合实例:

将模板文件,放到单独的一个仓库中;

GitLabCI-CD流水线语法_第81张图片

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(这个有点难度)

老师文档

  1. 创建一个新的项目 “devops4/devops4-devops-service” (模板库);
  2. 为项目添加一个CI模板文件(ci/ci.yml);(参考pipeline.yml)
  3. 添加cicd.yml 文件, pipeline, include导入模板库中的ci/ci.yml

GitLabCI-CD流水线语法_第82张图片

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

image-20220506233706473

自己测试过程

  • 创建模板库devops4-devops-service

GitLabCI-CD流水线语法_第83张图片

创建如下相关文件:

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

GitLabCI-CD流水线语法_第84张图片

写完后提交。

获取cicd.yml文件的路径:

GitLabCI-CD流水线语法_第85张图片

http://172.29.9.101/devops4/devops4-devops-service/-/raw/main/cicd.yml

GitLabCI-CD流水线语法_第86张图片

  • 来到另一个项目里devops4-monrepo-service

image-20220506235944620

在项目>Settings>CI/CD>Gerneral pipelines里把刚才拷贝的那个链接填在这里,保存:

GitLabCI-CD流水线语法_第87张图片

  • 然后,直接运行这个流水线,观察效果:

GitLabCI-CD流水线语法_第88张图片

GitLabCI-CD流水线语法_第89张图片

符合预期,实验

关于我

我的博客主旨:

  • 排版美观,语言精炼;
  • 文档即手册,步骤明细,拒绝埋坑,提供源码;
  • 本人实战文档都是亲测成功的,各位小伙伴在实际操作过程中如有什么疑问,可随时联系本人帮您解决问题,让我们一起进步!
  1. 个人微信二维码:x2675263825 (舍得), qq:2675263825。

    image-20211002091450217

  2. 个人微信公众号:《云原生架构师实战》

    GitLabCI-CD流水线语法_第90张图片

  3. 个人csdn

    https://blog.csdn.net/weixin_39246554?spm=1010.2135.3001.5421

    GitLabCI-CD流水线语法_第91张图片

  4. 个人已开源干货

    不服来怼:宇宙中最好用的云笔记 & 其他开源干货:https://www.yuque.com/go/doc/73723298?#

    GitLabCI-CD流水线语法_第92张图片

  5. 个人博客:(www.onlyyou520.com)

  6. 知乎:https://www.zhihu.com/people/foryouone/posts

    image-20220507123311568

最后

好了,关于本次实验就到这里了,感谢大家阅读,最后祝大家生活快乐,每天都过的有意义哦,我们下期见!

你可能感兴趣的:(CI/CD,Gitlab,ci,gitlab)