DEV Community

loading...
Cover image for Restart your GitHub Actions workflow when something failed

Restart your GitHub Actions workflow when something failed

Elio Struyf
Elio Struyf is an independent Engineering Lead working for Valo Intranet, Blogger, and Speaker. Loves stickers - if you want stickers as well, get them at https://pyod.shop.
・2 min read

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

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.

##################################
### 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 }}" }'
Enter fullscreen mode Exit fullscreen mode

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

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

Automatically restart the flow when it failed

I hope this helps you fully automate your processes.

Article originally posted on eliostruyf.com

Discussion (0)

Forem Open with the Forem app