【问题标题】:Inject passwords to the build as environment variables将密码作为环境变量注入构建
【发布时间】:2015-06-10 18:31:12
【问题描述】:

我正在尝试通过“构建环境”部分中的选项在 jenkins 中设置密码,该选项可在我的测试中用于获取密码并使用它。这是我在“将密码作为环境变量注入构建”中检查的选项。问题是一旦种子作业运行,我就会失去这些值。因此,种子作业运行后,我添加的值正在消失。有人遇到过这个问题吗?如何使其永久化,以便每次我都可以在测试代码中检索这些密码?

【问题讨论】:

    标签: jenkins jenkins-plugins jenkins-job-dsl


    【解决方案1】:

    运行种子作业后,对生成作业的所有手动更改都将丢失。这是 Job DSL 插件的预期行为。

    要在作业 DSL 生成的作业中使用密码,请使用 Credentials plugin 在 Jenkins 中存储密码(或任何机密)。然后使用Credentials Binding plugin 将密码映射到作业中的环境变量。查看 Job DSL wiki 以获取 example

    【讨论】:

    • 感谢您的回复 daspilker。我知道我们已经安装了凭据插件。上述过程涉及将 pwd 保存在代码中?我唯一的尝试是不要在代码中保持密码打开。
    • Credentials Binding 插件与Credentials 插件不同,代码中不需要保存密码,只需要保存凭证ID。
    • @JesseGlick,我们使用的是 Job DSL 1.27,所以我无法使用内置的凭据绑定调用。所以我试图通过“配置”块来编写它。我得到“没有这样的财产”例外。 groovy.lang.MissingPropertyException:没有这样的属性:类的usernameVariable:com.tgt.esv.jenkins.job.apis.test.GrailsFunctionalTestJob.create_closure1_closure8(GrailsFunctionalTestJob.groovy:115)的javaposse.jobdsl.dsl.helpers.wrapper.WrapperContext
    • 这是我得到“没有这样的属性”的代码。 if (isCredentailsBinding) { configure { project -> project / 'buildWrappers' / 'org.jenkinsci.plugins.credentialsbinding.impl.UsernamePasswordMultiBinding' { credentialsId = credId usernameVariable= userNameVar passwordVariable = passwordVar
    • 听起来你需要问一个新问题,关于 Job DSL 的使用。
    【解决方案2】:

    @daspilker,@JesseGlick,非常感谢您的回复。它帮助我在 Jenkins 中编写了我的第一个配置块。提及我的行为,以便其他面临同样问题的人可能会有所帮助。

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

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

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

    【讨论】:

      猜你喜欢
      • 2014-02-06
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      • 2017-07-30
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多