如何在 Jenkins 管道的 AWS 凭据参数中使用用户范围的凭据?
How to use user-scoped credentials in an AWS credentials parameter in Jenkins pipeline?
我正在尝试访问通过管道作业中的参数提供的 AWS 凭证。
我有一个管道作业,我在其中使用如下定义的 AWS 凭据参数:
credentials (
credentialType: 'com.cloudbees.jenkins.plugins.awscredentials.AWSCredentialsImpl',
defaultValue: 'jenkins-deploy-proj',
description: '''
My description
''',
name: 'AWS_ACCOUNT'
)
我是通过 Jenkins 中的“管道语法 > 声明式指令生成器 > 参数”得到的 UI。
我稍后需要在工作中访问这些凭据。 From other discussions 看来我应该使用 withCredentials
块来访问凭据,所以我尝试了这个
script {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY',
credentialsId: "${params.AWS_ACCOUNT}"
]]) {
sh 'bash myscript.sh'
}
}
我是通过 Jenkins 中的“管道语法 > 代码片段生成器 > withCredentials”获得的 UI。
管道使用默认凭据(所有用户都可以访问)运行良好,但是当我尝试使用我的个人凭据(仍在全局域中)时,我收到 Jenkins 的错误消息,告诉我凭据不不存在:
ERROR: Could not find credentials entry with ID '557ff283-70f3-402b-b065-fb4c9f28305e'
我可以在像这样配置的其他(非管道)Jenkins 作业中使用这些相同的凭据作为参数,它们工作正常:
我确实采取了额外的步骤,通过创建一个新的凭据对象来确保问题不仅仅出在那个凭据对象上,但我遇到了同样的 Could not find credentials entry with ID
错误。
我能够联系到 CloudBees support (they provide tools and services on top of Jenkins) who mentioned a change introduced in JENKINS-58170 which allows credentials to be accessed using the name of the credentials parameter as the id. This is the solution mentioned in this CloudBees article about using user scoped credentials in pipeline jobs。在这种情况下,解决方案看起来像:
script {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY',
credentialsId: 'AWS_ACCOUNT'
]]) {
sh 'bash myscript.sh'
}
}
这可能适用于许多 Jenkins 用户。
然而,这些改进出现在 the credentials plugin 的 2.3 版中。由于我们是 运行 旧版本的插件,因此此功能不可用。
相反,我们不得不使用 JENKINS-58170 中提到的“特殊语法”:credentialsId: '${credentialsParameterName}'
。请注意,单引号在这里很重要!来自票务:
user-scoped credentials are currently only looked up if the credential id is provided using the former template syntax
即'{userScopedCredsParameterName}'
语法。
所以最终的工作流水线定义如下所示:
script {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY',
credentialsId: '${AWS_ACCOUNT}'
]]) {
sh 'bash myscript.sh'
}
}
我正在尝试访问通过管道作业中的参数提供的 AWS 凭证。
我有一个管道作业,我在其中使用如下定义的 AWS 凭据参数:
credentials (
credentialType: 'com.cloudbees.jenkins.plugins.awscredentials.AWSCredentialsImpl',
defaultValue: 'jenkins-deploy-proj',
description: '''
My description
''',
name: 'AWS_ACCOUNT'
)
我是通过 Jenkins 中的“管道语法 > 声明式指令生成器 > 参数”得到的 UI。
我稍后需要在工作中访问这些凭据。 From other discussions 看来我应该使用 withCredentials
块来访问凭据,所以我尝试了这个
script {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY',
credentialsId: "${params.AWS_ACCOUNT}"
]]) {
sh 'bash myscript.sh'
}
}
我是通过 Jenkins 中的“管道语法 > 代码片段生成器 > withCredentials”获得的 UI。
管道使用默认凭据(所有用户都可以访问)运行良好,但是当我尝试使用我的个人凭据(仍在全局域中)时,我收到 Jenkins 的错误消息,告诉我凭据不不存在:
ERROR: Could not find credentials entry with ID '557ff283-70f3-402b-b065-fb4c9f28305e'
我可以在像这样配置的其他(非管道)Jenkins 作业中使用这些相同的凭据作为参数,它们工作正常:
我确实采取了额外的步骤,通过创建一个新的凭据对象来确保问题不仅仅出在那个凭据对象上,但我遇到了同样的 Could not find credentials entry with ID
错误。
我能够联系到 CloudBees support (they provide tools and services on top of Jenkins) who mentioned a change introduced in JENKINS-58170 which allows credentials to be accessed using the name of the credentials parameter as the id. This is the solution mentioned in this CloudBees article about using user scoped credentials in pipeline jobs。在这种情况下,解决方案看起来像:
script {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY',
credentialsId: 'AWS_ACCOUNT'
]]) {
sh 'bash myscript.sh'
}
}
这可能适用于许多 Jenkins 用户。
然而,这些改进出现在 the credentials plugin 的 2.3 版中。由于我们是 运行 旧版本的插件,因此此功能不可用。
相反,我们不得不使用 JENKINS-58170 中提到的“特殊语法”:credentialsId: '${credentialsParameterName}'
。请注意,单引号在这里很重要!来自票务:
user-scoped credentials are currently only looked up if the credential id is provided using the former template syntax
即'{userScopedCredsParameterName}'
语法。
所以最终的工作流水线定义如下所示:
script {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY',
credentialsId: '${AWS_ACCOUNT}'
]]) {
sh 'bash myscript.sh'
}
}