DEV Community

Cover image for Preventing concurrent GitHub Actions
jldec
jldec

Posted on • Originally published at jldec.me

Preventing concurrent GitHub Actions

GitHub Actions Workflows

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.

Screenshot of Github Actions log showing failed git push

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.

Screenshot of GitHub Blog from April 19, 2021 announcing the new concurrency key in GitHub Actions

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.

Screenshot of yaml for GitHub Action with concurrency group

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

Thank you GitHub!

Discussion (0)