是否可以允许CI/CD作业中的脚本失败? [英] Is it possible to allow for a script in a CI/CD job to fail?
问题描述
整个作业可以允许失败
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
(一定不能失败),容器的 stop
(如果容器未运行可能会失败)的docker部署,以及 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).
我当前的解决方法是将其拆分为单独的作业,但这是一个丑陋的骇客:
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
推荐答案
如果作业中的任何脚本步骤返回失败状态,则该作业将失败.因此,您需要防止这种情况发生,最简单的方法是添加 ||.正确
(或在注释中记录一些类似@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屋!