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
First of all, you need to add the workflow_dispatch
event trigger to your GitHub Actions workflow.
InfoCheck 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
.
ImportantYou can find the workflow its ID by navigating to the following URL
https://api.github.com/repos/estruyf/doctor/actions/workflows
.
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.
|
|
InfoBy 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.
Configure the PAT
in your project secrets. Once set, your flow can now automatically restart itself.
I hope this helps you fully automate your processes.