GitLab CI:即使脚本失败,如何继续工作 [英] GitLab CI: How to continue job even when script fails

查看:268
本文介绍了GitLab CI:即使脚本失败,如何继续工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在管道中有一份工作,其中包含两个非常重要的步骤的脚本:

I have a job in my pipeline that has a script with two very important steps:

  • mvn test 对我的代码运行JUnit测试
  • junit2html 将测试的XML结果转换为HTML格式(由于我的管道不是通过MR完成的,因此是唯一的查看结果的可能方法),并作为工件上传到GitLab
  • docker rm 破坏管道中较早创建的容器
  • mvn test to run JUnit tests against my code
  • junit2html to convert the XML result of the tests to a HTML format (only possible way to see the results as my pipelines aren't done through MRs) that is uploaded to GitLab as an artifact
  • docker rm to destroy a container created earlier in the pipeline

我的问题是,当我的测试失败时,脚本会立即在 mvn test 处停止,因此永远不会达到 junit2html 步骤,这意味着测试结果永远不会上传到发生故障时,也不会执行 docker rm ,因此容器将保留并弄乱随后的管道.

My problem is that when my tests fail, the script stops immediately at mvn test, so the junit2html step is never reached, meaning the test results are never uploaded in the event of failure, and docker rm is never executed either, so the container remains and messes up subsequent pipelines as a result.

我想要的是即使脚本在某个时候失败,也能够将工作一直持续到最后.基本上,该作业在GitLab CI/CD中仍应视为失败,但应执行其整个脚本.我该如何配置?

What I want is to be able to keep a job going till the end even if the script fails at some point. Basically, the job should still count as failed in GitLab CI / CD, but its entire script should be executed. How can I configure this?

推荐答案

即使该步骤失败了,在您需要继续执行的每个步骤中,您都可以在 .gitlab-ci.yml 在该步骤中归档.例如:

In each step that you need to continue even if the step fails, you can add a flag to your .gitlab-ci.yml file in that step. For example:

...
Unit Tests:
  stage: tests
  only:
    - branches
  allow_failure: true
  script:
    - ...

allow_failure:true 标志,即使该特定步骤失败,该标志也将继续执行管道.有关allow_failure的Gitlab CI文档位于: https://docs.gitlab.com/ee/ci/yaml/#allow_failure

It's that allow_failure: true flag that will continue the pipeline even if that specific step fails. Gitlab CI Documentation about allow_failure is here: https://docs.gitlab.com/ee/ci/yaml/#allow_failure

评论更新:如果您需要采取措施在失败后继续前进,并且意识到某些事情失败了,那么这对我来说效果很好:

Update from comments: If you need the step to keep going after a failure, and be aware that something failed, this has worked well for me:

./script_that_fails.sh > /dev/null 2>&1 || FAILED=true

if [ $FAILED ]
    then ./do_something.sh
fi

这篇关于GitLab CI:即使脚本失败,如何继续工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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