带有凭据的 Active Choices 参数

Sim*_*eth 3 jenkins jenkins-groovy

我正在尝试访问存储在 Jenkins 中的凭据,而不必在脚本本身中对它们进行硬编码。

#!/usr/bin/env groovy
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'GroovyAWSScMgr', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
    return ["${env.AWS_ACCESS_KEY_ID}"]
}
Run Code Online (Sandbox Code Playgroud)

我试过了:

return [AWS_ACCESS_KEY_ID]

return [env.AWS_ACCESS_KEY_ID]

return ["${env.AWS_ACCESS_KEY_ID}"]

return ["${env.AWS_ACCESS_KEY_ID}"]

结果继续为NULL

小智 10

你可以试试这个:

import jenkins.model.*

credentialsId = 'GroovyAWSScMgr'

def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
  com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class, Jenkins.instance, null, null ).find{
    it.id == credentialsId}

return [creds.username]
Run Code Online (Sandbox Code Playgroud)

您可以在脚本中使用creds.usernamecreds.password

我不确定它是否安全。