# Run step based on status of another step syntactic sugar

**URL:** <https://forum.buildkite.community/t/run-step-based-on-status-of-another-step-syntactic-sugar/3971>\
**Category:** Features Requests\
**Created:** [October 16, 2024, 2:52pm UTC](https://forum.buildkite.community/t/run-step-based-on-status-of-another-step-syntactic-sugar/3971 "2024-10-16T14:52:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaimeb](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@jaimeb](https://forum.buildkite.community/u/jaimeb)\
**Post date:** [October 16, 2024, 2:52pm UTC](https://forum.buildkite.community/t/run-step-based-on-status-of-another-step-syntactic-sugar/3971/1 "2024-10-16T14:52:59Z")

</div>

I want to run a step based on the status of another step. The [docs on conditionals](https://buildkite.com/docs/pipelines/conditionals#conditionals-in-steps) state:

> Since `if` conditions are evaluated at the time of the pipeline upload, it’s not  
> possible to use the `if` attribute to conditionally run a step based on the result of another step.
> 
> To run a step based on the result of another step, upload a new pipeline based on the `if` condition set up in the [command step](https://buildkite.com/docs/pipelines/command-step) like in the example below:

```auto
steps:
  - label: "Validation check"
    command: ./scripts/validation_tests.sh
    key: "validation-check"
  - label: "Run regression only if validation check is passed"
    depends_on: "validation-check"
    command: |
      if [$$(buildkite-agent step get "outcome" --step "validation-check") == "passed"]; then
         cat <<- YAML | buildkite-agent pipeline upload
         steps:
           - label: "Run Regression"
             command: ./scripts/regression_tests.sh
      YAML
      fi

```

This works fine, but is more error-prone. We tried using this but because our IDE was unable to syntax-check the embedded YAML, we committed an error.

Suggestion: could we add something like a `if-runtime` or `if-after-load` that essentially does the above under the hood for us? E.g. the following (mind my syntax, may not be 100% correct)

```auto
  - label: "Run regression only if validation check is passed"
    depends_on: "validation-check"
    if-runtime: $$(buildkite-agent step get "outcome" --step "validation-check") == "passed"
    command: ./scripts/regression_tests.sh

```

will be converted to/interpreted by Buildkite as:

```auto
  - label: "Run regression only if validation check is passed"
    depends_on: "validation-check"
    command: |
      if [$$(buildkite-agent step get "outcome" --step "validation-check") == "passed"]; then
         cat <<- YAML | buildkite-agent pipeline upload
         steps:
           - label: "Run Regression"
             command: ./scripts/regression_tests.sh
      YAML
      fi

```

---

<div class="post-metadata">

**Author:** ![ivanna](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/ivanna/32/1410_2.png) [@ivanna](https://forum.buildkite.community/u/ivanna)\
**Post date:** [October 16, 2024, 5:30pm UTC](https://forum.buildkite.community/t/run-step-based-on-status-of-another-step-syntactic-sugar/3971/2 "2024-10-16T17:30:08Z")

</div>

Hey @jaimeb,  
Thank you for your suggestions to implement an `if-runtime` or` if-after-load` attributes to Buildkite. I will pass your feedback to the product team and we will keep you updated.  
Thanks,

---

<div class="post-metadata">

**Author:** ![chris\_barrell](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@chris\_barrell](https://forum.buildkite.community/u/chris_barrell)\
**Post date:** [October 17, 2024, 4:53am UTC](https://forum.buildkite.community/t/run-step-based-on-status-of-another-step-syntactic-sugar/3971/3 "2024-10-17T04:53:31Z")

</div>

Hi @jaimeb ,  
I’m from the product team at Buildkite. What you said makes sense and would be much neater. But I’m also wondering why you need to explicitly check the outcome of the step when you’ve already set your second step to `depends_on` the first step? Because of this, it already will only run when the first step passes anyway. Was there something different about your use case of checking the outcome?  
Cheers,  
Chris

---

<div class="post-metadata">

**Author:** ![jaimeb](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@jaimeb](https://forum.buildkite.community/u/jaimeb)\
**Post date:** [October 21, 2024, 3:16pm UTC](https://forum.buildkite.community/t/run-step-based-on-status-of-another-step-syntactic-sugar/3971/4 "2024-10-21T15:16:23Z")

</div>

Hi Chris,

We have several steps that do some work whenever a specific job fails (send a slack notification, annotate the job, to name a few). We only want to run this step if the step it depends on failed, and otherwise skip it altogether.  
Here is an example of one of those jobs:

```auto
  - label: ":slack: Check if we should notify on deploy failure"
    depends_on: "deploy-service"
    command: |
      if [$(buildkite-agent step get "outcome" --step "deploy-service") != "passed"]; then
         cat <<- YAML | buildkite-agent pipeline upload
         steps:
           - label: ":alert: Notify slack about failed deploy step :alert:"
             soft_fail: true
             command:
               - echo "Notifying slack about the deploy-service step"
               - exit 1
             notify:
               - slack:
                   channels:
                     - "#deploy-notifications"
                   message: "deploy-service step has failed"
      YAML
      fi

```

We are uploading a new step IFF a step fails. If we could merge these two steps (the uploader and the uploaded), it’d be great.

---

<div class="post-metadata">

**Author:** ![stephanie.atte](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/stephanie.atte/32/1357_2.png) [@stephanie.atte](https://forum.buildkite.community/u/stephanie.atte)\
**Post date:** [October 21, 2024, 9:40pm UTC](https://forum.buildkite.community/t/run-step-based-on-status-of-another-step-syntactic-sugar/3971/5 "2024-10-21T21:40:54Z")

</div>

Hey @jaimeb,

I totally see where you’re coming from, but unfortunately, we can’t merge the two. The conditionals for steps are evaluated when the pipeline or step is uploaded, so it makes sense to handle the step upload after the first conditional is in place.

Since a new step is created at runtime, there’s no way to merge an upload that’s already been processed with one that’s currently happening.

Is there a specific challenge you’re running into with this setup, or is it more about keeping the YAML shorter and cleaner?

---

<div class="post-metadata">

**Author:** ![jaimeb](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@jaimeb](https://forum.buildkite.community/u/jaimeb)\
**Post date:** [October 28, 2024, 7:24pm UTC](https://forum.buildkite.community/t/run-step-based-on-status-of-another-step-syntactic-sugar/3971/6 "2024-10-28T19:24:09Z")

</div>

Oh no what we have now meets our needs!

This idea came to mind when debugging an issue with one of our pipelines where a syntax error we introduced into our embedded YAML went unnoticed during review, and would have been caught if our IDE had highlighted it as YAML. So this is more of a nice-to-have to avoid human error

---

<div class="post-metadata">

**Author:** ![Priya](https://avatars.discourse-cdn.com/v4/letter/p/eada6e/32.png) [@Priya](https://forum.buildkite.community/u/Priya)\
**Post date:** [October 28, 2024, 10:33pm UTC](https://forum.buildkite.community/t/run-step-based-on-status-of-another-step-syntactic-sugar/3971/7 "2024-10-28T22:33:18Z")

</div>

Hey @jaimeb 👋

Good to know you got this working with depends on and thanks for clarifying the challenge with the current setup.

I’m passing your feedback to the feature request we created for the product team to review

Let us know if you have any other questions!

Cheers,

---

<div class="post-metadata">

**Author:** ![chris\_barrell](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@chris\_barrell](https://forum.buildkite.community/u/chris_barrell)\
**Post date:** [October 29, 2024, 4:37am UTC](https://forum.buildkite.community/t/run-step-based-on-status-of-another-step-syntactic-sugar/3971/8 "2024-10-29T04:37:07Z")

</div>

Interesting! Yes, selectively running on failure isn’t handled with depends\_on. I get where you’re coming from now.

Being able to specify “Run this job if step key ABC state != passed” would be much more convenient. I can also see it being useful to just have an if statement on the notify to handle the same situation. “Notify if job.state is failed”.

Thanks for sharing another example, I’ll add them to our product feedback so we’ll see them when we review next.

Cheers
