Expressing `not includes` in `if` condition?

I would like to choose between two steps, depending on whether the GitHub pull request has a particular label. Something like this:

steps:
  - command: "echo normal"
    if: !(build.pull_request.labels includes "experimental")
    
  - command: "echo experimental"
    if: build.pull_request.labels includes "experimental"

However this doesn’t seem to work. What I really want is a BUILDKITE_PULL_REQUEST_LABELS environment variable so I can change behavior in the command, but that isn’t available, so I’m trying to pick between two jobs using built.pull_request.labels.

For now I’m able to get away with querying the GitHub API directly, however I think the !(array includes "item") syntax makes sense and is sorely lacking in Buildkite.

Hi @evanrelf!

Using !(...) should work, but perhaps you’re having trouble because starting a value with “!” has special meaning in YAML sorry. This syntax should work:

steps:
  - command: "echo normal"
    if: |
      !(build.pull_request.labels includes "experimental")
    
  - command: "echo experimental"
    if: build.pull_request.labels includes "experimental"

This site is useful for testing how YAML is being parsed:

https://yaml-online-parser.appspot.com

1 Like

Ahh I see, thank you! YAML nuances strike again :slightly_smiling_face:

One thing to note: exposing PR labels only in the if attribute of the step, and not as an environment variable in the command environment, means you’re forced to choose different keys for each possible job.

In the example I gave, the “normal” and “experimental” job would require different keys, so you’d need to be aware of that when writing depends_on attributes in later steps.

In my case, this isn’t a problem. But I still think a hypothetical BUILDKITE_PULL_REQUEST_LABELS variable would be a better solution than the workaround I’m doing here.

2 Likes