How to retry a failed step in github actions workflow?

Hi Team,

I am aware that we have an option to re-run the failed workflow by clicking the button Re-run checks . But that would re-run the entire workflow again. I am also aware that currently we cannot resume the workflow directly from the failed step. But, does GitHub Actions provides a way to automatically retry the failed steps?

say for example, if my maven deploy step fails due to network issues for the first time, it should retry, let’s say 3 times, before it actually fails. Do we have a retry mechanism available ?

Hi @riprasad,

Thank you for reaching this out!

Currently it’s not supported to retry the failed step but only the workflow, you can raise a feature_request here where github product manager will take a review.

As a workaround, you can use below sample code for the retry, please refer to my comment:

    steps:
      - name: test
        continue-on-error: true          # make the step always success
        id: test
        run: |
          exit 1                         # let's say 1st failed. for you, here could be mvn deploy command.
      - name: retry
        continue-on-error: true
        id: retry1
        if: steps.test.outcome=='failure'         # check the step outcome, retry 1st time
        run: |
          exit 1                                # if it fails again. if it's succeeds, the latter steps will be skipped, and status is successful.
      - name: retry 2
        continue-on-error: true
        id: retry2
        if: steps.retry1.outcome=='failure'   # check the step outcome, retry 2nd time
        run: |
          echo retry 2nd time
          exit 1                                            # fail again
      - name: retry 3
        continue-on-error: true
        id: retry3
        if: steps.retry2.outcome=='failure'      # check the step outcome, retry 3rd time
        run: |
          echo retry 3rd time                 # it passed in retry 3
      - name: set the status              # set the workflow status if command failed
        if: always()
        run: |
          if ${{ steps.test.outcome=='success' || steps.retry1.outcome=='success' || steps.retry2.outcome=='success' || steps.retry3.outcome=='success' }}; then
             echo fine
          else
             exit 1
          fi

set continue-on-error: true will always make the step success, however steps.{stepid}.outcome can get the command result, hence at last, add a step to show the real execution status, it will fail if all(test, retry1, retry2, retry3) steps fails, but show passed if there’s a successful step.

Please refer to my workflow for more details.

Thanks.

1 Like