gitlab--needs、default

发布时间 2023-07-04 21:18:09作者: 邹邹很busy。

needs 并行阶段

可无序执行作业,无需按照阶段顺序运行某些作业,可以让多个阶段同时运行

例如下面的 ci 文件

stages:
  - build
  - test
  - deploy

module-a-build:
  stage: build
  script: 
    - echo "hello3a"
    - sleep 10
    
module-b-build:
  stage: build
  script: 
    - echo "hello3b"
    - sleep 10

module-a-test:
  stage: test
  script: 
    - echo "hello3a"
    - sleep 10
    
module-b-test:
  stage: test
  script: 
    - echo "hello3b"
    - sleep 10

运行上面的流水线

但现在我们可能需要,当 module-a-build 运行完成之后就运行 module-a-test,当 module-b-build 运行完成之后运行 module-b-test。这时候就需要 needs 了

修改 ci 文件,修改后的如下

stages:
  - build
  - test

module-a-build:
  stage: build
  script: 
    - echo "hello3a"
    - sleep 10
    
module-b-build:
  stage: build
  script: 
    - echo "hello3b"
    - sleep 30

module-a-test:
  stage: test
  script: 
    - echo "hello3a"
    - sleep 10
  needs: ["module-a-build"] # 当 module-a-build 运行完成之后就运行 module-a-test
    
module-b-test:
  stage: test
  script: 
    - echo "hello3b"
    - sleep 10
  needs: ["module-b-build"] # 当 module-b-build 运行完成之后就运行 module-b-test

运行流水线

可以查看作业依赖项

点击依赖关系图也可以看到

制品下载

在使用needs,可通过artifacts: trueartifacts: false来控制工件下载。 默认不指定为 true,表示下载制品

module-a-test:
  stage: test
  script: 
    - echo "hello3a"
    - sleep 10
  needs: 
    - job: "module-a-build"
      artifacts: true # 会下载 module-a-build 产生的制品

default

使用 default 可以定义每个 job 的参数,如果 job 里有,会覆盖 default 里的,例如下面的代码

default: # 定义了一个默认的参数
  tags: # 如果 job 里没有 tages,就使用这个 tags
    - build
  retry: # 如果 job 里没有 retry,就使用这个 tags
    max: 2
  before_script: # 如果 job 里没有 before_script,就使用这个 tags
    - echo "before_script"

stages:
  - build
  - test

build:
  stage: build
  before_script:
    - echo "我是 job 里的"
  script:
    - echo "我是 build 的 job"

 
test:
  stage: test
  script:
    - echo "test 的 job"
    

运行流水线,查看 build 的日志

在来看下 test