withCredentials插件-检索到的多个凭证的访问值



我想弄清楚如何访问在Jenkins管道中使用withCredentials检索的多个凭证的值。

这是我目前正在处理的:

def credentialsList = []
def credentials = readJSON text: env.E2E_CREDENTIALS // ['credId-1', 'credId-2']
credentials.each { credential ->
credentialsList << [$class: 'StringBinding', credentialsId: credential, variable: credentialPwd]
}
withCredentials(credentialsList) {
// How do I access the retrieved credentialPwd values here?
}

我不确定如何访问结果列表,是否有一种方法可以迭代已获取的内容?

它们将被注册为环境变量,因此您需要想出一种唯一地注册它们的方法。下面是在数组中使用凭据索引的示例:

def credentialsList = []
def credentials = readJSON text: env.E2E_CREDENTIALS // ['credId-1', 'credId-2']
credentials.each { credential ->
def index = credentials.findIndexOf { it == [credential] }
credentialsList << [$class: 'StringBinding', credentialsId: credential, variable: "CREDENTIAL$index"]
}
withCredentials(credentialsList) {
sh "echo my variable $CREDENTIAL0"
sh "echo my variable $CREDENTIAL1"
}

最新更新