Skip to main content
← Back to workflows

How to Use the Cancel Workflow Action in GitHub Actions

styfle-cancel-workflow-action -
GitHub Action
0.12.1
932
Contributors
Contributor - styfleContributor - MichaelDeBoey
Categories
CICUBE ANALYTICS INSIGHTS
Engineering Velocity: 25% Team Time Lost to CI Issues
View Platform →
3.5h
Time Saved/Dev/Week
40%
Faster Releases
Click for next insight
Usage
name: 'Usage of cancel-workflow-action GitHub Action'
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action

cancel-workflow-action logo

Cancel Workflow Action

⏹️ GitHub Action to cancel previous running workflows on push


What is Cancel Workflow Action?

The Cancel Workflow Action is a GitHub Action that cancels any previous runs that are not completed for a given workflow. This includes runs with a status of queued or in_progress.

tip

You probably don't need to install this custom action.

Instead, use the native concurrency property to cancel workflows, for example:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

Read GitHub's official documentation to learn more.

Advanced: Canceling Other Workflows Using GitHub Actions

In some cases, you may wish to avoid modifying all your workflows and instead create a new workflow that cancels your other workflows. This can be useful when you have a problem with workflows getting queued.

name: Cancel
on: [push]
jobs:
cancel:
name: 'Cancel Previous Runs'
runs-on: ubuntu-latest
timeout-minutes: 3
steps:
- uses: styfle/cancel-workflow-action
with:
workflow_id: 479426

Notes:

  • workflow_id: Can be a Workflow ID (number) or Workflow File Name (string).
  • workflow_id: Accepts a comma-separated list if you need to cancel multiple workflows.
  • workflow_id: Accepts the value all, which will cancel all the workflows running in the branch.

Advanced: Pull Requests from Forks

The default GitHub token access is unable to cancel workflows for pull_request events when a pull request is opened from a fork. Therefore, a special setup using workflow_run, which also works for push, is needed. Create a .github/workflows/cancel.yml with the following content, replacing "CI" with the workflow name that contains the pull_request workflow.

name: Cancel
on:
workflow_run:
workflows: ["CI"]
types:
- requested
jobs:
cancel:
runs-on: ubuntu-latest
steps:
- uses: styfle/cancel-workflow-action
with:
workflow_id: ${{ github.event.workflow.id }}

Advanced: Ignore SHA

In some cases, you may wish to cancel workflows when you close a Pull Request. Because this is not a push event, the SHA will be the same, so you must use the ignore_sha option.

on:
pull_request:
types: [closed]
jobs:
cleanup:
name: 'Cleanup After PR Closed'
runs-on: ubuntu-latest
timeout-minutes: 3
steps:
- name: Cancel build runs
uses: styfle/cancel-workflow-action
with:
ignore_sha: true
workflow_id: 479426