【问题标题】:How to use jenkins plugin for username with password in dsl?如何在 dsl 中使用 jenkins 插件作为用户名和密码?
【发布时间】:2019-10-09 02:19:10
【问题描述】:

我是 jenkins 的新手,我正在尝试使用凭证插件在 dsl 中使用凭证

模板.xml

<com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl>
      <scope>GLOBAL</scope>
      <id>PROD</id>
      <description>prod credentials</description>
      <username>prod</username>
      <password>{{ encrypted_password_prod }}</password
</com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl>

我已将 jenkins 中的凭据定义为 username with password 。上面的加密值保存在ansible中。

我的问题是我应该如何在我的 dsl 中调用它们

Map credentials = [:]

credentialsBinding {
                    credentials.each { key, value ->
                        string("${key}", "${value}")
                    }


.credentials(["TF_VAR_username": "PROD" ,"TF_VAR_password" : "password_prod"])

错误:

22:11:16 致命:凭据“PROD”属于“用户名和密码”类型 'org.jenkinsci.plugins.plaincredentials.StringCredentials' 在哪里 预计

【问题讨论】:

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


    【解决方案1】:

    您可以将凭据放入 Jenkins 密钥库(Jenkins -> 凭据 -> 系统 -> 全局凭据 -> 添加凭据),然后使用 withCredentials 块在管道中引用它们,如下所示:

    node {
      withCredentials([usernameColonPassword(credentialsId: 'mylogin', variable: 'USERPASS')]) {
        sh '''
          set +x
          curl -u "$USERPASS" https://private.server/ > output
        '''
      }
    }
    

    更多信息在这里:https://jenkins.io/doc/pipeline/steps/credentials-binding/

    【讨论】:

      猜你喜欢
      • 2022-10-05
      • 2018-08-15
      • 1970-01-01
      • 2022-10-25
      • 1970-01-01
      • 2016-02-20
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多