Home

Awesome

dbt Cloud Cancel Running CI Job Runs Action

This action checks if a dbt Cloud CI job (based on job ID) is running after a new or updated PR commit is made. If there is a job running or there are job runs that are queued - this action will cancel these running and queued runs since they are likely stale as a new commit to the PR has been made. This allows to always have the newest code commit running in the CI job without having to wait for the stale job runs to finish.

Some scenarios where this is useful:

To view a Loom video of this action being used, click here


Inputs

Required Inputs

Optional Inputs

It's recommend to pass sensitive variables as GitHub secrets. Example article on how to use Github Action secrets


Outputs


Creating a workflow

A workflow using the only_cancel_run_if_commit_is_using_pr_branch flag

# This is a basic workflow to show using this action

# name of the workflow
name: Cancel running slim ci job runs if new commit is made to the same pull request triggering a new run

# Controls when the workflow will run
on:
  pull_request:
    branches: [ "main" ]

  # Allows you to run this workflow manually from the Actions tab if needed
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:

  # This workflow contains a single job called "cancel_running_slim_ci_jobs"
  cancel_running_slim_ci_jobs:
  
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:

      # running the step to cancel another other CI job runs that are running except the latest, only for the given PR
      - name: Cancel other CI runs that are running for the given PR except the latest run
        id: cancel_stale_ci_runs
        uses: Stevedow99/dbt-cloud-cancel-running-ci-job-action@v1.3
        with:
          dbt_cloud_token: ${{ secrets.DBT_CLOUD_TOKEN }}
          dbt_cloud_account_id: 12345
          dbt_cloud_job_id: 130247
          only_cancel_run_if_commit_is_using_pr_branch: true
          github_pr_number: ${{ github.event.number }}
          max_runs: "50"

A workflow not using the only_cancel_run_if_commit_is_using_pr_branch flag

# This is a basic workflow to show using this action

# name of the workflow
name: Cancel all other running slim ci job runs if new commit is made

# Controls when the workflow will run
on:
  pull_request:
    branches: [ "main" ]

  # Allows you to run this workflow manually from the Actions tab if needed
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:

  # This workflow contains a single job called "cancel_running_slim_ci_jobs"
  cancel_running_slim_ci_jobs:
  
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:

      # running the step to cancel another other CI job runs that are running except the latest
      - name: Cancel all other ci runs that are running except the latest run
        id: cancel_stale_ci_runs
        uses: Stevedow99/dbt-cloud-cancel-running-ci-job-action@v1.3
        with:
          dbt_cloud_token: ${{ secrets.DBT_CLOUD_TOKEN }}
          dbt_cloud_account_id: 12345
          dbt_cloud_job_id: 130247

Examples of using this Github action with other workflows

<br>

Example using workflow to log the cancelled job run IDs:



# This is a basic workflow to show using this action

# name of the workflow
name: Cancel running slim ci job runs if new commit is made to the same pull request triggering a new run

# Controls when the workflow will run
on:
  pull_request:
    branches: [ "main" ]

  # Allows you to run this workflow manually from the Actions tab if needed
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:

  # This workflow contains a single job called "cancel_running_slim_ci_jobs"
  cancel_running_slim_ci_jobs:
  
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:

      # running the step to cancel another other CI job runs that are running except the latest, only for the given PR
      - name: Cancel other CI runs that are running for the given PR except the latest run
        id: cancel_stale_ci_runs
        uses: Stevedow99/dbt-cloud-cancel-running-ci-job-action@v1.3
        with:
          dbt_cloud_token: ${{ secrets.DBT_CLOUD_TOKEN }}
          dbt_cloud_account_id: 12345
          dbt_cloud_job_id: 130247
          only_cancel_run_if_commit_is_using_pr_branch: true
          github_pr_number: ${{ github.event.number }}
          max_runs: "50"
          
      # logging if there was a job run(s) cancelled or not
      - name: Logging if there was a CI run that was cancelled
        if:  steps.cancel_stale_ci_runs.outputs.cancelled_jobs_flag == 'True'
        run: |
          echo "A dbt Cloud CI job run(s) was cancelled due to a new CI run being triggered"
          echo "The following job runs were cancelled ${{ steps.cancel_stale_ci_runs.outputs.cancelled_dbt_cloud_job_runs }}"
      
<br>

Example using workflow to cancel job runs and then post information about the cancelled job runs in a PR comment

This workflow will produce a PR comment that looks like this when job runs are cancelled:

image



# This is a basic workflow to show using this action

# name of the workflow
name: Cancel running slim ci job runs if new commit is made to the same pull request triggering a new run

# Controls when the workflow will run
on:
  pull_request:
    branches: [ "main" ]

  # Allows you to run this workflow manually from the Actions tab if needed
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:

  # This workflow contains a single job called "cancel_running_slim_ci_jobs"
  cancel_running_slim_ci_jobs:
  
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:

      # running the step to cancel another other CI job runs that are running except the latest, only for the given PR
      - name: Cancel other CI runs that are running for the given PR except the latest run
        id: cancel_stale_ci_runs
        uses: Stevedow99/dbt-cloud-cancel-running-ci-job-action@v1.3
        with:
          dbt_cloud_token: ${{ secrets.DBT_CLOUD_TOKEN }}
          dbt_cloud_account_id: 12345
          dbt_cloud_job_id: 130247
          only_cancel_run_if_commit_is_using_pr_branch: true
          github_pr_number: ${{ github.event.number }}
          max_runs: "50"
          
      # logging if there was a job run(s) cancelled or not
      - name: Logging if there was a CI run that was cancelled
        if:  steps.cancel_stale_ci_runs.outputs.cancelled_jobs_flag == 'True'
        run: |
          echo "A dbt Cloud CI job run(s) was cancelled due to a new CI run being triggered"
          echo "The following job runs were cancelled ${{ steps.cancel_stale_ci_runs.outputs.cancelled_dbt_cloud_job_runs }}"
          
      # if there a job run(s) was cancelled, we grab the outputted markdown and put it into a PR comment for logging
      - name: PR comment with Job Run Cancelation Information
        uses: mshick/add-pr-comment@v1
        if:  steps.cancel_stale_ci_runs.outputs.cancelled_jobs_flag == 'True'
        with:
          message: |
            ${{ steps.cancel_stale_ci_runs.outputs.cancelled_dbt_cloud_job_runs_markdown }}
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          repo-token-user-login: 'github-actions[bot]'
          allow-repeats: false # This is the default