How to Negate a Condition in an if in GitHub Actions
Use ! to negate, and quote the whole if value when it starts with ! so YAML parses it.
The ! operator inverts a boolean. Because YAML treats a leading ! as a tag, wrap the entire expression in quotes when it begins with !.
Steps
- Prefix the expression with
!to invert it. - Quote the whole
if:value if it starts with!. - Combine with
contains,startsWith, or comparisons as needed.
Workflow
.github/workflows/ci.yml
steps:
- name: Deploy unless skipped
if: "!contains(github.event.head_commit.message, '[skip-deploy]')"
run: ./deploy.sh
- name: Non-fork only
if: "!github.event.pull_request.head.repo.fork"
run: ./privileged.shGotchas
- An unquoted leading
!triggers a YAML error about an unexpected tag. - Negating a non-boolean uses truthiness: empty string is false, non-empty is true.
Related guides
How to Combine Conditions With && and || in an if in GitHub ActionsCombine multiple GitHub Actions if conditions with && and ||, and preserve status behavior by leading with al…
How to Avoid the Empty-String vs Boolean Gotcha in an if in GitHub ActionsAvoid the common GitHub Actions if pitfall where the string false is truthy, by comparing explicitly against…