如何在 Jenkins Pipeline 的 withCredentials 中使用多个凭据

How to use multiple credentials in withCredentials in Jenkins Pipeline

我的声明性詹金斯管道中有以下步骤: 我使用 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 部分 'nest' 还是我可以再次添加 usernamePassword 'array'?

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

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

此外,您可以将其与 $class

一起使用
                    withCredentials([[
                      $class: 'AmazonWebServicesCredentialsBinding',
                      credentialsId: 'awsID',
                      accessKeyVariable: 'AWS_ACCESS_KEY_ID',
                      secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'],

                    [$class: 'UsernamePasswordMultiBinding',
                      credentialsId: 'myID',
                      usernameVariable: 'USR',
                      passwordVariable: 'PWD']])