How to Trigger a Workflow When a Label Is Added in GitHub Actions
The pull_request labeled type fires whenever a label is added, and an if check narrows it to the exact label you want.
Add types: [labeled] to the pull_request event, then gate the job with if: github.event.label.name == 'deploy-preview'.
Steps
- Add
pull_request:withtypes: [labeled]. - Gate the job on
github.event.label.name. - The event fires once per label addition.
Workflow
.github/workflows/preview.yml
on:
pull_request:
types: [labeled]
jobs:
preview:
if: github.event.label.name == 'deploy-preview'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: ./deploy-preview.shGotchas
- A PR opened with a label already applied does not fire
labeled; only later additions do. github.event.labelexists only for the labeled and unlabeled types, not for other pull_request types.
Related guides
How to Trigger a Workflow on an Issue or PR Comment in GitHub ActionsRun a GitHub Actions workflow when someone comments on an issue or pull request using the issue_comment event…
How to Trigger a Workflow When a Review Is Requested in GitHub ActionsRun a GitHub Actions workflow when a reviewer is requested on a pull request using the review_requested activ…