Cannot assign meta-data to variable

trying to implement very simple use case…

Group 1, Step

buildkite-agent meta-data set ENV $DEFAULT_ENV

Group 2, Step

buildkite-agent meta-data get ENV
var=$(buildkite-agent meta-data get ENV)
echo “->” $var

Result is


It seems that command “buildkite-agent meta-data get” works but I cannot assign it to var
Any idea ? Thanks

Agent is running on MacOS, buildkite-agent version 3.57.0+x.78b37076ce6362a0c3f700edcbd0bdcf9f22d2d8

Hello @xtof!

Thanks for the question - and welcome to the Buildkite Forum! :wave:

I’m assuming you’d want to keep this within the Step editor? Since these commands are all done within processes (with PIDs) - you could do something like this (assuming its a similar approach!) - note the $$ interpolation:

  - group: "Group 1"
    key: "group_1"
      - label: ":cd: Setting env"
        commands: | 
          buildkite-agent meta-data set env $ENV
  - group: "Group 2"
    key: "group_2"
    - "group_1"
      - label: ":printer: Export/obtaining var1"
        commands: | 
          export var1=$(buildkite-agent meta-data get env)
          echo $$var1

Keep in mind, var1 will only be available to that job itself (from the export) - you could run this all in a script to a similar effect too, this is the assumption its all kept inside the group step as above.

Another way you could go around this is using build level environment variables, so something like

  ENVIRONMENT: production

at the start of the pipeline, and use $ENVIRONMENT through the build (this can also be set at the start of the build too, so for example, ENVIRONMENT: $ENVIRONMENT (and set ENVIRONENT=xyz in the Environment Variables section on a new build through the UI/set in the payload within the API:


Hope that helps!

Thanks @james.s
It is working : i missed interpolation to manipulate variable after meta-data get
why do we need it ?

:wave: Interpolation happens at upload time, so in this case when you are trying to use $var is actually evaluated before the build starts running and at that time, the step to set the variable hasn’t run, so it ends up being empty.
You can read more about variable interpolation in our docs

thanks @paula