是否可以允许 CI/CD 作业中的脚本失败? [英] Is it possible to allow for a script in a CI/CD job to fail?

查看:22
本文介绍了是否可以允许 CI/CD 作业中的脚本失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

整个作业可以允许失败

job1:
  stage: test
  script:
    - execute_script_that_will_fail
  allow_failure: true

是否有可能在一系列脚本中包含一个允许失败(而其他 - 不允许)的脚本?

job1:
  stage: test
  script:
    - execute_script_that_MAY_fail_and_should_be_marked_somehow_in_this_config_as_such
    - execute_script_that_MUST_NOT_fail

理由是可能存在相关的脚本,应该组合在一起并按顺序排列,并且只允许其中一些失败.

The rationale is that there may be scripts which are related, should be grouped together and sequential, and only some of them are allowed to fail.

一个例子可以是一个带有 build 的 docker 部署(不能失败),一个容器的 stop (如果容器没有运行,它可能会失败)和run(不能失败).

An example could be a docker deployment with a build (must not fail), a stop of a container (which may fail if the container is not running) and a run (which must not fail).

我目前的解决方法是将其拆分为单独的作业,但这是一个丑陋的 hack:

My current workaround is to split this into separate jobs but this is an ugly hack:

stages:
  - one
  - two
  - three

one:
  stage: one
  script:
    - execute_script_that_MUST_NOT_fail

two:
  stage: two
  script:
    - execute_script_that_MAY_fail
  allow_failure: true

three:
  stage: three
  script:
    - execute_script_that_MUST_NOT_fail

推荐答案

如果其中的任何脚本步骤返回失败状态,则作业失败.所以你需要防止这种情况发生,最简单的方法是添加 ||true 到该步骤(或像@ahogen 在评论中建议的一些日志记录):

A job fails if any of the script steps inside it return a failed status. So you need to prevent that from happening, and the simplest way is to add || true to the step (or some logging like @ahogen suggests in a comment):

job1:
  stage: test
  script:
    - execute_script_that_MAY_fail || true
    - execute_script_that_MUST_NOT_fail

这篇关于是否可以允许 CI/CD 作业中的脚本失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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