Conditional (block) step

I am trying to find a simple solution for:

- Step: Sanity check
- Block
  - if: sanity check failed
- Step: Run Update

The block step should only be shown if the sanity check failed (so some human can check), if the sanity check passes, we can go straight to Run Update.

a) Sanity check fails:
S -> B -> U

b) Sanity check passes:
S -> U

I don’t think I can “communicate” between steps using build.state using passed, failed as I never want to fail the build.

An alternative could be to duplicate the "Run Update step and use depends_on, but that violates the DRY principle:
a) S -> B -> U1
b) S -> B2

Any recommendations?

Hi @tfrokt!

Thanks for checking with us!

We can check the outcome of the “Sanity check” step and then dynamically upload a block step based on the result of the ‘Sanity check’. We also need to soft_fail the ‘Sanity check” step, so it continues to run the remaining steps in the pipeline even when it fails.

A sample pipeline would be like this:

steps:
  - command: exit -1
    key: "sanity-check"
    soft_fail: 
      - exit_status: "*"
  - wait: ~
  - command : |
      if [ $$(buildkite-agent step get "outcome" --step "sanity-check") == "soft_failed" ]; then
         cat <<- YAML | buildkite-agent pipeline upload
         steps:
           - block: "sanity is failed. check and unblock me."
      YAML
      fi
  - wait: ~
  - command: echo "running regression"

Some useful info on the insertion order when using dynamic upload: Steps are inserted immediately following the job performing the pipeline upload. Note that if you perform multiple uploads from a single step, they can appear to be in reverse order, because the later uploads are inserted earlier in the pipeline.

Hope this helps solve your use case!

Let us know for any questions!

Best,
Nithya

Hi @nithya,

Thanks for the quick reply. It is not the prettiest solution though :) Would be nice if it would be possible to just use an if on a later step rather than injecting yaml. But I’ve done it that way and it works perfectly.

Thanks again!