cancel-workflow-action

⏹️ GitHub Action to cancel previous running workflows on push

Github星跟踪图

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@0.9.0
        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/cancel-workflow-action@0.9.0
        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/cancel-workflow-action@0.9.0
      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@0.9.0
        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@0.9.0
        with:
          all_but_latest: true
          access_token: ${{ github.token }}

Contributing

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

主要指标

概览
名称与所有者styfle/cancel-workflow-action
主编程语言TypeScript
编程语言TypeScript (语言数: 2)
平台
许可证MIT License
所有者活动
创建于2020-02-02 00:01:51
推送于2024-04-29 03:01:14
最后一次提交
发布数18
最新版本名称0.12.1 (发布于 )
第一版名称0.1.1 (发布于 )
用户参与
星数1k
关注者数10
派生数122
提交数185
已启用问题?
问题数56
打开的问题数2
拉请求数130
打开的拉请求数2
关闭的拉请求数20
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?