Restart your GitHub Actions workflow when something failed

March 10, 2021

Sometimes it happens that your GitHub Actions workflow fails. When this happens, it is appropriate to check what exactly went wrong before you restart it.

Sometimes it could be a timeout or something that was incorrectly configured on the site to test. This issue is precisely the case for my doctor build/test/release GitHub Actions workflow as I do so many changes to test out the tool. It happens from time to time during scheduled runs. My environment makes the workflow fail. The solution is to start the workflow again and specify it needs to start clean.

This process is something doctor supports. The only thing that I need to do was a manual restart of the workflow. To make it easier, I wanted to automate this process.

Restarting the workflow

Restarting the GitHub Actions workflow can be achieved by using the workflow_dispatch event trigger.

Info Events that trigger workflows

First of all, you need to add the workflow_dispatch event trigger to your GitHub Actions workflow.

Info Check out the doctor flow here: release.yml on GitHub.

When that trigger is in place, all you need is to implement the job to run when a failure happens that restart your flow. The job itself is nothing more than an API call to the GitHub API, which triggers the workflow.

The API URL is: https://api.github.com/repos/{user}/{repo}/actions/workflows/{workflow-id}/dispatches.

Important You can find the workflow its ID by navigating to the following URL https://api.github.com/repos/estruyf/doctor/actions/workflows.

Get the workflow ID
Get the workflow ID

For the restart process itself, I use a job that only gets triggered when the workflow ran via its schedule, and it failed on one of my builds.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
##################################
### Run when a schedule failed ###
##################################
restart_when_failed:
  name: Restarts the scheduled run when it failed
  runs-on: ubuntu-latest
  if: github.event_name == 'schedule' && failure()
  needs: [build, build_pwsh, build_cmd]
  steps:
    - name: Retry the workflow
      run: |
        curl -i \
        -X POST \
        -H "Accept: application/vnd.github.v3+json" \
        -H "Authorization: token ${{ secrets.ACTIONS_PAT }}" \
        https://api.github.com/repos/estruyf/doctor/actions/workflows/6155745/dispatches \
        -d '{"ref": "${{ github.ref }}" }'

Info By checking for the schedule event type, I can make sure it does not go in an endless loop of restarting the workflow.

In the code block, there are two variables in use. One is the branch ref from on which it was running. You need this value for restarting the flow on the right branch.

The other one is the secrets.ACTIONS_PAT. This secret is a personal access token with the workflow scope.

PAT - workflow scope
PAT - workflow scope

Configure the PAT in your project secrets. Once set, your flow can now automatically restart itself.

Restart workflow from job
Restart workflow from job

I hope this helps you fully automate your processes.

Comments

comments powered by Disqus