Sub topic 7
Built-in functions: contains, format, join, hashFiles, and others
This chapter assumes you can read a workflow file and know that ${{ }} marks an expression, as covered in the earlier chapters on expression syntax and contexts. Here you will learn the built-in functions you can call inside those expressions.
How functions work
A function takes arguments and returns a value. You call it inside ${{ }} or inside an if condition. Function names are case-insensitive. Arguments can be literals or context values.
| Function | Purpose |
|---|---|
| `contains(search, item)` | True if a string contains a substring, or an array contains an item |
| `startsWith(s, prefix)` / `endsWith(s, suffix)` | Prefix and suffix tests |
| `format(template, a, b)` | Fills `{0}`, `{1}` placeholders |
| `join(array, sep)` | Joins array items into one string |
| `toJSON(v)` / `fromJSON(s)` | Convert a value to JSON text, or parse JSON text |
| `hashFiles(pattern)` | Returns a hash of the matching files |
String comparisons in functions ignore case, so contains('Hello', 'hello') is true.
Strings and arrays
contains, startsWith, and endsWith return a boolean, so they fit naturally in if. format builds a string from a template. To write a literal brace, double it: {{ and }}.
join accepts an array, often produced by an object filter. The expression github.event.pull_request.labels.*.name collects every label name into an array, and join turns it into one string.
Hashing files for cache keys
hashFiles takes one or more glob patterns and returns a SHA-256 hash of all matching files in the workspace. If no file matches, it returns an empty string. A cache key built from a lockfile hash changes only when the lockfile changes.
name: functions
on: pull_request
jobs:
demo:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
- name: Label summary
env:
LABELS: ${{ join(github.event.pull_request.labels.*.name, ', ') }}
TITLE_LINE: ${{ format('Run {0} on {1}', github.run_number, runner.os) }}
run: |
echo "$TITLE_LINE"
echo "Labels: $LABELS"The checkout step comes first because hashFiles reads files from the workspace. Without it, the hash would be empty. The values reach the shell through env, which is the safe pattern covered later in the injection chapter.
Converting to and from JSON
toJSON turns a context into readable text, which helps with debugging. fromJSON parses a JSON string into an object, array, or typed value. It is how a job output, which is always a string, becomes a matrix list or a number. A later chapter uses it to build dynamic matrices.
Common errors
- Calling
hashFilesbefore checkout, which gives an empty hash and a cache key that never changes. - Passing a string to
joininstead of an array. A single string is returned unchanged. - Forgetting that
formatplaceholders start at{0}, not{1}. - Using a function name that does not exist. The workflow fails validation when it loads.
Exercise
A cache key is ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}. The job never restores a cache and the key always ends in npm-. What are two likely causes?
Show answer
The hashFiles call returned an empty string. Either the job has no checkout step before it, so the workspace has no files, or the glob matches no file, for example because the lockfile has a different name or path.