• Stars
    star
    863
  • Rank 52,844 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

⏹️ GitHub Action to cancel previous running workflows on push

Warning
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.

Cancel Workflow Action

This is a GitHub Action that will cancel any previous runs that are not completed for a given workflow.

This includes runs with a status of queued or in_progress.

How does it work?

When you git push, this GitHub Action will capture the current Branch and SHA. It will query GitHub's API to find previous workflow runs that match the Branch but do not match the SHA. These in-progress runs will be canceled leaving only this run, or the latest run.

Read more about the Workflow Runs API.

Usage

Typically, you will want to add this action as the first step in a workflow so it can cancel itself on the next push.

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Cancel Previous Runs
        uses: styfle/cancel-workflow-action
      #- name: Run Tests
      #  uses: actions/setup-node
      #  run: node test.js
      # ... etc

Versioning

It is recommended to pin a specific version of this action so you don't automatically upgrade to the latest which could introduce breaking changes.

uses: styfle/[email protected]

Notice there is no v prefix, just the @major.minor.patch version.

Visit Releases to find the latest version at the top of the page.

Warning

You might run into "the `uses' attribute must be a path, a Docker image, or owner/repo@ref" error if you don't specify a version.

Advanced: Canceling Other Workflows

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.

  • Visit https://api.github.com/repos/:org/:repo/actions/workflows to find the Workflow ID(s) you wish to automaticaly cancel.
  • Add a new file .github/workflows/cancel.yml with the following:
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
  • Note: workflow_id can be a Workflow ID (number) or Workflow File Name (string)
  • Note: workflow_id also accepts a comma separated list if you need to cancel multiple workflows
  • Note: 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 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 instead and replace "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

Advanced: All But Latest

Because this action can only cancel workflows if it is actually being run, it only helps if the pipeline isn't saturated and there are still runners available to schedule the workflow.

By default, this action does not cancel any workflows created after itself. The all_but_latest flags allows the action to cancel itself and all later-scheduled workflows, leaving only the latest.

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

Advanced: Token Permissions

No change to permissions is required by default. The instructions below are for improved control over of those permissions.

By default, GitHub creates the GITHUB_TOKEN for Actions with some read/write permissions. It may be a good practice to switch to read-only permissions by default. Visit the dedicated documentation page for details.

Permissions can be set for all Jobs in a Workflow or a specific Job, see the reference manual page. cancel-workflow-action only requires write access to the actions scope, so it is enough to have:

jobs:
  test:
    runs-on: ubuntu-latest
    permissions:
      actions: write
    steps:
      - name: Cancel Previous Runs
        uses: styfle/cancel-workflow-action
        with:
          access_token: ${{ github.token }}

Note : This is typical when global access is set to be restrictive. Only this job will elevate those permissions.

Contributing

  • Clone this repo
  • Run yarn install
  • Edit ./src/index.ts
  • Run yarn build
  • Commit changes including ./dist/index.js bundle

More Repositories

1

awesome-online-ide

🌩️ A list of awesome online development environments
2,919
star
2

packagephobia

⚖️ Find the cost of adding a new dependency to your project
TypeScript
2,115
star
3

awesome-desktop-js

🖥️ A list of awesome packages and frameworks for implementing javascript applications on the desktop
740
star
4

breaking-changes-web

💢 A list of breaking changes to the web platform
351
star
5

react-server-example-tsx

⚛️ Boilerplate for isomorphic web app with React server-side rendering in TypeScript
TypeScript
277
star
6

copee

📄 Copy text from browser to clipboard...natively! < 1kB
TypeScript
138
star
7

links-awakening

🔗 Recursively check a website for broken links
TypeScript
130
star
8

magnemite

⏺️ Capture repro steps with this screen recorder for websites, powered by Electron
TypeScript
57
star
9

screenshot-v2

New here and want something similar? See https://github.com/vercel/og-image. Historian and want to see the original PR? See https://github.com/zeit/now-examples/pull/207
JavaScript
30
star
10

styfle.dev

👨‍💻 The source code for my website, built with Next.js and hosted on Vercel
TypeScript
23
star
11

geoslack

📍 Geolocate your team in Slack
TypeScript
18
star
12

doorbell

🛎️ Virtual doorbell to notify Amazon Alexa devices
HTML
15
star
13

tls-check

✅ Check the TLS protocol support of one or more web servers
JavaScript
13
star
14

typed-tmpl

🛡A typed, template module using ES6 Tagged Template Strings with TypeScript
JavaScript
6
star
15

The-Harvest-Club

🍊A student-designed Contact Management System for connecting volunteers and growers
PHP
6
star
16

exeggcute

🥚 A node.js module to make executing shell cmds a breeze!
JavaScript
5
star
17

rediscovering-neverland

🧚‍♂️Slides from my Tech Talk given on January 23, 2020
HTML
5
star
18

Basic-Wars

🎖️A turn-based strategy game written in pure Java
Java
4
star
19

dotfiles

⚫ My .bashrc .vimrc etc
Vim Script
3
star
20

tsc-example

Different ways to compile typescript
JavaScript
3
star
21

ncc-bug-socketio

A bug with ncc
JavaScript
2
star
22

ncc-bug-stack

JavaScript
2
star
23

device-width-detection

📱Generate dynamic SVG based on device srcset
JavaScript
2
star
24

ncc-bug-sourcemap

A bug with ncc
TypeScript
2
star
25

dotnet-api-example

Example of what a simple .NET API might look like
C#
1
star
26

ncc-bug-fluent-ffmpeg

JavaScript
1
star
27

micro-ts-example

Example of using micro with TypeScript
JavaScript
1
star
28

ncc-bug-npm

A bug when npm is a dependency
JavaScript
1
star
29

alexa-wunderground

🗣️ WIP - An Amazon Alexa skill for Weather Underground
JavaScript
1
star
30

xtend-es6

extend like a modern boss with Object.assign()
JavaScript
1
star
31

aws-xray-bug

aws xray issue #60
JavaScript
1
star
32

mma-api

👊 api for mma dataset
JavaScript
1
star
33

node-docker-echo

Example node.js echo environment variables in docker
JavaScript
1
star
34

node-http-log

❓ log http request header & body to a file
JavaScript
1
star
35

bug-term-size-tput

Bug in `term-size`
JavaScript
1
star