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)andendsWith(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
How to Run a Step Only for a Specific Label in GitHub ActionsRun a GitHub Actions step only when a pull request carries a given label by testing contains on the labels ar…
How to Compare Two Strings in an if Condition in GitHub ActionsCompare strings in a GitHub Actions if using == and !=, understanding case sensitivity and how the expression…