How to Retry Failed Steps in GitHub Action Workflows
Allan M. Jeremy
3 min read
This article was written over 18 months ago and may contain information that is out of date. Some content may still be relevant, but please refer to official documentation for the latest information.
Sometimes things can go wrong in your GitHub Action workflow step(s), and you may want to retry them. In this article, we'll cover two methods for doing this!
GitHub workflow files are usually .yaml/.yml files that contain a series of jobs and steps to be executed by GitHub Actions. These files often reside in .github/workflows. If the directories do not exist, go ahead and create them. Create a file retry.yml in .github/workflows. For now, the file can contain the following:
name:"Retry action using retry step"on:# This action is called when this is pushed to githubpush:# This action can be manually triggeredworkflow_call:jobs:# This name is up to youretry-job:runs-on:"ubuntu-latest"name:MyJobsteps:-name:Checkoutrepositoryuses:actions/checkout@v3-name:Printrun:|echo'Hello'
You can test your GitHub Action workflow by pushing your changes to GitHub and going to the actions tab of the repository. You can also choose to test locally using Act.
By using the retry-step action, we can retry any failed shell commands. If our step or series of steps are shell commands, we can use the retry-step action to retry them.
If, however, you'd like to try retry a step that is using another action, then the retry-step action will NOT work for you. In that case, you may want to try the alternative steps mentioned below.
Modify your action file to contain the following:
name:"Retry action using retry step"on:# This action is called when this is pushed to githubpush:# This action can be manually triggeredworkflow_call:jobs:# This name is up to youretry-job:runs-on:"ubuntu-latest"name:MyJobsteps:-name:Checkoutrepositoryuses:actions/checkout@v3-name:Usethereusableworkflow# Use the retry actionuses:nick-fields/retry@v2with:max_attempts:3retry_on:errortimeout_seconds:5# You can specify the shell commands you want to retry herecommand:|echo'some command that would potentially fail'
If you are trying to retry steps that use other actions, the retry-step action may not get the job done. In this case, you can still retry steps by retrying steps conditionally, depending on whether or not a step failed.
GitHub provides us with two main additional attributes in our steps:
continue-on-error - Setting this to true means that the even if the current step fails, the job will continue on to the next one (by default failure stops a job's running).
steps.{id}.outcome - where {id} is an id you add to the steps you want to retry.
This can be used to tell whether a step failed or not, potential values include 'failure' and 'success'.
if - allows us to conditionally run a step
name:"Retry action using retry step"on:# This action is called when this is pushed to GitHubpush:# This action can be manually triggeredworkflow_call:jobs:# This name is up to youretry-job:runs-on:"ubuntu-latest"name:MyJobsteps:-name:Checkoutrepositoryuses:actions/checkout@v3-name:Someactionthatcanfail# You need to specify an id to be able to tell what the status of this action wasid:myStepId1# This needs to be true to proceed to the next step of failurecontinue-on-error:trueuses:actions/someaction# Duplicate of the step that might fail ~ manual retry-name:Someactionthatcanfailid:myStepId2# Only run this step if step 1 fails. It knows that step one failed because we specified an `id` for the first stepif:steps.myStepId1.outcome=='failure'# This needs to be true to proceed to the next step of failurecontinue-on-error:trueuses:actions/someaction
If you want to retry multiple steps at once, then you can use composite actions to group the steps you want to retry, and then use the duplicate steps approach mentioned above.
If you are retrying a step that is only shell commands, then you can use the retry step action.
If you are retrying a step that needs to use another action, then you can use duplication of steps with conditional running to manually retry the steps.
About the author
Allan M. Jeremy
Senior Software Engineer
Allan Jeremy is a self-proclaimed polymath, entrepreneur and Senior Software Engineer at ThisDot Labs. He is an AI and crypto enthusiast that loves to learn most things tech and business as well as sharing what he learns.