How to programmatically end or cancel the workflow/build?

Hello,

Yes it’s possible to conditionally cancel workflows.

      - checkout
      - run: | 
          commit_message=$(git log -1 HEAD --pretty=format:%s)
          if [[ $commit_message == *"custom-identifier"* ]]; then
          curl --request POST \
            --url https://circleci.com/api/v2/workflow/$CIRCLE_WORKFLOW_ID/cancel \
            --header "Circle-Token: ${CIRCLE_TOKEN}"
          fi

Basically after you run a checkout this pulls the latest commit message into a variable. Then it check to see if your custom-identifier exists anywhere in the commit message. If so, it makes a request to our API to cancel the workflow. It uses the CIRCLE_WORKFLOW_ID envar that we automatically include in the job, and a CircleCI user API token (CIRCLE_TOKEN) that you would need to manually add to the project in your project settings page.

I’m not sure how robust this is for your development workflow, as it would only check the HEAD commit for the string. But at the very least it’s a basic example of how to conditionally cancel a workflow.

1 Like