Cancel Action run, when Pull Request changes

Viewed 972

For my project, I have a pretty long github action that is triggered when a new pull request is created. These are the triggers I use:

on:
  pull_request:
    types: [opened, ready_for_review, labeled]

This action is defined as a "status check" requirement for my pull request, to make sure that the action is running fine before allowing someone to merge his PR.

But, my PRs also require to be up to date with develop before being allowed to merge anything.

So the scenario I encounter frequently is this:

  • Create a new Pull Request
  • The Github Action starts, and my PR's check status go to "in progress"
  • I realise that my branch is not up to date with develop, so I update it.
  • The PR's check status go to "Expected", since my current run was started on a previous version of the branch.

My issue with that scenario, is that, even though my github action run is now completely invalid/obsolete (because the content of the branch changed), the action itself is still running. I need to manually go to the actions tab and cancel this run.

Is there a way to automatically cancel this run, if the content of the branch or the PR changed ?

1 Answers

As CI job ares executed in a different github runners, there is no direct way to automatically cancel previous CI jobs natively with Github Actions.

However, there as some workarounds to help you resolve your issue:

1 - use the Github API to cancel workflow runs directly (which might not be trivial to automate in your specific case).

2 - use a cancel-workflow-action, that will cancel any previous runs that are not completed for a given workflow.

3 - use a skip-duplicate-actions, to skip duplicate workflow-runs after merges, pull requests or similar.

4 - use the native concurrency field to ensure that only a single job or workflow using the same concurrency group will run at a time.

Related