Skip to content

Trigger a workflow from another workflow with GitHub Actions

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

  1. Go to your GitHub account settings and create a new GitHub App.
  2. Generate a private key for the app and note down the App ID.
  3. Install the GitHub App on the repository where you want to trigger the workflow.

Step 2: Store the credentials

  1. Add the app_id and app_private_key as secrets in your repository settings. Name them APP_ID and APP_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.

my-org/triggered-repo/.github/workflows/triggered-workflow.yml
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.

my-org/triggering-repo/.github/workflows/triggering-workflow.yml
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

  1. Triggered Workflow:

    • This workflow (triggered-workflow.yml) is set to run on the workflow_dispatch event, which allows it to be triggered manually or by another workflow.
  2. Triggering Workflow:

    • This workflow (triggering-workflow.yml) runs on a push to the main branch.
    • It uses the actions/create-github-app-token ↗ action to generate a GitHub App token using the app_id and app_private_key stored in the repository secrets.
    • It uses the benc-uk/workflow-dispatch ↗ action to trigger the triggered-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.

Note

  • Ensure the GitHub App has the necessary permissions to dispatch events and is installed on the repository.