问题描述
我有一个詹金斯多分支管道作业,该作业使用Jenkinsfile
中的秘密值:
I have a Jenkins multi branch pipeline job that uses a secret value in Jenkinsfile
:
pipeline {
agent any
stages {
stage('Test') {
steps {
echo "DOCKER_REGISTRY_USER is ${env.DOCKER_REGISTRY_USER_NSV}"
}
}
}
}
机密值作为ID为DOCKER_REGISTRY_USER_NSV
的机密文本存储在凭据管理器中:
The secret value is stored in Credentials Manager as secret text with the ID DOCKER_REGISTRY_USER_NSV
:
如上所述,我正在尝试在Jenkinsfile中读取此值,但是我得到以下输出,为我的秘密打印出值null
:
I'm trying to read this value in Jenkinsfile as shown above but I get the following output that prints out the value null
for my secret:
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
DOCKER_REGISTRY_USER is null
[Pipeline] sh
我还尝试像这样在管道中引用机密文本:
I also tried referencing the secret text in my pipeline like this:
echo "DOCKER_REGISTRY_USER is ${DOCKER_REGISTRY_USER_NSV}"
但是运行Jenkins作业时出现此错误:
But then I get this error when running the Jenkins job:
groovy.lang.MissingPropertyException: No such property: DOCKER_REGISTRY_USER_NSV for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:264)
我认为我需要将该凭证绑定到该工作,但是我看不到针对多分支管道工作(如Freestyle或Pipeline作业)执行此操作的选项.
I think I need to bind that credential to the job but I don't see an option to do that for a Multi-Branch Pipeline job, the way you can for a Freestyle or Pipeline job.
如何在多分支管道作业中使用秘密凭证?
How can I use a secret credential in a Multi Branch Pipeline job?
推荐答案
您可以使用 credentials()辅助方法来存档您的目的.
You can use credentials() helper method to archive your purpose.
pipeline {
agent any
environment {
DOCKER_REGISTRY_USER = credentials('DOCKER_REGISTRY_USER_NSV')
// put the ID of credential as credentials()'s parameter.
}
stages {
stage('Test') {
steps {
echo "DOCKER_REGISTRY_USER is ${DOCKER_REGISTRY_USER}"
}
}
}
}
这篇关于使用凭证参数在多分支管道作业中从Jenkins凭证管理器读取问题难以解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!