# How can I refactor the same conditions in many steps

**URL:** <https://forum.buildkite.community/t/how-can-i-refactor-the-same-conditions-in-many-steps/2185>\
**Category:** Pipelines\
**Created:** [August 24, 2022, 12:13pm UTC](https://forum.buildkite.community/t/how-can-i-refactor-the-same-conditions-in-many-steps/2185 "2022-08-24T12:13:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![haohaolee](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/haohaolee/32/1115_2.png) [@haohaolee](https://forum.buildkite.community/u/haohaolee)\
**Post date:** [August 24, 2022, 12:13pm UTC](https://forum.buildkite.community/t/how-can-i-refactor-the-same-conditions-in-many-steps/2185/1 "2022-08-24T12:13:31Z")

</div>

Hi folks,  
When I compose steps, I always have to use the same condition for many steps, for example

```auto
steps:
  - block: ":rocket: Release!"
    if: some condition
  - command: some.sh
    if: some condition

```

What can I do if I want to reuse the conditions and make these two steps run together

Thanks.

---

<div class="post-metadata">

**Author:** ![iammcgaber](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@iammcgaber](https://forum.buildkite.community/u/iammcgaber)\
**Post date:** [August 24, 2022, 1:05pm UTC](https://forum.buildkite.community/t/how-can-i-refactor-the-same-conditions-in-many-steps/2185/2 "2022-08-24T13:05:35Z")

</div>

Can you clarify what you mean by reusing the condition? You could place steps which depend on the same `if` logic in their own pipeline file and only upload that pipeline if the condition is true, in which case you would not need to apply that logic to any of the steps inside that pipeline.

---

<div class="post-metadata">

**Author:** ![haohaolee](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/haohaolee/32/1115_2.png) [@haohaolee](https://forum.buildkite.community/u/haohaolee)\
**Post date:** [August 24, 2022, 1:26pm UTC](https://forum.buildkite.community/t/how-can-i-refactor-the-same-conditions-in-many-steps/2185/3 "2022-08-24T13:26:43Z")

</div>

I mean I don’t want to write too many same if: statements.  
If I understand what you suggested correctly, it should be like:

```auto
steps:
- command: "buildkite-agent pipeline upload some.yaml"
  if: some_condition

```

I am thinking the same solution, but also want to know if there is any way else without a new yaml file.  
If this is the only way to go, I am happy to use this.

Thanks.

---

<div class="post-metadata">

**Author:** ![suma](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/suma/32/1306_2.png) [@suma](https://forum.buildkite.community/u/suma)\
**Post date:** [August 24, 2022, 5:00pm UTC](https://forum.buildkite.community/t/how-can-i-refactor-the-same-conditions-in-many-steps/2185/4 "2022-08-24T17:00:04Z")

</div>

Hi @haohaolee

If the conditional check you are doing is based on branch and builds are triggered through webhook then you can define conditional at pipeline level as explained in this [doc](https://buildkite.com/docs/pipelines/conditionals#conditionals-in-pipelines)

However if you want conditional check at step level then you can combine the command steps as shown below if that works for you and use the condition check

```auto
steps:
  - commands:
    - "npm install && npm test"
    - "moretests.sh"
    - "build.sh

```

Other option is to use Yaml anchors so that you do not need to define the check in every step and use yaml anchors to populate it for which ever step you want that.

---

<div class="post-metadata">

**Author:** ![vlad.voelk](https://avatars.discourse-cdn.com/v4/letter/v/f05b48/32.png) [@vlad.voelk](https://forum.buildkite.community/u/vlad.voelk)\
**Post date:** [August 25, 2022, 5:27am UTC](https://forum.buildkite.community/t/how-can-i-refactor-the-same-conditions-in-many-steps/2185/5 "2022-08-25T05:27:17Z")

</div>

We are using YAML’s alias construct for reusable code snippets  
something like that

```auto
configs:
  environments:
    - &terraform_agent_queue
      agents:
        queue: some_queue

  pipelines:
      # triggers build in the dev account on feature branch push. Optionally supports deleting resources.
      - &feature_pipeline
        if: |
          build.branch != "master" && build.pull_request.base_branch != "master"

      # triggers when a pull request has been created to merge to master
      - &tfplan_pipeline
        if: |
          build.pull_request.base_branch == "master"

      # triggers after the code has been merged to master
      - &master_pipeline
        if: |
          build.branch == "master"

steps:
  ################################################################################################
  # Pipeline Logic
  ################################################################################################
  - label: Execute Feature Pipeline
    <<: *terraform_agent_queue
    <<: *feature_pipeline
    commands:
      - cat ".buildkite/feature.yaml" | buildkite-agent pipeline upload --replace

  - label: Execute Terraform Plan
    <<: *terraform_agent_queue
    <<: *tfplan_pipeline
    commands:
      - cat ".buildkite/terraformplan.yaml" | buildkite-agent pipeline upload --replace

  - label: Execute Staging to Prod Pipeline
    <<: *terraform_agent_queue
    <<: *master_pipeline
    commands:
      - cat ".buildkite/master.yaml" | buildkite-agent pipeline upload --replace

```

---

<div class="post-metadata">

**Author:** ![haohaolee](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/haohaolee/32/1115_2.png) [@haohaolee](https://forum.buildkite.community/u/haohaolee)\
**Post date:** [August 25, 2022, 9:18am UTC](https://forum.buildkite.community/t/how-can-i-refactor-the-same-conditions-in-many-steps/2185/6 "2022-08-25T09:18:42Z")

</div>

Thanks. This is helpful, never knew this function.

---

<div class="post-metadata">

**Author:** ![paula](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/paula/32/954_2.png) [@paula](https://forum.buildkite.community/u/paula)\
**Post date:** [August 29, 2023, 5:39am UTC](https://forum.buildkite.community/t/how-can-i-refactor-the-same-conditions-in-many-steps/2185/7 "2023-08-29T05:39:32Z")

</div>


