Restart your GitHub Actions workflow when something failed
Tags: devops, github, actions, showdev
Published: 2021-03-10 13:55:47

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](https://getdoctor.io) 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](https://getdoctor.io) 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](https://docs.github.com/en/actions/reference/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](https://github.com/estruyf/doctor/blob/main/.github/workflows/release.yml)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lf76u6q8z2my56cnbpsq.png)

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.

```yaml
##################################
### 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 }}" }'
```

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.

![Create PAT with workflow scope](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hge69eyx1lcwehptqf95.png)

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

![Automatically restart the flow when it failed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ihosqbk94uvwzcvp97f.png)

I hope this helps you fully automate your processes.

*Article originally posted on [eliostruyf.com](https://www.eliostruyf.com/restart-github-actions-workflow-failed/)*