Fail a build based on Conditionals

I have a buildkite pipeline.yaml file, which contains multiple steps. Something like this:

env:
  SOME_ENV: "env"

steps:
  - label: "label-1"
    command: "./build-config.sh"

  - label: "label-2"
    command: .buildkite/publish.sh

I want to make the build fail, if the branch from which this build will be made contains an underscore (_) in the branch name. I tried using conditionals on the first command step like this : if: "build.branch =~ /[_]/", but this only skips the step and not fail the build. I want to know how can I achieve this.

It’s not pretty, but if you need it to fail, you could have an early step that checks for preconditions, and make it exit with an error code if the branch name has an underscore.

Something like:

- label: "check-preconditions"
  command: [[ ! "$BUILDKITE_BRANCH" =~ ^.*_.*$ ]]

hey @Andy , when I try to include this step I get this error: Pipeline parsing of "pipeline.yml" failed (Failed to parse pipeline.yml: line 5: did not find expected ',' or ']') and line 5 is label: "check-branch-name"

Hey!

We’ll probably need to see the whole pipeline, but it seems there’s a syntax error. If you can’t share the pipeline here you can drop a message to support@buildkite.com so we can take a look.

Best!

hey @paula the rest of the pipeline does not relate to what I am trying to achieve. I just want to make the build fail if the branch from which it is building contains an _ in the branch name. That’s it.

I think the only part @paula wanted to check was the step you added to check the branch, not necessarily the whole pipeline.

Just as a heads up, when I wrote the snippet above, I did not test it. I had just typed it out to illustrate the idea, and I guess I messed up the YAML syntax.

I’ve since done something similar, and I found that using the | YAML syntax helped:

steps:
  - command: |
      [[ ! "$BUILDKITE_BRANCH" =~ "_" ]]
    label: "check-preconditions"

Hey @praj!

Did you only want to do this for this pipeline, or for all pipelines in your org?

If it’s just for the pipeline, then @Andy’s solution above would be the simplest way to go about it, something like this would ensure the build fails on the first step and nothing runs after it:

steps:
  - command: |
      [[ ! "$BUILDKITE_BRANCH" =~ "_" ]]
    label: "check-preconditions"
  - wait:
  - command: echo Hello World
    label: "hello-world"

If you wanted to run it on all pipelines then you could do so by writing a check in the hook file.

Cheers,

Ben