Inject passwords to the build as environment varia

2019-07-14 02:25发布

问题:

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?

回答1:

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.



回答2:

@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: "credentials-binding@1.4") {
        bindings {
            'org.jenkinsci.plugins.credentialsbinding.impl.UsernamePasswordMultiBinding' {
                usernameVariable userNameVar
                passwordVariable passwordVar
                credentialsId credId
            }
        }
    }
}