# Meta-data in conditional clause

**URL:** <https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190>\
**Category:** Features Requests\
**Created:** [August 18, 2020, 5:30pm UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190 "2020-08-18T17:30:46Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![renanberto](https://avatars.discourse-cdn.com/v4/letter/r/a6a055/32.png) [@renanberto](https://forum.buildkite.community/u/renanberto)\
**Post date:** [August 18, 2020, 5:30pm UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190/1 "2020-08-18T17:30:47Z")

</div>

Example:

```auto
  - block: "Request release"
    prompt: "Fill out the details for release"
    fields:
      - text: "Version"
        key: "version"
        hint: "Include the version according to the standard: 2.x.x.x"
        required: true
      - select: "Type"
        key: "release-type"
        default: "stable"
        options:
          - label: "Stable"
            value: "stable"
          - label: "Beta"
            value: "beta"
          - label: "Debug"
            value: "debug"
  - wait
  - trigger: "stable"
    label: ":rocket: Release a stable version"
    if: $(buildkite-agent meta-data get release-type) == "stable"

```

today i try to do a conditional based on a block but it is not possible

---

<div class="post-metadata">

**Author:** ![sj26](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/sj26/32/17_2.png) [@sj26](https://forum.buildkite.community/u/sj26)\
**Post date:** [August 19, 2020, 2:01am UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190/2 "2020-08-19T02:01:27Z")

</div>

Hi @renanberto!

I saw this comment over in our agent repository, too:

> <https://github.com/buildkite/agent/issues/1155>
>
> Hi there,
> I'm looking to add a step in my pipeline to block and ask if tests should be run. Here's the...

I think this is a great idea so I’ve raised the issue internally and we’ll see what we can do. 🕺

---

<div class="post-metadata">

**Author:** ![renanberto](https://avatars.discourse-cdn.com/v4/letter/r/a6a055/32.png) [@renanberto](https://forum.buildkite.community/u/renanberto)\
**Post date:** [August 19, 2020, 2:50pm UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190/4 "2020-08-19T14:50:40Z")

</div>

Cool! tomorrow I’ll spend a few hours to propose the change here: [https://github.com/buildkite/conditional](https://github.com/buildkite/conditional)! I send it to you on the issue and here to see if I can help you with something!

it is also interesting to say that we did a workaround with hook. At the time of decision we look for metadata and transform it into environment, but it is definitely not the best approach, considering that metadata will not always be present in the step.

Thanks for prioritizing!

---

<div class="post-metadata">

**Author:** ![sj26](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/sj26/32/17_2.png) [@sj26](https://forum.buildkite.community/u/sj26)\
**Post date:** [August 19, 2020, 11:40pm UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190/5 "2020-08-19T23:40:04Z")

</div>

Thanks for being eager, but that repository is for the language, not how it is hooked up to builds. :-)

---

<div class="post-metadata">

**Author:** ![sj26](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/sj26/32/17_2.png) [@sj26](https://forum.buildkite.community/u/sj26)\
**Post date:** [August 21, 2020, 7:01am UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190/6 "2020-08-21T07:01:28Z")

</div>

Hi folks! After a deep discussion we’ve decided not to support meta-data in conditionals just yet, sorry! We want to do it well, and have an answer for what happens when meta-data changes over the lifecycle of a build, and we haven’t quite figured that out yet.

For now, you can do this with the buildkite-agent and a bit of scripting — and it’s not super different, like:

```auto
# buildkite.yml
steps:
  - block: "Request release"
    prompt: "Fill out the details for release"
    fields:
      - text: "Version"
        key: "version"
        hint: "Include the version according to the standard: 2.x.x.x"
        required: true
      - select: "Type"
        key: "release-type"
        default: "stable"
        options:
          - label: "Stable"
            value: "stable"
          - label: "Beta"
            value: "beta"
          - label: "Debug"
            value: "debug"
            
  - command: |
      if [["$$(buildkite-agent meta-data get release-type)" == "stable"]]; then
        buildkite-agent pipeline upload buildkite-release-stable.yml
      fi

   # or even

  - command: buildkite-agent pipeline upload "buildkite-release-$$(buildkite-agent meta-data get release-type).yml"

```

```auto
# buildkite-release-stable.yml
steps:
  - trigger: stable
    label: ":rocket: Release a stable version"

```

We’ll make some noise when conditional support for meta-data changes.

---

<div class="post-metadata">

**Author:** ![renanberto](https://avatars.discourse-cdn.com/v4/letter/r/a6a055/32.png) [@renanberto](https://forum.buildkite.community/u/renanberto)\
**Post date:** [August 21, 2020, 2:18pm UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190/7 "2020-08-21T14:18:43Z")

</div>

thanks for the feedback! We put a case to determine the steps within a command:

```auto
  - command: >
      case $(buildkite-agent meta-data get "release-type") in
        "stable") buildkite-agent pipeline upload .buildkite/stable.yml ;;
        "beta") buildkite-agent pipeline upload .buildkite/beta.yml ;;
        *) echo "Unknown release type" && exit 1
      esac
    label: ":rocket: - Release"
    key: "choose-release"
    agents:
      queue: "android"
    if: build.pull_request.id != null

```

I’ll get that thread dirty to ask you something…We have artifact dependency between jobs, in this case, before I call stable.yml I generate an artifact. How do I get this artifact inside another step that was uploaded by the agent?

E.G:

```auto
  - command:
      - "gradle build'"
      - "mv apk/production.apk /tmp/production.apk"
    artifact_paths: 
      - "/tmp/production.apk"
    label: "Build"
    key: "production-release"
    agents:
      queue: "android"

```

- 
## in this case, stable is selected

```auto
  - block: "Request release"
    prompt: "Fill out the details for release"
    fields:
      - text: "Version"
        key: "version"
        hint: "Include the version according to the standard: 2.x.x.x"
        required: true
      - select: "Type"
        key: "release-type"
        default: "stable"
        options:
          - label: "Stable"
            value: "stable"
          - label: "Beta"
            value: "beta"
          - label: "Debug"
            value: "debug"
    key: "request-release"

  - command: >
      case $(buildkite-agent meta-data get "release-type") in
        "stable") buildkite-agent pipeline upload .buildkite/stable.yml ;;
        "beta") buildkite-agent pipeline upload .buildkite/beta.yml ;;
        *) echo "Unknown release type" && exit 1
      esac
    label: ":rocket: - Release"
    key: "choose-release"
    agents:
      queue: "android"

```

- 
## After uploaded I have new step, and I need to the get artifact:

```auto
steps:
  - command:
      - buildkite-agent artifact download /tmp/production.apk /tmp --step production-release

```

Can you help me with this? Again I understand that this is not the place, but I’ve been breaking my head for some time.

---

<div class="post-metadata">

**Author:** ![Jason](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/jason/32/742_2.png) [@Jason](https://forum.buildkite.community/u/Jason)\
**Post date:** [August 23, 2020, 10:24pm UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190/8 "2020-08-23T22:24:16Z")

</div>

Hey @renanberto, can you send this through to us at [support@buildkite.com](mailto:support@buildkite.com) and we can get someone to go deep on this issue for you.

---

<div class="post-metadata">

**Author:** ![jerry](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/jerry/32/775_2.png) [@jerry](https://forum.buildkite.community/u/jerry)\
**Post date:** [September 24, 2020, 5:44am UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190/9 "2020-09-24T05:44:08Z")

</div>

I’d love for the `if:` clause mechanism to be made more powerful – at my company we use top-level git diffing to do a lot of downstream decisions w.r.t. which parts of a build hierarchy to run (complex mono-repo!)

And I’d love to be able to build a (more flexible) mechanism to use diff output in `if:` clauses, which is complicated by the limitations of which data sources can be fed into this mechanism.

If there were some more general mechanism to feed data (files) reliably and without formatting changes into inspectable variables for downstream pipelines and steps that’d open up a large swathe of improvements and simplifications w.r.t. ease-of-use for our developers.

---

<div class="post-metadata">

**Author:** ![jeffhappily](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@jeffhappily](https://forum.buildkite.community/u/jeffhappily)\
**Post date:** [December 17, 2020, 10:06am UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190/10 "2020-12-17T10:06:26Z")

</div>

Hey, I’m actually trying to do something similar, just wondering what’s the difference between using `buildkite-agent` to upload a pipeline that triggers the target pipeline, and directly using `buildkite-agent` to upload the target pipeline?

And if I want to use the proposed method, how do I pass arguments when doing the upload?  
For example

```auto
# pipeline.yml
- command: 
  if [["$$(buildkite-agent meta-data get release-type)" == "stable"]]; then
    buildkite-agent pipeline upload trigger.yml
  fi

```

```auto
# trigger.yml
steps:
  - trigger: target
    build:
      env:
        something: ${something}

```

I want to pass something from `pipeline.yml` to `trigger.yml` so that it can pass it into `target.yml`, how can I do it?

---

<div class="post-metadata">

**Author:** ![Jason](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/jason/32/742_2.png) [@Jason](https://forum.buildkite.community/u/Jason)\
**Post date:** [December 18, 2020, 3:16am UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190/11 "2020-12-18T03:16:11Z")

</div>

Hey @jeffhappily welcome! 👋

I don’t think there is much difference in the target and trigger pipeline, it’s just an example in this case where the release pipeline most likely already exists and we don’t want to do a pipeline upload for it and a trigger makes more sense.

If I was going to create trigger.yml like that, I would probably do it as a [dynamic pipeline](https://buildkite.com/docs/pipelines/defining-steps#dynamic-pipelines) which would make passing those values a little easier.

```
# pipeline.yml
- command: 
  if [["$$(buildkite-agent meta-data get release-type)" == "stable"]]; then
    .buildkite/trigger.sh | buildkite-agent pipeline upload
  fi
```

---

<div class="post-metadata">

**Author:** ![jeffhappily](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@jeffhappily](https://forum.buildkite.community/u/jeffhappily)\
**Post date:** [December 21, 2020, 8:00am UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190/12 "2020-12-21T08:00:56Z")

</div>

Thanks for the reply! I have another similar question, since metadata doesn’t work for the `if` clause in `trigger`, will it work in the `build.env` or `build.meta_data`? Like is it possible for me to pass in a metadata in the current pipeline to another pipeline with the `trigger`?

---

<div class="post-metadata">

**Author:** ![jeffhappily](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@jeffhappily](https://forum.buildkite.community/u/jeffhappily)\
**Post date:** [December 21, 2020, 9:30am UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190/13 "2020-12-21T09:30:40Z")

</div>

And how does pipeline upload actually work or what’s the expected behavior of pipeline upload?

If I have a pipeline with 4 steps, and if I do a pipeline upload at step 2, what will happen? Will it overwrite step 3 & 4 and run the new steps uploaded, or will it put the uploaded pipeline in between step 2 & 3, or will it run the uploaded pipeline somewhere else?

I tried running

```auto
buildkite-agent pipeline upload mypipeline.yml

```

and the step ran successfully but nothing really happens. I don’t see anything from the `mypipeline.yml` running

---

<div class="post-metadata">

**Author:** ![Jason](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/jason/32/742_2.png) [@Jason](https://forum.buildkite.community/u/Jason)\
**Post date:** [December 21, 2020, 8:57pm UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190/14 "2020-12-21T20:57:04Z")

</div>

Hey @jeffhappily The upload command will insert the steps from the script into the build immediately after the upload step.

If you send us through a message to [support@buildkite.com](mailto:support@buildkite.com) with a link to your build and the issues you are having we can help you with your pipeline in a bit more detail.

---

<div class="post-metadata">

**Author:** ![Dave](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@Dave](https://forum.buildkite.community/u/Dave)\
**Post date:** [May 11, 2021, 2:12am UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190/15 "2021-05-11T02:12:06Z")

</div>

On the README here [GitHub - buildkite/conditional: A small language for conditionals](https://github.com/buildkite/conditional) there is a syntax example:  
// complex expressions  
((build.tag =~ ^v) || (meta-data(“foo”) == “bar”))

I don’t see `meta-data()` anywhere else in that repo so I guess the example was wishful thinking?

---

<div class="post-metadata">

**Author:** ![anon18884907](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@anon18884907](https://forum.buildkite.community/u/anon18884907)\
**Post date:** [May 11, 2021, 4:20am UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190/16 "2021-05-11T04:20:10Z")

</div>

Hi @Dave, welcome to the forum! – yeah, I think that might be a leftover fragment of a design decision we made that’s largely to do with conditionals being evaluated at the time of pipeline upload vs. how meta-data might change over the course of a build. I’ll cross-link a post from @sj26 about this on GitHub.

> Hi folks! After a deep discussion we’ve decided not to support meta-data in conditionals just yet, sorry! We want to do it well, and have an answer for what happens when meta-data changes over the lifecycle of a build, and we haven’t quite figured that out yet.

There’s a description of a workaround using scripting in the previously linked issue:

> <https://github.com/buildkite/agent/issues/1155#issuecomment-678073377>
>
> Hi there,
> 
> I'm looking to add a step in my pipeline to block and ask if tests …should be run. Here's the general pattern:
> 
> \`\`\`yaml
> steps:
> - block: "Prompt for tests"
> fields:
> - select: "Run tests?"
> key: "run-tests"
> options:
> - label: "Sure"
> value: "yes"
> - label: "No thanks"
> value: "no"
> if: build.source == "ui"
> 
> # Ideal approach using an "if" (feature request?)
> - label: "Run tests"
> key: test
> command: echo "test me!"
> if: build.metadata("run-tests", "false") == "true"
> plugins:
> - docker-compose#v3.1.0:
> run: app
> 
> # Workaround, but doesn't work inside docker image without buildkite-agent
> - label: "Run tests take 2"
> key: test
> command: \>
> if \["$(buildkite-agent meta-data get run-tests --default false)" = "true" \]; then
> echo "test me!"
> else
> echo "tests skipped"
> fi
> plugins:
> - docker-compose#v3.1.0:
> run: app
> \`\`\`
> 
> I suppose another approach could be to have a hook that tries to read the metadata and export that as an environment variable that can be used, but it feels like a lot of work to do something that should be a native solution.
> 
> Any thoughts on this?

---

<div class="post-metadata">

**Author:** ![Dave](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@Dave](https://forum.buildkite.community/u/Dave)\
**Post date:** [May 11, 2021, 4:48am UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190/17 "2021-05-11T04:48:11Z")

</div>

Thanks for the reply @anon18884907 , makes sense and I appreciate the workaround.

The weird use case I have is an initial step that checks a database and a ready/not ready value to determine whether or not to run the rest of the pipeline.

I thought perhaps something like 3 pipeline files  
pipeline.yml contains the initial step and a publish pipeline based on that  
noop.yml just echoes that there is nothing to do  
dostuff.yml has the full rest of the pipeline

Is that sensible? what would pseudocode for that look like, or is there something cleaner and simpler?

tbh I think this is probably nicer than using metadata in conditionals - I can keep the dostuff.yml pipeline clean

---

<div class="post-metadata">

**Author:** ![Jason](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/jason/32/742_2.png) [@Jason](https://forum.buildkite.community/u/Jason)\
**Post date:** [May 12, 2021, 5:03am UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190/18 "2021-05-12T05:03:37Z")

</div>

Hey @Dave 👋

Hmm - kinda sounds like you could achieve that with a [dynamic pipeline](https://buildkite.com/docs/pipelines/defining-steps#dynamic-pipelines) upload on the one pipeline?

If you need any further help with this you could also send us a message at [support@buildkite.com](mailto:support@buildkite.com) with your pipeline(s) and we can look at them a little deeper for you?

---

<div class="post-metadata">

**Author:** ![Fisher](https://avatars.discourse-cdn.com/v4/letter/f/a8b319/32.png) [@Fisher](https://forum.buildkite.community/u/Fisher)\
**Post date:** [December 13, 2021, 1:37pm UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190/19 "2021-12-13T13:37:24Z")

</div>

This conditional feature will be helpful for us, as developers want to manually skip UAT step when committing specific branches, and expect a block step with the selection of "deply in \*\*\*” （YES or NO), then the next step (like trigger another pipeline in DEV or UAT) can be skipped if the selection is “NO”.

---

<div class="post-metadata">

**Author:** ![a\_Sam\_a](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@a\_Sam\_a](https://forum.buildkite.community/u/a_Sam_a)\
**Post date:** [April 6, 2022, 10:11pm UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190/20 "2022-04-06T22:11:58Z")

</div>

I was able to use meta-data in conditionals by loading them into environment variables, like this:

```auto
steps:
  - block: ":rocket: PROD Release!"
    prompt: You are about to create a new release. This cannot be undone.
    fields:
      - select: "Release type"
        key: "release_type"
        multiple: false
        options:
          - label: "Patch"
            value: "Patch"
          - label: "Minor"
            value: "Minor"
          - label: "Major"
            value: "Major"
          - label: "None (What are you doing here then?)"
            value: "None"
  - wait: ~

  - label: "Load release type into env variable"
    command: |
      export release_type=$(buildkite-agent meta-data get release_type)

  - wait: ~

  - label: "Triggering primarily pipeline to build and release"
    if: "build.env('release_type') != 'None'"
    command: |
      echo Building release of "$release_type"

  - label: "Cancelled release"
    if: "build.env('release_type') == 'None'"
    command: echo Cancelled release

```

---

<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:** [April 6, 2022, 10:23pm UTC](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190/21 "2022-04-06T22:23:20Z")

</div>

Thanks for sharing @a_Sam_a! and welcome to the community! 🤗

[Next page](https://forum.buildkite.community/t/meta-data-in-conditional-clause/1190.md?page=2)
