Does circleci step halt works with version 2.1?

Hi everyone,

Basically I’m trying to use circleci step halt to stop a workflow at a given point, but I don’t see it doing anything or even erroring out, maybe I’m not using it correctly or it is not supported in 2.1? Is there any alternative way of accomplishing something like this without failing the job?

This is the config I’m testing with:

version: 2.1

orbs:
  hello: circleci/hello-build@0.0.5
  build-tools: circleci/build-tools@2.6.3

jobs:
  build:
    docker:
      - image: circleci/node:12
    steps:
      - checkout
      - setup_remote_docker
      - build-tools/install-ci-tools
      - run: |
            if [[ $(echo "$CIRCLE_PULL_REQUEST $CIRCLE_PULL_REQUESTS" | grep -c "pull") -gt 0 ]]; then
              echo "a PR staph!"
              circleci step halt
              circleci-agent step halt
              exit 0
            else
              echo "not a PR"
            fi

workflows:
  "Hello Workflow":
    jobs:
      - hello/hello-build:
          requires:
            - build
          filters:
            branches:
              only:
                - testing
                - /^(?!pull\/).*$/
            tags:
              only:
                - /^pull\/.*$/
      - build:
          filters:
            branches:
              only:
                - testing
                - /^(?!pull\/).*$/
            tags:
              only:
                - /^pull\/.*$/

Thanks.

1 Like

You should be able to stop a job via circleci-agent step halt with 2.1

https://circleci.com/docs/2.0/configuration-reference/#ending-a-job-from-within-a-step

Looking at your config, it looks like it is the last step in the build job. Therefore it would have the same effect as exit 0. The halt call will just prevent any later steps from running.

1 Like

So it does execute in the correct order, I added the requires for that, but still the build continues as normal, see the image below:

Thanks!

Ah, I see where the confusion might be.

circleci-agent step halt will return a green build for a single job while skipping all the remaining steps in that one specific job. The workflow will still continue to execute additional jobs. A workflow is a collection of jobs and a job is a collection of steps.

circleci-agent step halt does not have any effect on the rest of the workflow.

If you want to prevent the remaining workflow from running, you would need to either fail the job or make use of the CircleCI API v2 and cancel the workflow.

https://circleci.com/docs/api/v2/#cancel-a-workflow

Awesome, that worked, for future reference this is how I did it:

Go to account settings -> Personal API Tokens -> New token. Once you have the token go to the project and create a new environment variable something like CIRCLE_TOKEN and save it there.

Then in the config.yml you can run something like this to cancel the current workflow:

curl -X POST https://circleci.com/api/v2/workflow/${CIRCLE_WORKFLOW_ID}/cancel -H 'Accept: application/json' -u '${CIRCLE_TOKEN}:'

And that’s it, if that runs you should see something like this:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.