2

I have a BuildKite pipeline with two command steps and a block step in-between. Among other things, the first command step gathers a bunch of information and puts it into environment variables. Then the block runs and asks the user to continue. Assuming approved, the second command runs but does not have the previously-set environment variables (separate step, could be separate agent, etc.).

Simplest reproducible:

    steps:
      - label: "Test setting variable"
        command: |
          export MY_VAR=SomeValue
      - wait: ~
      - label: "Test using variable"
        command: |
          echo MY_VAR=$$MY_VAR

Is there any way to tell BuildKite to add exported environment variables to the pipeline-wide variables? I have a wait so the second step will not run until the first finishes.

I realize I can use buildkite-agent meta-data set/get. I'll do that if there is no better way. Thank you.

Samuel Neff
  • 73,278
  • 17
  • 138
  • 182

1 Answers1

4

You can have a global env section for the whole pipeline:

env:
  MY_VAR: SomeValue

steps:
  ...

Variables set here are available to all steps.

Not sure you have the values you want to set at that point though, sounds like the values are only calculated in the step.

As an alternative you can try export the values into a file, store the file as an artifact and then restore it in the desired step:

- label: "First step"
  commands:
    - echo export MY_VAR=SomeValue > shared_vars.sh
  artifact_paths:
    - shared_vars.sh

- label "Second step"
  commands:
    - buildkite-agent artifact download shared_vars.sh .
    - source shared_vars.sh
    - echo MY_VAR=$$MY_VAR

This is an untested pseudo-code but I was using something similar to fix the same problem in Bitbucket pipelines and it worked well.

Vočko
  • 2,478
  • 1
  • 23
  • 31
  • Thanks. Generating an artifact is a good idea. I ended up generating the next steps so I could inject the variables dynamically. It worked ok but made a mess of escaping, sometimes `$`, `\$`, or `\$\$` depending on when I wanted them injected variable evaluated. – Samuel Neff Jul 01 '22 at 23:26
  • Thanks for the reply. Is this (the "global `env` section") mentioned anywhere in the Buildkite documentation? So far I couldn't find it. – Capt. Crunch Feb 02 '23 at 22:18
  • I can't find where it would be explicitly explained but you can find it in some examples, like here https://buildkite.com/docs/pipelines/secrets#anti-pattern-storing-secrets-in-your-pipeline-dot-yml . But I agree that Buildkite's documentation is horrible. We are moving away from bk as it is a hopeless tool. – Vočko Feb 04 '23 at 07:03