是否可以允许 CI/CD 作业中的脚本失败?

WoJ*_*WoJ 2 gitlab gitlab-ci

可以允许整个作业失败

job1:
  stage: test
  script:
    - execute_script_that_will_fail
  allow_failure: true
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

例如,docker 部署具有 a build(不得失败)、stop容器的 a(如果容器未运行,则可能会失败)和 a run(不得失败)。

我当前的解决方法是将其分成单独的工作,但这是一个丑陋的黑客:

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
Run Code Online (Sandbox Code Playgroud)

161*_*903 6

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

job1:
  stage: test
  script:
    - execute_script_that_MAY_fail || true
    - execute_script_that_MUST_NOT_fail
Run Code Online (Sandbox Code Playgroud)