Inject passwords to the build as environment variables

24.8k views Asked by At

I am trying to set a password in jenkins through an option in 'build environments' section which can be used in my test to get the password and use it. This is the option i am checking in "Inject passwords to the build as environment variables". Problem is i am loosing these values once the seed job runs. So my values added are disappearing after seed job runs. Did anybody faced this problem? How to make it permanent so every time i can retrieve those pwds in my test code?

2

There are 2 answers

0
Venkat On BEST ANSWER

@daspilker , @JesseGlick, Thank you very much for your responses. It helped me to write my first configure block in Jenkins. Mentioning my actions so for others facing same problem might help.

Since we are using Job DSL 1.27, I am not able to use credentials-Binding directly. So created a configure block and injected required variables through my .groovy script.

Note: You need to take the converted value for 'credentialsId' from '*****/job/config.xml if you are getting "credentialsId did not found" error.

static def credentialsBinding = { String userNameVar, String passwordVar, String credId, wrapperContext ->
    def nodeBuilder = new NodeBuilder()
    wrapperContext.wrapperNodes << nodeBuilder.'org.jenkinsci.plugins.credentialsbinding.impl.SecretBuildWrapper'(plugin: "[email protected]") {
        bindings {
            'org.jenkinsci.plugins.credentialsbinding.impl.UsernamePasswordMultiBinding' {
                usernameVariable userNameVar
                passwordVariable passwordVar
                credentialsId credId
            }
        }
    }
}
5
daspilker On

After running the seed job, all manual changes to a generated job will be lost. That's intended behavior of the Job DSL plugin.

To use passwords in a Job DSL generated job, use the Credentials plugin to store the password (or any secret) in Jenkins. Then use the Credentials Binding plugin to map the password to an environment variable in the job. Have a look at the Job DSL wiki for an example.