Optimize Your CI/CD Pipeline
Get instant insights into your CI/CD performance and costs. Reduce build times by up to 45% and save on infrastructure costs.
name: 'Usage of repository-dispatch GitHub Action'
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Repository Dispatch
uses: peter-evans/repository-dispatch@v3
with:
event-type: my-event
Repository Dispatch
A GitHub action to create a repository dispatch event
What is a Repository Dispatch Event?
With this GitHub Action, you are enabled to trigger other workflows within the same repository or even in different repositories by means of the repository dispatch event.
How to Dispatch an Event to a Remote Repository using a Personal Access Token (PAT)
The following example demonstrates how a GitHub Action can dispatch a repository event by sending a webhook event to a remote repository with a Personal Access Token (PAT) for workflow execution in different repositories.
- name: Repository Dispatch
uses: peter-evans/repository-dispatch@v3
with:
token: ${{ secrets.PAT }}
repository: username/my-repo
event-type: my-event
How to Receive a Repository Dispatch Event
Example of configuring a workflow to run on repository_dispatch: Note: repository_dispatch
will only run a Workflow if it is inside the default branch.
name: Repository Dispatch
on:
repository_dispatch:
types: [my-event]
jobs:
myEvent:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.client_payload.ref }}
- run: echo ${{ github.event.client_payload.sha }}
How to Dispatch Events to Multiple Repositories
You can dispatch the events to more than one repository with the matrix strategy in your GitHub Actions workflow. Below is an example of matrix strategy and an event dispatched to three different repositories upon a successful build job.
jobs:
build:
# Main workflow job that builds, tests, etc.
dispatch:
needs: build
strategy:
matrix:
repo: ['my-org/repo1', 'my-org/repo2', 'my-org/repo3']
runs-on: ubuntu-latest
steps:
- name: Repository Dispatch
uses: peter-evans/repository-dispatch@v3
with:
token: ${{ secrets.PAT }}
repository: ${{ matrix.repo }}
event-type: my-event
with All Available Options
This example demonstrates how to use a GitHub Action to dispatch a repository event to a remote repository using a Personal Access Token (PAT) and set all the input parameters. This can be useful for triggering workflows in different repositories with specific payload data.
- name: Repository Dispatch
uses: peter-evans/repository-dispatch@v3
with:
token: ${{ secrets.PAT }}
repository: username/my-repo
event-type: my-event
client-payload: '{"ref": "${{ github.ref }}", "sha": "${{ github.sha }}"}'