How can I refactor the same conditions in many steps

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

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.

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.

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

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.

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

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

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.

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

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
1 Like

Thanks. This is helpful, never knew this function.

1 Like