Cancel current running workflow from Github Actions

Viewed 19

I want to be able, based on a condition to cancel my current run of a workflow.

I tried to use this action like this

      - name: cancel workflow if none of files we look for changes has not changed
        if: steps.changed-files.outputs.any_changed != 'true'
        uses: styfle/cancel-workflow-action@0.10.0
        with:
          workflow_id: ${{ github.workflow_id }}
          access_token: ${{ github.token }}

but as it turns out from the logs:

Found token: yes
Found workflow_id: ["12345567"]
Found 3 runs total.
Found 0 runs to cancel.

it only cancels other concurrently running workflows.

Is there an easy way around this or should I resort to GH API?

edit: I have tried this from within the step that is supposed to cancel the rest of the workflow

 gh api --method POST -H "Accept: application/vnd.github+json" https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.run_id }}/cancel

However it just returns a {} and the execution continues...

1 Answers

This is one way to go about it

      - name: cancel run if none of files we look for changes has changed
        if: steps.changed-files.outputs.any_changed != 'true'
        uses: andymckay/cancel-action@0.2

      # needed since the run is not cancelled immediately
      - name: wait for run cancellation
        if: steps.changed-files.outputs.any_changed != 'true'
        shell: bash
        run: while true; do echo "Waiting for job to be cancelled"; sleep 5; done
Related