1 minute read

Preview

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:

Re-run jobs

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/[email protected]
       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/[email protected]
       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: