【问题标题】:Jenkins : use withCredentials in global environment sectionJenkins:在全局环境部分使用 withCredentials
【发布时间】:2018-01-10 08:05:00
【问题描述】:

我有一个包含多个阶段的 Jenkins 管道,它们都需要相同的环境变量,我这样运行:

script {
    withCredentials([usernamePassword(credentialsId: 'COMPOSER_REPO_MAGENTO', passwordVariable: 'MAGE_REPO_PASS', usernameVariable: 'MAGE_REPO_USER')]) {
        def composerAuth = """{
            "http-basic": {
                "repo.magento.com": {
                    "username": "${MAGE_REPO_USER}",
                    "password": "${MAGE_REPO_PASS}"
                }
            }
        }""";
        // do some stuff here that uses composerAuth
    }
}

我不想每次都重新声明composerAuth,所以我想将凭据存储在一个全局变量中,这样我可以执行以下操作:

script {
    // do some stuff here that uses global set composerAuth
}

我试过把它放在环境部分:

environment {
    DOCKER_IMAGE_NAME = "magento2_website_sibo"
    withCredentials([usernamePassword(credentialsId: 'COMPOSER_REPO_MAGENTO', passwordVariable: 'MAGE_REPO_PASS', usernameVariable: 'MAGE_REPO_USER')]) {
        COMPOSER_AUTH = """{
            "http-basic": {
                "repo.magento.com": {
                    "username": "${MAGE_REPO_USER}",
                    "password": "${MAGE_REPO_PASS}"
                }
            }
        }""";
    }
}

但是(像我这样时髦的菜鸟)这行不通。那么使用凭据设置全局可访问变量但只需声明一次的最佳方法是什么?

【问题讨论】:

  • 我尝试了该帖子中的建议,但不起作用(至少,不适合我)。这里的问题是我有来自不同插件的凭据,需要一种方法将这些凭据存储在全局可访问的(环境?)变量中。
  • 好的,在这种情况下可能没问题:)
  • @Giel Berkers 鉴于您正在尝试发出 HTTP 请求,您是否尝试过使用 HTTPRequest 插件?如果是这种情况,那么有一种方法可以全局声明用户名和密码,并在没有“withCredentials”块的情况下使用它们。

标签: jenkins groovy jenkins-pipeline credentials


【解决方案1】:

经过大量搜索(和挣扎),我想出了一个简单的解决方法:

正如 Handling Credentials 的 jenkins 文档中更好地解释的那样,当将 usernamePassword 类型的凭据注入名为 VAR_NAME 的环境变量中时,jenkins 会自动生成另外两个以结尾的变量_USR_PSW 分别用于 usernameVariablepasswordVariable 参数。

我所做的是将来自 USR 和 PSW 新变量的值注入我的变量。

在@Giel Berkers 的情况下,应该是这样的:

environment {
    DOCKER_IMAGE_NAME = "magento2_website_sibo"
    COMPOSER_REPO_MAGENTO_CREDENTIAL = credentials('COMPOSER_REPO_MAGENTO')
    COMPOSER_AUTH = """{
        "http-basic": {
            "repo.magento.com": {
                "username": "${COMPOSER_REPO_MAGENTO_CREDENTIAL_USR}",
                "password": "${COMPOSER_REPO_MAGENTO_CREDENTIAL_PSW}"
            }
        }
    }""";
}

【讨论】:

    【解决方案2】:

    您可以使用environment 部分的credentials 辅助方法。对于“用户名和密码”类型的凭据,它分配了 2 个额外的环境变量。示例:

    environment {
      MAGE_REPO_CREDENTIALS = credentials('COMPOSER_REPO_MAGENTO')
      COMPOSER_AUTH = """{
          "http-basic": {
              "repo.magento.com": {
                  "username": "${env.MAGE_REPO_CREDENTIALS_USR}",
                  "password": "${env.MAGE_REPO_CREDENTIALS_PSW}"
              }
          }
      }"""
    }
    

    Read more

    【讨论】:

      【解决方案3】:

      我发现了这个,它很有帮助: 来源:https://wiki.jenkins.io/display/JENKINS/Credentials+Binding+Plugin

         // Basic example
      withCredentials([usernamePassword(credentialsId: 'amazon',
                           usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
          //available as an env variable, but will be masked if you try to print it out any which way
          sh 'echo $PASSWORD'
          echo "${env.USERNAME}"
      }
      
      // You can also request multiple credentials in a single call
      withCredentials([usernamePassword(credentialsId: 'amazon',
                           usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD'),
                       string(credentialsId: 'slack-url',
                           variable: 'SLACK_URL'),]) {
          sh 'echo $PASSWORD'
          echo "${env.SLACK_URL}"
      }
      
      // Older code might not use the new syntax (usernamePassword, string, ...) yet, and directly call the class:
      withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'amazon',
                        usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
          //available as an env variable, but will be masked if you try to print it out any which way
          sh 'echo $PASSWORD'
          echo "${env.USERNAME}"
      }
      

      【讨论】:

      • 无方法签名:javaposse.jobdsl.dsl.helpers.step.StepContext.usernamePassword() 适用于参数类型:(java.util.LinkedHashMap)
      【解决方案4】:

      你可以这样做

      pipeline {
          agent any
          stages {
              stage('first') {
                  steps {
                      script {
                          withCredentials([usernamePassword(credentialsId: 'COMPOSER_REPO_MAGENTO', passwordVariable: 'MAGE_REPO_PASS', usernameVariable: 'MAGE_REPO_USER')]) {
                              def user = env.MAGE_REPO_USER
                              def password = env.MAGE_REPO_PASS
                              //Initializing a global variable. Notice there is no def here 
                              composerAuth = """{
                                  "http-basic": {
                                      "repo.magento.com": {
                                          "username": "${user}",
                                          "password": "${password}"
                                      }
                                  }
                              }"""
                          }
                      }
                  }
              }
              stage('second') {
                  steps {
                      script {
                          println composerAuth
                      }
                  }
              }
          }
      }
      

      【讨论】:

      • Artem 的解决方案,由 Leonardo 解释,是对“如何在全局环境部分中使用 withCredentials”问题的更优雅的解决方案。此外,如果变量对我的管道来说是全局的,我更喜欢使用 @Field 或 environment {} 定义我的变量
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-25
      • 2019-01-04
      • 2018-01-26
      • 2018-10-19
      • 2017-05-06
      • 1970-01-01
      相关资源
      最近更新 更多