Is there a command or method that cancels the execution of subsequent steps/jobs but returns success?

I have the following workflow that has multiple jobs. I would like to halt the execution of subsequent steps/jobs in previous job.
And I would like to return a success status.

In the following workflow, I would like to halt the execution of the say-hello-second job in the say-hello job if the SKIP_JOB environment variable is set.
I have tried using the circleci-agent step halt command but it returns a success status and executes the subsequent jobs.
I didn’t find a way to do it, so could you give me the correct command or method?

version: 2.1

jobs:
  say-hello:
    docker:
      - image: cimg/base:current

    steps:
      - checkout
      - run:
          name: "halt step"
          command: |
            if [ -n "$SKIP_JOB" ]; then
              echo "halt this step and subsequent steps"
              circleci-agent step halt
            else
              echo "continue to the next step"
            fi
      - run:
          name: "Say Hello"
          command: echo "Hello, World!"

  say-hello-second:
    docker:
      - image: cimg/base:current
    steps:
      - checkout
      - run:
          name: "Say Hello Second"
          command: echo 'Hello, World'


workflows:
  say-hello-workflow:
    jobs:
      - say-hello:
          context:
            - context-text
      - say-hello-second:
          requires:
            - say-hello

Maybe try exit 1?

Thank you for your response.
I tried exit 1 and found that halt the execution of subsequent steps/jobs in previous job.
However, I want the success state mentioned above, but exit 1 return failure state.

And I would like to return a success status.

That’s why I’m looking for a way other than exit 1.

Hrm, yes, that is a little tricky.

Just to be clear, you’re trying to skip subsequent steps in the same job, right (not conditionally not run job b when job a hits this condition)?

I don’t think CircleCI conditionals can factor in env vars that were set during the job, so you might have to write a wrapper script to run all the steps (with the downside of increasing the risk of making the job evergreen if you don’t handle exit codes the right way within it; I’d still make sure to have -e, and probably -o pipefail set within it except where you’re handling this exception), or else persist the env var via $BASH_ENV, and run your same check in each subsequent step.

I’m not sure if there’s a way to trivially short-circuit the rest of the steps in the same job

If you want to look at using multiple jobs (with pipeline parameters), and break this job into multiple jobs, it might be able to do something a little more elegant? Like have a downstream job that runs the remaining steps, and trigger it from the upstream one, setting a parameter on the downstream one that determines whether to skip?

1 Like

Thank you for your response.
It is still difficult. I will continue to consider remedial measures based on your advice.