Skip to content
Latchkey

How to Match Strings With contains, startsWith, and endsWith in GitHub Actions

contains, startsWith, and endsWith are the built-in string matchers for expressions.

These three functions return booleans you can drop straight into an if. contains also works on arrays, which is handy for label checks.

Steps

  • Use contains(haystack, needle) for a substring or array-membership test.
  • Use startsWith(str, prefix) and endsWith(str, suffix) for anchored matches.
  • All three are case-sensitive.

Workflow

.github/workflows/ci.yml
steps:
  - name: Only for docs branches
    if: startsWith(github.ref_name, 'docs/')
    run: echo "Docs branch"
  - name: Skip WIP commits
    if: "!contains(github.event.head_commit.message, 'WIP')"
    run: ./deploy.sh
  - name: YAML files only
    if: endsWith(github.event.head_commit.message, '.yml')
    run: echo "message ends with .yml"

Gotchas

  • A leading ! makes YAML treat the value as special, so quote the whole expression.
  • Normalize case (e.g. compare lowercase) when input casing is unpredictable.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →