Sub topic 18
Object filters with the * syntax
This chapter assumes you can read a workflow file and know that ${{ }} evaluates an expression. It also assumes you know what the github and steps contexts are.
Object filters with the * syntax
Many contexts hold arrays of objects. The pull request payload, for example, has a labels array, and each label is an object with a name, a color, and other properties. You often want one property from every item. An object filter does that with *.
The expression github.event.pull_request.labels.*.name reads as "the name of every item in labels". The result is an array of the matching values. If a pull request has the labels bug and docs, the filter returns ["bug", "docs"].
An array is awkward to print or compare as it is. Pair the filter with a function that accepts arrays:
join(array, separator)turns the array into one string.contains(array, item)checks whether an item is in the array.toJSON(array)serializes the array for logging.
The next workflow runs only when the pull request carries a release label. It also prints all the label names.
name: label-filter
on:
pull_request:
types: [opened, labeled, synchronize]
jobs:
check:
runs-on: ubuntu-latest
steps:
- name: Print label names
env:
LABELS: ${{ join(github.event.pull_request.labels.*.name, ', ') }}
run: echo "Labels: $LABELS"
- name: Release pull requests only
if: contains(github.event.pull_request.labels.*.name, 'release')
run: echo "This pull request is marked for release"The label names pass through env, as the injection chapter recommends. Label names are user-controlled text, so never paste them straight into a run script.
Filters have a few rules worth knowing:
- Matching is case-insensitive, like other property lookups.
- If the array is empty, the filter returns an empty array and no error occurs.
- The
*also works on objects, where it returns the values of all properties. - A property that is missing on some items is skipped in the result.
Debugging contexts and expressions
When an expression returns an empty string, the cause is usually a wrong property name or a context that does not hold what you expected. Expressions do not raise an error for a missing property. They quietly evaluate to an empty value. So inspect the real data first.
The standard technique is to serialize a context with toJSON and print it:
name: debug-contexts
on: push
jobs:
dump:
runs-on: ubuntu-latest
steps:
- name: Dump the event payload
env:
EVENT_JSON: ${{ toJSON(github.event) }}
run: echo "$EVENT_JSON"The log then shows every property of github.event, so you can copy the exact path you need. Do the same for github, runner, job, steps, needs, or matrix when those are the contexts in question.
A short checklist helps when a value is not what you expect:
- Dump the context that should contain the value, and confirm the property name and nesting.
- Check that the context is available at that workflow key. A step
ifcannot readsecrets, for example. - Check the type. A comparison like
'1' == 1coerces both sides to numbers, but a JSON string output stays a string untilfromJSONconverts it. - Check the event. A
pull_requestpayload has nohead_commit, and apushpayload has nopull_request. - For more detail, re-run the job with debug logging enabled. Setting the repository variable
ACTIONS_STEP_DEBUGtotrueadds extra step logs.
Exercise
A step uses if: contains(github.event.pull_request.labels.*.name, 'urgent'), but it never runs even though the pull request has an "Urgent" label. What do you check first, and how?
Show answer
First confirm the workflow actually receives the label data. Add a step that sets env: EVENT_JSON: ${{ toJSON(github.event.pull_request.labels) }} and prints it. Check that the event is pull_request and that the labeled type is listed under types, because the default types do not re-run the workflow when a label is added later. Also check that the label names appear in the dump as expected.