DEV Community

We added one condition to our release notes step and every push to every branch emailed forty partners

Our integration platform has forty partners who receive an email whenever we tag a release, with the notes generated from merged pull requests.

The step lives in the same GitHub Actions workflow that builds and tests every push, and for a year it carried a single condition:

if: ${{ startsWith(github.ref, 'refs/tags/v') }}

The change

In October a colleague wanted the email to go out only for tags pushed by hand, not for tags created by a re-run. He extended the line to:

if: ${{ startsWith(github.ref, 'refs/tags/v') }} && ${{ github.event_name == 'push' }}

It looked right, it read right, and two of us approved it.

The next morning a partner asked why they had received twenty three release announcements since nine o'clock, for versions that did not exist, listing work that was still on feature branches.

Why it failed

GitHub evaluates an if field as an expression. When the value contains ${{ }} with other text around it, the runner does not treat the whole thing as one expression. It substitutes each ${{ }} block and the result is a piece of text, here something like false && true. Any non-empty string is truthy.

The condition could no longer be false, and every push of every branch ran the step, with the mail credentials it needed.

Nothing in the run looked odd. The step was green, because sending an email is easy. The only sign was in the log, where the condition was printed as evaluated and nobody reads that for a step that succeeds.

The fix

The condition now has no ${{ }} at all:

if: startsWith(github.ref, 'refs/tags/v') && github.event_name == 'push'

GitHub wraps an if in an expression for you, so the braces were never needed.

actionlint runs on every pull request that touches a workflow and fails on any condition with text outside its braces, which it reports as always true. It found two more in other repositories, one guarding a cache purge.

The mail step also moved into its own job, triggered only by tags, so its credentials are not available to a branch build at all. And the email job checks that the version it is announcing exists in our package registry before it sends anything.

We wrote to the forty partners the same afternoon. The line had two halves and each was correct. Together they were a sentence, and a sentence is always true. - Sergey Shinder

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.