How to autocancel GitHub Actions workflows

We found a way to autocancel redundant workflows. Hint: we use another action!

Written in Development by Marc Riera — March 03, 2020

GitHub announced some months ago GitHub Actions, a system that allows you to run arbitrary code based on some given events happening around a given repository. People flocked to it, hoping it would allow them to run their test suites. This is particularly true for open-source projects, since GitHub gives out an unlimited amount of minutes to run actions for these kind of repos.

At Codegram we wanted to try it for some of our repos, but found some little issues that kind of made switching difficult. For example, GitHub Actions does not support autocanceling previous workflows.

Imagine this scenario:

  1. I commit to a branch and push it to my repo
  2. A CI build is triggered
  3. Before the CI build is finished, I push again
  4. Another CI build is triggered

What I'd like to happen is that the first CI build is stopped, since it's no longer necessary: it's become redundant, now that we have new changes.

Some CI services (CircleCI, for example) support autocancelling built on these kind of scenarios. Autocancelling is useful because it frees resources so that you can run newer, more up-to-date test suites, and it helps you save money (in case you're paying for those resources, of course).

Enter actions!

GitHub Actions has a concept that is a game changer, in my opinion: community actions. This means you can reuse actions made by other people, the whole community can benefit from them. And well, it took a while, but finally someone made it: an action to autocancel redundant builds. Here's how to use it:

on:
  push: []

jobs:
  my-job:
    runs-on: ubuntu-latest
    steps:
    - uses: rokroskar/workflow-run-cleanup-action@v0.2.2
      env:
        GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
    - uses: actions/checkout@v2.0.0
    ...

By running the action before checking out the repo we save a couple of seconds (it ain't much, but it's honest work) so we can cancel redundant workflows earlier. Beware, though: as its configured, it will run on any branch. We can limit it, though, to avoid autocancelling workflows on our master branch.

on:
  push: []

jobs:
  my-job:
    runs-on: ubuntu-latest
    steps:
    - uses: rokroskar/workflow-run-cleanup-action@v0.2.2
      env:
        GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
      if: "github.ref != 'refs/heads/master'"
    - uses: actions/checkout@v2.0.0
    ...

Note the if: part. I won't go into too much detail here, but we can conditionally run some steps based on the if: key. In this case, we're telling GitHub Actions to ignore this step if the current branch is master. You could add other branches, depending on your own workflow, but I hope you get the idea. Don't forget to check the repo for more info.

So, in any case, thanks Rok Roškar for your work on this action!

Photo by Rosie Steggles on Unsplash

View all posts tagged as