Skip to content
Latchkey

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.sh

Gotchas

  • 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

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