【问题标题】:How to use multiple credentials in withCredentials in Jenkins Pipeline如何在 Jenkins Pipeline 的 withCredentials 中使用多个凭据
【发布时间】:2018-05-08 13:55:24
【问题描述】:

我的声明式 jenkins 管道中有以下步骤: 我使用 libraryResource 创建来自我的resources/ 文件夹的脚本。此脚本包含我的autobuild 用户和一些admintest 用户的凭据。

stage('Build1') {
                steps {
                    node{
                        def script = libraryResource 'tests/test.sh'
                        writeFile file: 'script.sh', text: script
                        sh 'chmod +x script.sh'
                        withCredentials([usernamePassword(credentialsId: xxx, usernameVariable: 'AUTOBUILD_USER', passwordVariable: 'AUTOBUILD_PASSWD')]){
                            sh './script.sh "
                        }

                    }

                }   

这很好用。我可以使用我的autobuild 用户。现在我正在寻找最好的方法来包含我的admintest 用户的凭据。 我必须用第二个withCredentials 部分“嵌套”它还是可以再次添加一个usernamePassword“数组”?

【问题讨论】:

    标签: jenkins jenkins-pipeline credentials


    【解决方案1】:

    当然,您可以使用一个 withCredentials 块将多个凭据分配给不同的变量。

    withCredentials([
        usernamePassword(credentialsId: credsId1, usernameVariable: 'USER1', passwordVariable: 'PASS1'),
        usernamePassword(credentialsId: credsId2, usernameVariable: 'USER2', passwordVariable: 'PASS2')
    ]){
        //...
    }
    

    【讨论】:

    • 如何用 Groovy 做到这一点?
    • @meshfields 这是 Groovy
    • @SteveK stage('stage 2') { steps{ withCredentials([usernamePassword(usernameVariable: 'user_1', passwordVariable: 'password_1', credentialsId: 'id_1'), usernamePassword(usernameVariable: 'username_2 ', passwordVariable: 'password_2', credentialsId: 'id_2')]){ sshagent(credentials : ['jenkins_ssh_user_key']) { sh """ """ } } } }
    【解决方案2】:

    此外,您可以将其与 $class 一起使用

                        withCredentials([[
                          $class: 'AmazonWebServicesCredentialsBinding',
                          credentialsId: 'awsID',
                          accessKeyVariable: 'AWS_ACCESS_KEY_ID',
                          secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'],
    
                        [$class: 'UsernamePasswordMultiBinding',
                          credentialsId: 'myID',
                          usernameVariable: 'USR',
                          passwordVariable: 'PWD']])
    

    【讨论】:

    • 字符串文本的任何类
    猜你喜欢
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多