Next: Getting started with Goroutines and channels
June 13, 2021

Preventing concurrent GitHub Actions

What happens when you trigger a GitHub Actions workflow which is already running? Workflows which depend on being run one-at-a-time might fail.

I recently encountered this with a workflow for publishing a static website. This workflow generates HTML files which are pushed to another git repo for publishing by GitHub Pages.

When two workflows try to push to a checked-out repo at the same time, one will fail because it is missing the last commit from the other.

This is just one example where concurrent workflows are problematic. Workflows which automate deployments have the same problem.

A number of 3rd party solutions exist, but these introduce additional waiting costs or other issues. For one of my projects, I host a lock-service, just to force concurrent workflows to exit quietly, and then auto-trigger re-runs.

Finally, on April 19, 2021,
this appeared in the GitHub Blog.

In the case of using actions to generate a GitHub Pages website, the feature works exactly as required.

  • The first workflow will run to completion.
  • Subsequent concurrent workflows will either be delayed or cancelled.
  • In the end only the first and last of the overlapping workflows will be run.

And all you need is 2 lines of yaml. This is from the workflow which generates jldec.uk.

The group can be any string - workflows in the same group are effectively serialized.

Thank you GitHub!

(c) Jürgen Leschner