buildkite-plugins / docker-buildkite-plugin

🐳📦 Run any build step in a Docker container
MIT License
113 stars 106 forks source link

input from block steps not passed in env #185

Closed secureoptions closed 3 years ago

secureoptions commented 3 years ago

Hi Team,

I'm trying to understand the best way to pass user input fields from block steps into the docker plugin. Something like this for example:

steps:
  - block: "Get import details"
    fields:
      - text: "What is your name?"
        key: username

  - label: "Print username from container"
     env: 
         USERNAME: $(buildkite-agent meta-data get "username")
    plugins:
        - docker#v3.5.0:
            image: ubuntu:latest
            propagate-environment: true
            propagate-aws-auth-tokens: true
            mount-ssh-agent: true

The problem I run into is that ${USERNAME} is interpreted literally as $(buildkite-agent meta-data get "username") instead of the actual username. Do you have any other suggestions for adding more vars from block input?

pzeballos commented 3 years ago

Hi @secureoptions!

This happens because the environment in the YML is being evaluated on upload, so the value of "username" is empty because the build doesn't actually run that input step yet. Meta-data cannot be interpolated directly from the pipeline.yml at runtime. The meta-data store can only be accessed from within a command step.

We suggest uploading the first part of that pipeline then have another pipeline upload steps that upload the remaining of the pipeline because then it would get the value. For example like this:

steps:
  - block: "Get import details"
    fields:
      - text: "What is your name?"
        key: myvar

  - command: >
      export USERNAME="$(buildkite-agent meta-data get 'myvar')" && echo '
      steps: 
        - env:
            USERNAME: \$USERNAME
          command: echo \\\$USERNAME' | buildkite-agent pipeline upload

    label: upload

I would recommend running the shell command with a script, but this would be the idea.

You can find more information about this here.

Cheers,