Stop GitHub Jobs in Progress if Another Failed (stop on fail)

Viewed 2262

TL; DR: Running jobs a,b in parallel. If a fails, stop the execution of b, while it's still running.

My company uses GitHub Actions to deploy our code.

The first step in our deployment is building dockers and pushing them to DockerHub.

We wrote a test for our code, which we want to run in parallel with building the dockers.

Both of these are separate jobs, and we have a few more jobs depending on the success of the first two.

Right now, if the test job fails, the other job continues to run, but obviously, the next one won't run, because the test job failed.

What I would like to do is cancel the docker building job while it's running, if the test failed.

Is that possible? After searching the web, StackOverflow and the GitHub Actions page, I haven't found a way to do that.

Thanks!

2 Answers

You can use the Cancel this build action.

The basic idea is to add it as a final step in each of your jobs that you want to cause a short-circuit in case of failure:

jobs
  job_a:
    steps:
      - run: |
          echo 'I am failing'
          exit 1
      - name: Cancelling parallel jobs
        if: failure()
        uses: andymckay/cancel-action@0.2
  job_b:
    steps:
      - run: echo 'long task'

This will basically cancel job_b or any other in the same workflow whenever job_a fails.

Related