简体   繁体   中英

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.

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()

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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