gitlab--trigger 触发不同项目之间的 CI、 触发同一项目下的其他 ci

发布时间 2023-04-28 22:06:47作者: 邹邹很busy。

trigger

当我们在运行阶段需要运行下一个项目的流水线时,就可以使用 trigger了

创建一个项目 manage_zz

在里面写个 .gitlab-ci.yml 文件,内容如下

stages: # 指定运行的步骤,没有指定就顺序执行
  - build
  - deploy
  - test
  - rebase

build1:
  tags: 
    - k8s
  stage: build
  script:
    - echo "Do your build here"

test1:  
  stage: test
  script:
    - echo "Do a test here"
    - echo "For example run a test suite" 

rebase:
  stage: rebase
  script:
    - echo "Do another parallel test here"
    - echo "For example run a lint test"
  

deploy1:
  tags: 
    - k8s
  stage: deploy
  script:
    - echo "Do your deploy here"

在另一个项目 LRUNWEB 的 .gitlab-ci.yml 里写上 trigger,内容如下

workflow:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "push"' # 当为 push 的时候
      when: never # 为 true 时,永远不执行
    - when: always # 为 false 时,永远执行

stages: # 指定运行的步骤,没有指定就顺序执行
  - build
  - deploy
  - test
  - rebase

build1:
  tags: 
    - k8s
  stage: build
  script:
    - echo "Do your build here"

test1:  
  stage: test
  script:
    - echo "Do a test here"
    - echo "For example run a test suite" 

rebase:
  stage: rebase
  script:
    - echo "Do another parallel test here"
    - echo "For example run a lint test"
  

deploy1:
  # tags: 
  #   - k8s
  stage: deploy
  # script:
  #   - echo "Do your deploy here"
  trigger: # 如果是 trigger 时,不需要 tags 和 script,要不然会报错
    project: root/manage_zz # 项目名称
    branch: main # 分支
    strategy: depend # 状态同步

注意:如果是 trigger ,不需要加 tags 和 script。要不然会报错

运行 LRUNWEB 项目,查看流水线

可以看到,在运行 Deploy 阶段的时候,运行了下游的 manage_zz 项目。当下游项目运行成功时, Deploy 阶段也就成功了

点击下游的流水线 id (#27)跳转到下游项目,可以看到下游也触发了流水线

仅通过流水线触发

我们也可以在 manage_zz 项目里,加上 workflow,只有当上游触发的时候才会执行该 pipeline,其他情况下不会执行,加上下面的代码就可以了

workflow:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "pipeline"' # 当为 pipeline 的时候才会触发,其他情况下不会触发该流水线,也就是上游在 trigger 里调度该项目
      when: always # 为 true 时,永远执行
    - when: never # 为 false 时,永远不执行

触发同一项目下的其他 ci

上面我们是触发不同项目之间的 ci,我们也可以在同一个项目下创建多个 ci 文件,然后去触发对应的 ci

创建两个目录,test1 和 test2,在两个目录下各创建一个 ci.yml 的文件,文件内容如下

stages: # 指定运行的步骤,没有指定就顺序执行
  - build
  - deploy
  - test
  - rebase

build1:
  tags: 
    - k8s
  stage: build
  script:
    - echo "Do your build here"

test1:
  tags: 
    - k8s  
  stage: test
  script:
    - echo "Do a test here"
    - echo "For example run a test suite" 

rebase:
  tags: 
    - k8s
  stage: rebase
  script:
    - echo "Do another parallel test here"
    - echo "For example run a lint test"
  

deploy1:
  tags: 
    - k8s
  stage: deploy
  script:
    - echo "Do your deploy here"

在根项目的 .gitlab-ci.yml 里调用其他 ci 文件,如下

stages: # 指定运行的步骤,没有指定就顺序执行
  - build
  - test
  - build_test1
  - build_test2

build1:
  tags: 
    - k8s
  stage: build
  script:
    - echo "Do your build here"

test1:  
  stage: test
  script:
    - echo "Do a test here"
    - echo "For example run a test suite" 


build-test1:
  stage: build
  trigger:
    include: # 运行 test1 目录下的 ci.yml 文件
      - local: test1/ci.yml # 相对路径
    strategy: depend # 状态同步
  rules: # 触发规则,test1 目录下的任何文件改变时触发
    - changes:
      - test1/* # test1 下的任何文件发生改变就执行
      when: always

build-test2:
  stage: build
  trigger:
    include: # 运行 test2 目录下的 ci.yml 文件
      - local: test2/ci.yml  # 相对路径
    strategy: depend # 状态同步
  rules: # 触发规则,test2 目录下的任何文件改变时触发
    - changes:
      - test2/* # test2 下的任何文件发生改变就执行
      when: always

然后在去 test2 目录下新增一个文件,看会不会触发

我创建了一个 hello.go 文件

在去查看流水线

发现只有 build-test2 触发了 ci,因为我们只在 test2 目录下新增了 hello.go 文件,test1 目录下的文件没有改变,所以不会触发 build-test1 的 ci