Setting plugin properties from environment

This probably applies other things too, but currently I’m wondering if this is possible.

I’m trying to use the docker-login plugin GitHub - buildkite-plugins/docker-login-buildkite-plugin: 🐳 Login to Docker registries
and I want to supply the username from an environment variable.

The environment variable is defined on the agent.

steps:
  - command: ./run_build.sh
    plugins:
      - docker-login#v2.0.1:
          username: $MY_USER_FROM_ENV
          password-env: MY_DOCKER_LOGIN_PASSWORD

Can this be done?

(In general I often struggle when resolving environment variables in pipelines)

1 Like

Hi @thomas,

Welcome to the community!
Apologies for the delayed response.

You can do what you posted, or you can use the environment variable BUILDKITE_PLUGIN_DOCKER_LOGIN_USERNAME defined in the plugin. For the latter, you shouldn’t use username on the plugin invocation. For example:

steps:
  - command: ./run_build.sh
    plugins:
      - docker-login#v2.0.1:
          password-env: MY_DOCKER_LOGIN_PASSWORD

You can find more information about environment variables in pipelines here.

Hope this helps!

Cheers,

Hi @paula, I think there is a misunderstanding here. I think @thomas was asking how you can use an environment variable on the parameters of a plugin. Which is actually not possible.

I’m trying to develop my own plugin right now and I am trying to pass in a parameter that contains an environment variable. Something like

    plugins:
      - slack:
          message: Finished step ${BUILDKITE_LABEL}

${BUILDKITE_LABEL} is actually not resolved, it’s just empty in the script of the plugin. I’ve also tried $${BUILDKITE_LABEL}, in which case it just resolves to the string “${BUILDKITE_LABEL}”, but not the actual value of the env variable.

Is there a way to do this?

1 Like

If anyone finds this:
I am currently using this rather cumbersome line in my hook:

MESSAGE="$(envsubst < <(echo "${BUILDKITE_PLUGIN_SLACK_MESSAGE}"))"

That works because environment variables like ${BUILDKITE_LABEL} are actually available during execution of the hooks.
But for some reason they don’t get evaluated if they are just used as value on a plugin property.

Hi @jonnylangefeld! :wave:

Hmm, :thinking: This sounds like a different issue than the original one in this thread.

If you use the single dollar sign, values will be interpolated at pipeline upload time. If you want to interpolate when your plugin runs, then your plugins need to handle that

Here are some docs about environment variables, interpolation, and escaping: Environment Variables | Buildkite Documentation

Hope this helps!

Cheers

Agree with @jonnylangefeld. The author intended to dynamically read an (environment) variable. Keen to get a solution as well. Tried your proposed:

MESSAGE="$(envsubst < <(echo "${ENVIRONMENT}"))"

but it fails with Pipeline parsing of "pipeline.yaml" failed (Failed to parse pipeline.yaml: line 103: did not find expected key). I am trying to use an environment variable defined in env which should exist and be set.