Dispatch a GitHub Action workflow with script action
This post is over a year old, some of this information may be out of date.
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.
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.
name: Playwright Testson: repository_dispatch: types: [trigger-e2e-tests]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.
POST https://api.github.com/repos/{owner}/{repo}/dispatches HTTP/1.1Content-Type: application/jsonAuthorization: Bearer <PAT>X-GitHub-Api-Version: 2022-11-28
{ "event_type": "trigger-e2e-tests",}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:
jobs: build: runs-on: ubuntu-latest steps: # All your other steps
- name: Start E2E tests uses: actions/github-script@v7 with: github-token: ${{ secrets.REPO_ACCESS_TOKEN }} script: | await github.rest.repos.createDispatchEvent({ owner: context.repo.owner, repo: "<your repo name>", event_type: "trigger-e2e-tests" });In the script, you need to replace <your repo name> with the repository’s name where you want to trigger the workflow.
Related articles
Manual GitHub workflow triggers for Azure Static Web Site
Which service? Netlify vs Vercel vs Azure Static Web App
#DevHack: cross-platform testing your tool on GitHub Actions
Report issues or make changes on GitHub
Found a typo or issue in this article? Visit the GitHub repository to make changes or submit a bug report.
Comments
Let's build together
Manage content in VS Code
Present from VS Code
Engage with your audience throughout the event lifecycle