且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将密码作为环境变量注入到构建中

更新时间:2021-08-27 09:50:30

@daspilker,@JesseGlick,非常感谢您的答复.它帮助我用Jenkins编写了我的第一个configure块.提及我的行为,以便对其他面临相同问题的人有所帮助.

@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.

由于我们使用的是Job DSL 1.27,因此我无法直接使用凭据绑定.因此创建了一个configure块,并通过.groovy脚本注入了所需的变量.

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.

注意:如果收到找不到credentialsId"错误,则需要从'*****/job/config.xml中获取'credentialsId'的转换值.

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
            }
        }
    }
}