GitHub Actions Contexts and Expressions
Separate expression and shell evaluation
A context exposes structured information such as github.event_name, github.sha, matrix.os, or prior step information. GitHub evaluates expressions in supported workflow fields using ${{ ... }}. Bash variables such as $PR_TITLE are expanded later by the shell on the runner. Context availability depends on the field and event. An if expression may omit the delimiters in ordinary cases; quote or wrap expressions starting with ! because YAML treats it specially.
Keep data out of shell source
A pull request title is user-controlled data. Inserting it directly into a run script using an expression can change the script before the shell interprets it. Instead pass the title through a step environment variable and quote its shell expansion. Do not use eval or reparse the value as a command. Environment passing reduces this injection risk but does not make arbitrary downstream use safe.
Use a context safely
Add this step to an appropriate pull_request workflow for a reviewed title classification. It runs only for pull requests; the title is treated as data and never printed. The pattern Docs:* matches a title starting with Docs: in Bash. The step does not need write permissions or a secret. A title is not proof that a change is only documentation.
- name: Identify pull request title if: github.event_name == 'pull_request' env: PR_TITLE: ${{ github.event.pull_request.title }} shell: bash run: | if [[ "$PR_TITLE" == Docs:* ]]; then printf '%s\n' 'Documentation title' else printf '%s\n' 'Other title' fiUnderstand values and outputs
Use env for values supplied to commands, vars for configured non-secret values, and secrets for authorized sensitive values when truly required. A step with id can set an output using the runner-provided GITHUB_OUTPUT file, and later steps can refer to steps.<id>.outputs.<name>. Outputs are not automatically shell variables or automatically safe to log. Do not print tokens or complete contexts; masking is not an assurance against disclosure.
Verify the boundary
The local lab sets PR_TITLE using a subprocess environment, not by interpolating shell source. It checks a documentation title, a normal title, empty text, and a title containing shell punctuation and a newline. All are classified as data; no extra command is executed. The exercise does not emulate GitHub expression evaluation. Predict which titles match before running the lab.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
GitHub Actions Contexts and Expressions — knowledge check
Reason about GitHub Actions workflow behavior and safety.
Which is a GitHub expression rather than a Bash variable?
How should a user-controlled title reach a shell script?
What does the title prefix Docs: prove?
Where does a step set an output using the file interface?
Why avoid logging entire contexts?