ptierno ptierno - 2 months ago 18
Java Question

Jenkins: Export environment variables from a class extending SimpleBuildWrapper

I have a jenkins plugin that I wrote recently to source environment variables from secrets in a HashiCorp Vault.

For the most part everything works the way I expect, except when I expect a shell command to be aware of the environment variables that are set. I am setting the environment variables in the overridden setUp method of the SimpleBuildWrapper class.

Here is a relevant snippet of how I am setting the environment variables:

for (VaultSecretValue value : vaultSecret.getSecretValues()) {
context.env(value.getEnvVar(), values.get(value.getVaultKey()));
}


If I add a build step to echo the environment variable it returns the expected output, however, if I use an external command in the build step that expects the variable to be set there appears to be issues, something like the AWS cli:

aws --region us-east-1 ec2 describe-instances


If I take the same environment variable values and export them in a local terminal the result of the above command is what I expect.

My question is, when extending
SimpleBuildWrapper
, is there a step I'm missing to ensure the environment variables are exported to each subsequent shell command?

Please let me know if I can provide more information.

UPDATE

I have checked the output of
/usr/bin/env
and the environment variables are set. I think it may be an issue of how I am implementing the calls to
vault
it self that may be the issue. I will update this question accordingly either with an answer or more relevant information.

Answer

is there a step I'm missing to ensure the environment variables are exported to each subsequent shell command?

No. There is test coverage for this and it works.

You can run /usr/bin/env before aws … to see for sure what is getting set and narrow down the problem.