How to Add or Remove PR Labels From a Workflow
github-script exposes issues.addLabels and issues.removeLabel so a workflow can adjust PR labels directly.
Call github.rest.issues.addLabels to add and github.rest.issues.removeLabel to drop a single label. Wrap removal in try/catch since removing an absent label returns 404.
Steps
- Grant
issues: write(labels live on the issues API). - Call
addLabelswith an array of names. - Call
removeLabelfor one label, catching the 404 when absent.
Workflow
.github/workflows/ci.yml
permissions:
issues: write
pull-requests: write
jobs:
labels:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
with:
script: |
const common = {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
}
await github.rest.issues.addLabels({ ...common, labels: ['needs-review'] })
try {
await github.rest.issues.removeLabel({ ...common, name: 'wip' })
} catch (e) {
if (e.status !== 404) throw e
}Gotchas
- The label must already exist in the repo or
addLabelscreates it with a default color. removeLabelthrows 404 when the label is not applied; swallow only that status.
Related guides
How to Auto-Label Pull Requests by Changed PathsApply labels to a pull request based on which files it touches using actions/labeler and a .github/labeler.ym…
How to Trigger a Deploy From a PR LabelRun a deployment or preview job only when a specific label is added to a pull request by gating a workflow on…