简体   繁体   English

如何在 GitHub Actions 中失败时重新运行 docker 容器,但工作流仍然通过?

[英]How to rerun a docker container on failure in GitHub Actions, but still have the workflow pass?

I have a testing suite which is a little flaky and will fail on occasion, but if it is re-ran it will pass.我有一个测试套件,它有点不稳定,有时会失败,但如果重新运行它会通过。 This test is ran whenever a push happens on a repository on GitHub.只要在 GitHub 上的存储库上发生推送,就会运行此测试。

So far I have gotten the test to run a second time automatically by having a part of my workflow check if the first attempt failed, and then simply running the same command again.到目前为止,通过让我的工作流程的一部分检查第一次尝试是否失败,然后再次运行相同的命令,我已经让测试自动运行第二次。 Here is a workflow file illustrating what I mean:这是一个工作流文件,说明了我的意思:

  jobs:
    run_tests:
      steps:

        - name: Run Test
        id: first-attempt
        run: docker run test

        - name: Retry again on failure
        id: second-attempt
        if: ${{ failure() }}
        run: docker run test

So as you can see, it runs the first attempt, and if it fails runs it again.如您所见,它运行第一次尝试,如果失败,则再次运行。 This works, but the problem is that if the second attempt succeeds, the status of the test is still a "Fail" because the first attempt failed.这是可行的,但问题是如果第二次尝试成功,测试状态仍然是“失败”,因为第一次尝试失败。

So I'm wondering if there is a more elegant way to go about this, like a 'retry' option.所以我想知道是否有更优雅的方法来解决这个问题,比如“重试”选项。 Or at the very least, if there is a way to explicitly set the action's status as "Successful" if the second attempt passes.或者至少,如果有一种方法可以在第二次尝试通过时将操作的状态显式设置为“成功”。 Maybe by doing something like the following:也许通过执行以下操作:

        - name: Check on failures
        if: steps.first_attempt.outcome != 'fail' && steps.second_attempt.outcome != 'success'
        set_status: "Success"

Thanks for any help谢谢你的帮助

It looks like Lei Yang's answer was mostly correct, but needed the addition of using if: steps.first_attempt.outcome != 'success' rather than if: always()看起来雷杨的答案大部分是正确的,但需要添加使用if: steps.first_attempt.outcome != 'success'而不是if: always()

 jobs:
   run_tests:
     steps:

       - name: Run Test
       id: first-attempt
       run: docker run test
       continue-on-error: true

       - name: Retry again on failure
       id: second-attempt
       if: steps.first_attempt.outcome != 'success'
       run: docker run test

Thank you for the help感谢您的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM