Sub topic 10
Matching text with contains, startsWith and endsWith
This chapter assumes you can read a workflow file (on, jobs, steps) and that you know how a step-level if works. The three functions here let a condition match part of a string instead of comparing the whole value.
The three functions
| Function | Returns true when | Example |
|---|---|---|
| `contains(search, item)` | `search` includes `item` | `contains(github.ref, 'release')` |
| `startsWith(text, prefix)` | `text` begins with `prefix` | `startsWith(github.ref, 'refs/tags/')` |
| `endsWith(text, suffix)` | `text` ends with `suffix` | `endsWith(github.ref, '-rc')` |
String matching in these functions ignores case. contains('Hello', 'hello') is true. This differs from == on strings, which also ignores case in GitHub Actions, so you rarely need to normalize case yourself.
contains also works on arrays. If search is an array, it returns true when one element equals item. This is useful for labels on a pull request.
Matching refs and tags
A ref such as refs/tags/v1.2.0 is a plain string. Use startsWith to ask what kind of ref triggered the run, and endsWith to inspect the tail.
name: Release
on:
push:
branches: [main]
tags: ['v*']
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build
run: echo "Building"
- name: Publish release
if: ${{ startsWith(github.ref, 'refs/tags/v') }}
run: echo "Publishing a release"
- name: Mark release candidate
if: ${{ startsWith(github.ref, 'refs/tags/') && endsWith(github.ref, '-rc') }}
run: echo "This is a release candidate"The ${{ }} wrapper is optional here, but it is required when the expression starts with !. Negating a match looks like this:
- name: Skip release tags
if: ${{ !startsWith(github.ref, 'refs/tags/') }}
run: echo "Not a tag build"Matching commit messages and labels
The head commit message is available on push events. Teams often use it to let authors opt out of a step.
name: Optional step
on: [push]
jobs:
docs:
runs-on: ubuntu-latest
steps:
- name: Build docs
if: ${{ !contains(github.event.head_commit.message, '[skip docs]') }}
run: echo "Building docs"For pull request labels, pass the array of label objects through toJSON or compare against a mapped value. A simple and common form uses the label name list:
name: Labeled PR
on:
pull_request:
types: [opened, labeled, synchronize]
jobs:
slow-tests:
if: contains(github.event.pull_request.labels.*.name, 'run-slow-tests')
runs-on: ubuntu-latest
steps:
- run: echo "Running slow tests"The .*.name syntax is an object filter. It collects the name of every label into an array, and contains then checks that array for an exact element.
Common mistakes
- Swapping the arguments.
containstakes the text first and the thing to find second.contains('main', github.ref)asks the wrong question. - Expecting wildcards. These functions match literal text.
startsWith(github.ref, 'refs/tags/v*')looks for a literal*. Use a trigger filter for glob patterns. - Matching on a missing value. On events that have no commit message, such as
pull_request,github.event.head_commit.messageis empty.!contains(...)on an empty value is true, so the step would run. Add an event check when that matters. - Partial matches on arrays. With an array,
containstests whole elements. A label namedrun-slow-tests-nightlydoes not matchrun-slow-tests.
Exercise
You want a step to run only when the pushed ref is a tag whose name ends in -beta. Write the if line.
Show answer
if: ${{ startsWith(github.ref, 'refs/tags/') && endsWith(github.ref, '-beta') }}. The startsWith check confirms the ref is a tag and not a branch that happens to end in -beta.
Exercise
A step uses if: contains(github.event.pull_request.labels.*.name, 'deploy'). A pull request has only the label deploy-later. Does the step run, and why?
Show answer
No. When contains receives an array, it compares whole elements, and deploy-later is not equal to deploy.