You can use the GitHub Actions workflow repository_dispatch
event trigger to start a workflow by triggering a webhook.
I use this event trigger to start a workflow for building my website when I create or update content in my blog repository. Because my website and the blog content are two separate repositories, I must use the repository_dispatch
event to trigger the website build workflow. Another use case is to start my E2E tests when I deploy a new application version.
infoYou can find more information about the
repository_dispatch
event in the events that trigger workflows documentation.
Configure the workflow to trigger
In the workflow file you want to trigger, you must add the repository_dispatch
event with a types
key. The types
key is an array of event types that you can define to limit your workflow to run on those specific types.
|
|
How to trigger the workflow
You can start the workflow by making a POST
request to the dispatches
endpoint of the GitHub API. The endpoint is POST /repos/{owner}/{repo}/dispatches
.
The request body should contain the event_type
key with the event name you want to trigger. You can also add a client_payload
key with additional data you wish to send to the workflow.
|
|
importantTo be able to perform the call, you need to generate a personal access token. I recommend you use the fine-grained personal access token. The fine-grained personal access token needs read access to the metadata scope and read and write access to the contents scope.
More information can be found in my dispatch a GitHub Action via a fine-grained personal access token article.
Trigger the workflow with the GitHub script action
A helpful workflow action is the GitHub script action. This action allows you to use the Octokit/rest.js
library to interact with the GitHub API and trigger the repository_dispatch
event.
Here is an example of how you can trigger the repository_dispatch
event with the GitHub script action:
|
|
In the script, you need to replace <your repo name>
with the repository’s name where you want to trigger the workflow.
importantBy default, the action uses the GitHub token provided by the workflow. As we call another repository, you need to provide a PAT with the necessary permissions to the target repository. That is why the
github-token
property uses thesecrets.REPO_ACCESS_TOKEN
secret.
Add your PAT to the repository secrets.