Gitlab-CI:如果作业 A 失败,则指定作业 C 应在作业 B 之后运行 [英] Gitlab-CI: Specify that Job C should run after Job B if Job A fails

查看:10
本文介绍了Gitlab-CI:如果作业 A 失败,则指定作业 C 应在作业 B 之后运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有以下管道:

Job A (deploy) -> Job B (test) -> Job C (remove test deployment)

管道应部署测试映像并在成功部署后对其进行测试.测试后,无论测试输出如何,我都想运行清理脚本,但前提是部署了测试映像(作业 A).

The pipeline should deploy a test image and test it after a successful deployment. After the test I want to run a cleanup script regardless of the test output, but only if the test image (Job A) was deployed.

总结一下:我希望 Gitlab 仅在作业 A 成功时执行作业 C,但在作业 B 之后.

To summarize this: I want Gitlab to execute Job C only if Job A succeeds, but after Job B.

不起作用的事情:

  • when: on-failure(作业 A 或作业 B 可能失败,但只有作业 A 很重要)
  • when: always(可能 Job A 失败导致 Job C 失败)
  • when:on-success(要求所有作业都成功)
  • when: on-failure (Job A or Job B could failed, but only Job A is important)
  • when: always (maybe Job A failed which causes Job C to fail)
  • when: on-success (requires all jobs to succeed)

我知道 GitLab 有一个名为 DAG Pipelines 的功能,它允许您使用 needs 关键字指定对其他作业的多个依赖项,但遗憾的是 when 关键字始终是作用域之前的所有工作.所以你不能说这样的话:

I know that GitLab has a feature called DAG Pipelines which allow you to specify multiple dependencies on other jobs with the needs keyword, but sadly the when keyword is always scoped to all prior jobs. So you are not able to say something like:

when:
    on-success: job-a
    always: job-b

我错过了什么还是没有办法实现这种行为?

Do I miss something or is there no way to achieve such a behaviour?

推荐答案

needs DAG 字段可用于有条件地执行清理(Job C),如果 Job B 失败或成功,但 NOT因为作业 A 失败而跳过.

The needs DAG field can be used to conditionally execute the cleanup (Job C), if Job B fails or succeeds, but NOT when it is skipped because Job A failed.

创建 2 个符合以下布尔条件的清理作业:

Create 2 cleanup jobs that match the following boolean conditions:

  • (作业 A 成功,作业 B 成功):如果之前的所有任务都成功(作业 A 和作业 B),我们可以使用 when: on_success 运行清理.但是,如果作业 A 成功而作业 B 失败,这将不会触发.
  • (Job A 成功,Job B 失败):为了规避前面的未触发清理(Job C)的场景,我们利用了如果 Job B 失败,这意味着 Job一个成功的管道.通过创建重复的清理任务并在 Job B 上指定 needs 标签和 when:on_failure,清理任务将仅在 Job A 成功且 Job B 失败时运行.李>
  • (Job A succeeds and Job B succeeds): If all previous tasks succeed (Job A and Job B), we can run the cleanup with when: on_success. However, this will not trigger if Job A succeeds and Job B fails.
  • (Job A succeeds and Job B fails): To circumvent the previous scenario with an untriggered cleanup (Job C), we make use of the fact that if Job B fails, this implies that Job A succeeded in the pipeline. By creating a duplicate cleanup task and specifying a needs tag on Job B and when: on_failure, the cleanup task will only run if Job A succeeds and Job B fails.

重申:如果 (Job A 成功且 Job B 成功)(Job A 成功且 Job B 失败) 将运行清理作业,通过布尔表达式减少相当于(作业A成功).

To reiterate: a cleanup job will run if (Job A succeeds and Job B succeeds) or (Job A succeeds and Job B fails), which by boolean expression reduction is equivalent to (Job A succeeds).

这里有一个明显的警告,现在有 2 个清理作业显示在管道中;但是,它们是互斥的,只能执行一个.

An obvious caveat here is that there are now 2 cleanup jobs that are displayed in the pipeline; however, they are mutually exclusive and only one could ever be executed.

这是一个示例配置:

stages:
  - deploy
  - test
  - cleanup

deploy_job:
  stage: deploy
  script:
    - echo Deployed
    - "true"
  when: always

test_job:
  stage: test
  script:
    - echo Executing tests
    - "true"
  when: on_success

# a YAML anchor reduces repetition
.cleanup_job: &cleanup_job
  stage: cleanup
  script:
    - echo Cleaned up deployment

cleanup_deployment_success:
  when: on_success
  <<: *cleanup_job

cleanup_deployment_failure:
  needs: ["test_job"]
  when: on_failure
  <<: *cleanup_job

<小时>

在各种故意失败条件下,会产生以下管道状态:


With various intentional fail conditions, the following pipeline states are produced:

  • 管道失败:
  • 管道失败:
  • 通过的管道:

从逻辑上讲,这表示无论作业 B 成功还是失败,只要作业 A 成功,作业 C 就会运行.此外,故障状态保留在整个管道中.

Logically, this indicates that regardless of whether Job B succeeded or failed, Job C runs if Job A succeeded. Furthermore, the failure state is preserved in the overall pipeline.

这篇关于Gitlab-CI:如果作业 A 失败,则指定作业 C 应在作业 B 之后运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆