1 minute read

How to re-run only failed jobs on GitHub Actions

Introduction

Have you ever come across a GitHub Actions workflow that stumbled into level ground failed without good reason? If so I believe you’re well familiar with this ruthless button:

Preview

Yeah, the thing is that it restarts absolutely all the jobs (green, red, whatever), even if just the only one has failed. You can imagine how annoying this can be. While GitHub Actions doesn’t provide a way to re-run just the failed jobs out of the box, let’s build something on our own.

Implementation

Step-by-step guide

  1. Set up a step to get the current timestamp

     - id: timestamp
       run: echo "::set-output name=timestamp::$(timestamp +%s)"
    
  2. Name a cache container using the above timestamp

     - uses: actions/cache@v2
       with:
         path: |
           run_result
         key: ${{ github.run_id }}-${{ github.job }}-${{ steps.timestamp.outputs.timestamp }}
         restore-keys: |
           ${{ github.run_id }}-${{ github.job }}-
    
  3. Restore the previous result

     - id: run_result
       run: cat run_result 2>/dev/null || echo 'default'
    
  4. Set up a condition for further steps

     - uses: actions/checkout@v2
       if: steps.run_result.outputs.run_result != 'success'
    
  5. Save the result on success

     - run: echo "::set-output name=run_result::success" > run_result
    

Sample

Pros

  • CI time will be noticeably reduced
  • Developers nerves will be safely rescued
  • This approach is 100% configurable and can be implemented only for the chosen jobs and steps

Cons

  • The only drawback I could come up with is that the logs of the successful jobs will be cleared on re-run

Conclusion

The idea was to make our GitHub Actions more flexible and compromisable to speed up the development cycle, avoid time-consuming obstacles and increase fault tolerance.

Although this pattern is controversial enough, but on the other hand, you are now fully equipped to give your failed jobs a second chance (:

Updated: