问题描述
我试图在我的 Jenkins 构建中隐藏密码.
I'm trying to mask a password in my Jenkins build.
我一直在尝试 掩码密码插件.
然而,这似乎不适用于我的 Jenkins 管道脚本,因为如果我定义了密码 PASSWD1
然后我在脚本中使用它像这样 ${PASSWD1}
,我得到:
However, this doesn't seem to work with my Jenkins pipeline script, because if I define the password PASSWD1
and then I use it in the script like this ${PASSWD1}
, I am getting:
No such DSL method '$' found among steps [addToClasspath, ansiColor, ansiblePlaybook, ....]
如果我使用env.PASSWD1
,那么它的值将被解析为null
.
If I use env.PASSWD1
, then its value will be resolved to null
.
那么我应该如何在 Jenkins 管道脚本中屏蔽密码?
So how should I mask a password in a Jenkins pipeline script?
推荐答案
最简单的方法是使用 Credentials Plugin.
The simplest way would be to use the Credentials Plugin.
您可以在那里定义不同类型的凭据,无论是单个密码(秘密文本")、文件还是用户名/密码组合.此外,其他插件可以提供其他类型的凭据.
There you can define different types of credential, whether it's a single password ("secret text"), or a file, or a username/password combination. Plus other plugins can contribute other types of credentials.
创建凭据时(通过 Jenkins 主页面上的凭据链接),请确保设置了ID".在下面的示例中,我将其称为 my-pass
.如果你不设置它,它仍然可以工作,Jenkins 会为你分配一个不透明的 UUID.
When you create a credential (via the Credentials link on the main Jenkins page), make sure you set an "ID". In the example below, I've called it my-pass
. If you don't set it, it will still work, Jenkins will allocate an opaque UUID for you instead.
在任何情况下,您都可以使用 轻松生成所需的语法片段生成器.
In any case, you can easily generate the required syntax with the snippet generator.
withCredentials([string(credentialsId: 'my-pass', variable: 'PW1')]) {
echo "My password is '${PW1}'!"
}
这将使密码仅在此块内的给定变量中可用.如果您尝试打印密码,就像我在这里所做的那样,它将被屏蔽.
This will make the password available in the given variable only within this block. If you attempt to print the password, like I do here, it will be masked.
这篇关于在 Jenkins 管道脚本中隐藏密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!