Giter Site home page Giter Site logo

mrbrannan / cancel-workflow-action Goto Github PK

View Code? Open in Web Editor NEW

This project forked from styfle/cancel-workflow-action

0.0 1.0 0.0 385 KB

⏹️ GitHub Action to cancel previous running workflows on push

Home Page: https://github.com/marketplace/actions/cancel-workflow-action

License: MIT License

TypeScript 98.87% Shell 1.13%

cancel-workflow-action's Introduction

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/[email protected]
        with:
          access_token: ${{ github.token }}
      #- name: Run Tests
      #  uses: actions/setup-node@v1
      #  run: node test.js
      # ... etc

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/[email protected]
        with:
          workflow_id: 479426
          access_token: ${{ github.token }}
  • 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

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/[email protected]
      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/[email protected]
        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/[email protected]
        with:
          all_but_latest: true
          access_token: ${{ github.token }}

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/[email protected]
        with:
          access_token: ${{ github.token }}

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

Advanced: Disqualifying Jobs

In the case where you may want for an in_progress job to stop the cancellation of a workflow you may pass a JSON Array as input to disqualifying_jobs. If a job is named in the array and its status is in_progress the workflow will be removed from the list of jobs to cancel and skipped.

This is useful for operations such as static site deployment where two jobs have the ability to read/write files simultaneously which could cause downtime or runtime errors.

name: Cancel
on: [push]
jobs:
  cancel:
    name: 'Cancel Previous Runs'
    runs-on: ubuntu-latest
    timeout-minutes: 3
    steps:
      - uses: styfle/[email protected]
        with:
          access_token: ${{ github.token }}
          disqualifying_jobs: '["deploy"]'

Contributing

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

cancel-workflow-action's People

Contributors

styfle avatar dependabot[bot] avatar mrbrannan avatar pllim avatar ericmatte avatar spaceface777 avatar palasha avatar thomwiggers avatar warashi avatar mikehardy avatar michaeldeboey avatar martinnowak avatar mfn avatar jaredh159 avatar henryiii avatar ahanriat avatar alexesprit avatar adrienbernede avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.