Note: if you want to trigger a workflow from a workflow present in the same repository, you do not need to create a GitHub App and can skip to Step 3 (and remove the token
input from step 4, so that the default ${{ secrets.GITHUB_TOKEN }}
is used).
Step 1: Create the GitHub App
- Go to your GitHub account settings and create a new GitHub App.
- Generate a private key for the app and note down the
App ID
. - Install the GitHub App on the repository where you want to trigger the workflow.
Step 2: Store the credentials
- Add the
app_id
andapp_private_key
as secrets in your repository settings. Name themAPP_ID
andAPP_PRIVATE_KEY
.
Step 3: Create the triggered workflow
Create a workflow file (e.g., .github/workflows/triggered-workflow.yml
) that will be triggered by another workflow.
name: Triggered Workflow
on: workflow_dispatch:
jobs: build: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4
- name: Run a script run: echo "This is the triggered workflow"
Step 4: Create the triggering workflow
Create another workflow file (e.g., .github/workflows/triggering-workflow.yml
) that will trigger the above workflow.
name: Triggering Workflow
on: push: branches: - main
jobs: trigger: runs-on: ubuntu-latest steps: - name: Generate GitHub App token id: generate_token uses: actions/create-github-app-token@v1 with: app_id: ${{ secrets.APP_ID }} private_key: ${{ secrets.APP_PRIVATE_KEY }}
- name: Trigger another workflow uses: benc-uk/workflow-dispatch@v1 with: workflow: triggered-workflow.yml token: ${{ steps.generate_token.outputs.token }} repo: my-org/triggered-repo ref: main
Explanation
-
Triggered Workflow:
- This workflow (
triggered-workflow.yml
) is set to run on theworkflow_dispatch
event, which allows it to be triggered manually or by another workflow.
- This workflow (
-
Triggering Workflow:
- This workflow (
triggering-workflow.yml
) runs on a push to themain
branch. - It uses the
actions/create-github-app-token
↗ action to generate a GitHub App token using theapp_id
andapp_private_key
stored in the repository secrets. - It uses the
benc-uk/workflow-dispatch
↗ action to trigger thetriggered-workflow.yml
by dispatching a workflow dispatch event. - The
workflow
input specifies the workflow file to be triggered. - The
token
input uses the generated GitHub App token. - The
repo
input specify the repository where the event should be dispatched. - The
ref
input specifies the branch or tag to use for the triggered workflow run.
- This workflow (
Note
- Ensure the GitHub App has the necessary permissions to dispatch events and is installed on the repository.