如何在gitlab上获得阶段的状态?

时间:2020-11-12 08:22:22

标签: gitlab gitlab-ci

我有一个类似于下面的gitlab管道

stages
  - test

p1::test:
stage: test
script:
  - echo " parallel 1"
p2::test:
stage: test
script:
  - echo " parallel 2"
p3::test:
stage: test
script:
  - echo " parallel 3"
p4::test:
stage: test
script:
  - echo " parallel 4"

所有这四个作业将并行运行,如何得知阶段test的状态, 我想通知Success如果全部四个都通过了,Failed如果任何工作失败了。

1 个答案:

答案 0 :(得分:0)

判断前一阶段(及其之前的所有内容)是否通过或失败的一种简单方法是添加具有两个使用相反 when 关键字的作业的另一个阶段。

如果一个作业有 when: on_success(默认),它只会在所有先前的作业都成功(或者如果它们失败但有 allow_failure: true,或者有 when: manual 并且没有运行)。如果任何作业失败,则不会。

如果一个作业有 when: on_failure,它会在任何先前的作业失败时运行。

这对于清理构建工件或回滚更改很有用,但它也适用于您的用例。例如,您可以使用以下两个作业:

stages:
  - test
  - verify_tests

p1::test:
  stage: test
  script:
    - echo " parallel 1"

p2::test:
  stage: test
  script:
    - echo " parallel 2"

p3::test:
  stage: test
  script:
    - echo " parallel 3"

p4::test:
  stage: test
  script:
    - echo " parallel 4"

tests_passed:
  stage: verify_tests
  when: on_success # this is the default, so you could leave this off. I'm adding it for clarity
  script:
    - # do whatever you need to when the tests all pass

tests_failed:
  stage: verify_tests
  when: on_failure # this will only run if a job in a prior stage fails
  script:
    - # do whatever you need to when a job fails

如果您需要以编程方式了解每个阶段之后的状态,您可以为每个阶段执行此操作。