# Conditional (block) step

**URL:** https://forum.buildkite.community/t/conditional-block-step/1891
**Category:** General
**Created:** [February 2, 2022, 1:17am UTC](https://forum.buildkite.community/t/conditional-block-step/1891 "2022-02-02T01:17:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![tfrokt](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/tfrokt/32/1036_2.png) [@tfrokt](https://forum.buildkite.community/u/tfrokt)
#### Post date: [February 2, 2022, 1:17am UTC](https://forum.buildkite.community/t/conditional-block-step/1891/1 "2022-02-02T01:17:27Z")

</div>

I am trying to find a simple solution for:

```auto
- 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?

---

<div class="post-metadata">

### Author: ![anon4496704](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@anon4496704](https://forum.buildkite.community/u/anon4496704)
#### Post date: [February 2, 2022, 3:57am UTC](https://forum.buildkite.community/t/conditional-block-step/1891/2 "2022-02-02T03:57:51Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

### Author: ![tfrokt](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/tfrokt/32/1036_2.png) [@tfrokt](https://forum.buildkite.community/u/tfrokt)
#### Post date: [February 3, 2022, 9:42pm UTC](https://forum.buildkite.community/t/conditional-block-step/1891/3 "2022-02-03T21:42:46Z")

</div>

Hi @anon4496704,

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!
