Set pipeline-wide environment variables from a BuildKite command step?

2.9k views Asked by At

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.

1

There are 1 answers

3
Vočko On

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.