Dynamically set pipeline environment variable

Hi,

  1. I would like to set a pipeline-wide environment variable VERSION equal to bash command output, e.g. git describe --tags.

  2. Moreover when I export a variable in command, I can’t seem to consume it as the next command:

commands:
  - export VERSION=$(git describe --tags)
  - echo ${VERSION?}       # buildkite throws an error saying VERSION is not available

If I understand correctly:

  • env only allows to set static values.
  • command export VERSION… would only set the value for the current step

BUILDKITE_TAG works only on tagged builds and does not contain hash.

Hey there @sublimeye! :wave:

Have you considered using meta-data for this?

Something like the below should work for you:

steps:
  - label: Set the VERSION
    command: |
      buildkite-agent meta-data set version "$(git describe --tags)"
    key: set_version
  - label: Get the version
    command: |
      echo $(buildkite-agent meta-data get version)
    depends_on: set_version
  - wait: ~
  - label: Do more stuff...
    command: |
      echo "Doing the things :allthethings:"

Hope it helps! :gem:

Another option would be to run those commands like this:

commands: |
    export VERSION=$(git describe --tags)
    echo ${VERSION?}

This won’t make the VERSION variable available for other steps tho.

2 Likes

I would consider using the environment agent hook, Managing pipeline secrets | Buildkite Documentation.

Conversely, if you dynamically generate pipelines, Dynamic pipelines: Define CI/CD pipelines in code at runtime, you can generate and set it that way. For example,

- command: “.buildkite/script.sh | buildkite-agent pipeline upload”

where your script.sh might look like,

#!/usr/bin/bash

VERSION=$(git describe --tags)

echo "env:"
echo "  VERSION: $VERSION" 
echo "steps:"
echo "  - command: \"echo $$VERSION\""
1 Like