How to Run a Step Only for a Specific Label in GitHub Actions
Test contains(github.event.pull_request.labels.*.name, 'deploy') to gate on a label.
The labels.*.name array holds every label on the PR. Use contains against it, and trigger on the labeled action so re-labeling re-evaluates.
Steps
- Add
pull_requesttypes[labeled, opened, synchronize]toon. - Gate the step with
contains(github.event.pull_request.labels.*.name, '<label>'). - Match the label name exactly, including case.
Workflow
.github/workflows/ci.yml
on:
pull_request:
types: [labeled, opened, synchronize]
jobs:
preview:
runs-on: ubuntu-latest
steps:
- name: Deploy preview
if: contains(github.event.pull_request.labels.*.name, 'deploy-preview')
run: ./deploy-preview.shGotchas
- The
labels.*.nameobject filter returns an array;containschecks membership. - Without the
labeledtrigger type, adding a label later will not start a new run.
Related guides
How to Match Strings With contains, startsWith, and endsWith in GitHub ActionsUse the GitHub Actions expression functions contains, startsWith, and endsWith to match substrings, prefixes,…
How to Check the Event Name in a Condition in GitHub ActionsBranch GitHub Actions behavior on the trigger using github.event_name, so one workflow can react differently…