Github Actions workflow for cancelling jobs

cancel-gh-workflow

Cancel the selected workflow actions for a faster feedback.

https://github.com/marketplace/actions/cancel-the-selected-workflow-actions-for-a-faster-feedback

Use-case:

For example:

Your github repo have two github actions workflow files node-test.yml and django-test.yml run simultaneously triggered on pull_request

If the node-test.yml workflow is failed, you don’t want to continue the workflow run with django-test.yml

Usage

node-test.yml

Here if the job failed for node-test.yml, the action will cancel all the jobs running by the django-test.yml workflow


name: Node CI Test

on:
  pull_request:
    types:
      - opened
      - reopened
      - synchronize
      - ready_for_review
jobs:
  job-1:
    name: Run node job1
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - name: Extract branch name
      id: extract_branch
      run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"

    - run: exit 1

    - uses: vishnudxb/cancel-gh-workflow@v1.0
      if: failure()
      with:
        repo: octocat/hello-world
        branch_name: ${{ steps.extract_branch.outputs.branch }}
        workflow_file_name: django-test.yml
        access_token: ${{ github.token }}

django-test.yml

Here if the job failed for django-test.yml, the action will cancel all the jobs running by the node-test.yml workflow


name: Django CI Test

on:
  pull_request:
    types:
      - opened
      - reopened
      - synchronize
      - ready_for_review

jobs:
  job-1:
    name: Run django job1
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - name: Extract branch name
      id: extract_branch
      run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"

    - run: exit 1

    - uses: vishnudxb/cancel-gh-workflow@v1.0
      if: failure()
      with:
        repo: octocat/hello-world
        branch_name: ${{ steps.extract_branch.outputs.branch }}
        workflow_file_name: node-test.yml
        access_token: ${{ github.token }}

cancel-duplicate-workflow

Cancel a duplicate workflow https://github.com/marketplace/actions/cancel-workflow-action-for-duplicate-jobs

Use-case:

If you are using a single environment for your staging and testing and sometimes you may need to avoid a duplicate deployment of an endpoint, then you can use this action to cancel either the staging or testing build.

Usage

Basic:

steps:
- uses: vishnudxb/cancel-duplicate-workflow@v1.4
  with:
    repo: octocat/hello-world
    workflow_id: ${{ github.run_id }}
    access_token: ${{ github.token }}
    branch_name: staging  # The branch which you want to check if any jobs are running!

Sample usage in github action job:

For eg: your test.yml is triggered from your testing branch. The below action check if any deployment jobs are running in your staging branch. If do, cancel the test build.

jobs:
  deploy:
    name: Run deployment for testing
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v2
      with:
        token: ${{ secrets.GITHUB_TOKEN }}

    - uses: vishnudxb/cancel-duplicate-workflow@v1.4
      with:
        repo: octocat/hello-world
        workflow_id: ${{ github.run_id }}
        access_token: ${{ secrets.GITHUB_TOKEN }}
        branch_name: staging

    - run: echo "This is testing deployment"

cancel-workflow

Cancel a workflow run if a job failed in Github Actions https://github.com/marketplace/actions/cancel-workflow-action-for-failed-jobs

Usage

See action.yml

Basic:

steps:
- uses: vishnudxb/cancel-workflow@v1.2
  with:
    repo: octocat/hello-world
    workflow_id: ${{ github.run_id }}
    access_token: ${{ github.token }}

Sample usage in github action job:

jobs:
  job-1:
    name: Run job1
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v2
      with:
        token: ${{ secrets.GITHUB_TOKEN }}

    - run: exit 1

    - uses: vishnudxb/cancel-workflow@v1.2
      if: failure()
      with:
        repo: octocat/hello-world
        workflow_id: ${{ github.run_id }}
        access_token: ${{ github.token }}

  job-2:
    name: Run job2
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v2
      with:
        token: ${{ secrets.GITHUB_TOKEN }}

    - run: echo "running job2"

  job-n:
    name: Run job `n`

If the job1 fails, we don’t want to continue with the other jobs and cancel the workflow.