dan1st
dan1st

Reputation: 16447

Stop already running workflow/job in GitHub Actions

I have setup CI using GitHub Actions. One of my workflows is triggered by push events but it (or a job of this workflow) should not run on multiple instances the same time.

Is there a possible to create a job that stops the other workflow(or job or not allowing the workflow/job run in parallel)?

I would prefer cancelling the "old" job instead of the need for the "new" job to finish until the "old" job finished.

Ideally, I also want to run a job at the end that finishes the "old" job.(~saves the unfinished status). This can(but it doesn't have to) run parallel with the "new" job.

[Note] I want to cancel a job in another workflow, or the other workflow, not another job in the same workflow.

Upvotes: 8

Views: 10690

Answers (3)

Dharun
Dharun

Reputation: 21

To stop all the running workflow just add this job

jobs:
  cancel:
    name: 'Cancel Other Workflow'
    runs-on: ubuntu-latest
    steps:
      - uses: styfle/[email protected]
        with:
          access_token: ${{ github.token }}

You can also cancel specfic workflow by adding

workflow_id:<workflow id or workflow name>

in with: section.

for more information about this action check https://github.com/styfle/cancel-workflow-action#readme

Upvotes: 2

A.Chan
A.Chan

Reputation: 770

Now you can use offical concurrency support for Github Actions(but still in beta)

https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#concurrency

both workflow and job supported!

concurrency: 
  group: ${{ github.head_ref }}
  cancel-in-progress: true

Upvotes: 13

Polor Beer
Polor Beer

Reputation: 2012

Since Github has opened the new workflows API, I created this small script to manually cancel all unfinished jobs in one command:

Example:

  # Set up
  export GITHUB_TOKEN=394ba3b48494ab8f930fbc93
  export GITHUB_REPOSITORY=apache/incubator-superset

  # Cancel previous jobs for a PR
  ./cancel_github_workflows.py 1042

  # Cancel previous jobs for a branch
  ./cancel_github_workflows.py my-branch

  # Cancel all jobs including the last ones, this is useful
  # when you have closed a PR or deleted a branch and want
  # to cancel all its jobs.
  ./cancel_github_workflows.py 1024 --include-last

Upvotes: 2

Related Questions