Published on

Automatically Cancel Previous Jobs in GitHub Actions

Authors

Sometimes we might be making so many changes and trigger multiple CI/CD jobs in GitHub Actions and end up wasting so many resources and money.

Thankfully, GitHub Actions has concurrency feature using which we can make sure only one job can run at the same time.

You can do that by adding concurrency configuration in your Github action workflow.

And the best part we can use expressions to configure it based on our use case.

Cancel any jobs based on the PR Number

name: test
on: # ...
concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true
jobs: # ...

Note: github.ref is used when the trigger event is not a PR but a push. It'll use the branch or tag name

Reference

Happy efficient workflows!