Keep the input value around from input

I have this issue reusing my env-var around from my input to multiple tasks.
Say I have a task like this

steps:
  - input: "Select Target Environment"
    key: "environment-selection"
    prompt: "Choose target environment for deployment"
    fields:
      - select: "Environment"
        key: "environment-name"
        required: true
        options:
          - label: "Test Environment"
            value: "test"
          - label: "Staging Environment"
            value: "staging"
        default: "test"

This will select where I want to run my build, either test or staging

 - name: ":terraform: Infra Plan"
    key: "infra-plan"
    command: |
      buildkite-agent meta-data get environment-name
      SELECTED_ENV="$(buildkite-agent meta-data get environment-name)"
      cd terraform/infra/environments/$SELECTED_ENV

I have a few tasks that need to re-use that output ”test or staging” like above.
I can get the value with buildkite-agent meta-data get environment-name.
But is there a way I can reuse that value again without re-getting it from the build-kite agent over and over again?

Hey @matthewrollitt :waving_hand:,

Welcome to the Buildkite Community! :tada:

The best way to handle this would be using Dynamic pipelines, you could do something like this to specifically set a build-level environment variable based off of the input of the environment-selection key:

steps:
  - input: "Select Target Environment"
    key: "environment-selection"
    prompt: "Choose target environment for deployment"
    fields:
      - select: "Environment"
        key: "environment-name"
        required: true
        options:
          - label: "Test Environment"
            value: "test"
          - label: "Staging Environment"
            value: "staging"
        default: "test"

  - label: "Upload Pipeline with Environment"
    key: "upload-pipeline"
    depends_on: "environment-selection"
    command: |
      SELECTED_ENV=$(buildkite-agent meta-data get environment-name)
      
      cat << EOF > .buildkite/dynamic-pipeline.yml
      env:
        SELECTED_ENV: "$${SELECTED_ENV}"
      EOF
      
      buildkite-agent pipeline upload .buildkite/dynamic-pipeline.yml

  - label: "Environment check"
    command: |
      echo "Environment set to $$SELECTED_ENV"
    depends_on: "upload-pipeline"

Using this method, when calling SELECTED_ENV, you would need to reference it with an additional $ due to the way we handle variable interpolation.

Hi @Joe.Coleman thanks for the warm welcome! but I tried that exact code to see if would work

steps:
  - input: "Select Target Environment"
    key: "environment-selection"
    prompt: "Choose target environment for deployment"
    fields:
      - select: "Environment"
        key: "environment-name"
        required: true
        options:
          - label: "Test Environment"
            value: "test"
          - label: "Staging Environment"
            value: "staging"
        default: "test"

  - label: "Upload Pipeline with Environment"
    key: "upload-pipeline"
    depends_on: "environment-selection"
    command: |
      SELECTED_ENV=$(buildkite-agent meta-data get environment-name)
      
      cat << EOF > .buildkite/dynamic-pipeline.yml
      env:
        SELECTED_ENV: "$${SELECTED_ENV}"
      EOF
      
      buildkite-agent pipeline upload .buildkite/dynamic-pipeline.yml

  - label: "Environment check"
    command: |
      echo "Environment set to $$SELECTED_ENV"
    depends_on: "upload-pipeline"

but my output from my two tasks were this

Running commands
$ SELECTED_ENV=$(buildkite-agent meta-data get environment-name)
cat << EOF > .buildkite/dynamic-pipeline.yml
env:
  SELECTED_ENV: "${SELECTED_ENV}"
EOF
buildkite-agent pipeline upload .buildkite/dynamic-pipeline.yml
2025-08-04 05:22:49 INFO   Reading pipeline config from ".buildkite/dynamic-pipeline.yml"
2025-08-04 05:22:49 INFO   Updating BUILDKITE_COMMIT to "blah"
2025-08-04 05:22:49 WARN   There were some issues with the pipeline input - pipeline upload will proceed, but might not succeed:
pipeline contains no steps
2025-08-04 05:22:50 INFO   Successfully uploaded and parsed pipeline config
$ echo "Environment set to $SELECTED_ENV"
Environment set to

sorry, but is there something else i’m missing?

Hi @matthewrollitt ,

You should be able to retrieve your input key via the buildkite-agent meta-data get “input-key” syntax. But when referring to them at runtime, ensure to escape the environment variable using $$ or \$

steps:
  - input: "Select Target Environment"
    key: "environment-selection"
    prompt: "Choose target environment for deployment"
    fields:
      - select: "Environment"
        key: "environment-name"
        required: true
        options:
          - label: "Test Environment"
            value: "test"
          - label: "Staging Environment"
            value: "staging"
        default: "test"
  - name: ":terraform: Infra Plan"
    depends_on: "environment-selection" 
    command: | 
      SELECTED_ENV="$(buildkite-agent meta-data get environment-name)"
      echo "terraform/infra/environments/$$SELECTED_ENV"

Alternatively, you can also directly use the the buildkite-agent meta-data get command directly:

  - name: ":terraform: Infra Plan"
    depends_on: “environment-selection”
    command:  echo “terraform/infra/environments/$$(buildkite-agent meta-data get environment-name)” 

Hope this helps!

Cheers!