在Jenkins管道脚本中隐藏密码 [英] Hiding password in Jenkins pipeline script

查看:317
本文介绍了在Jenkins管道脚本中隐藏密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的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?

推荐答案

最简单的方法是使用凭据插件.

您可以在其中定义不同类型的凭据,无论是单个密码(秘密文本"),文件还是用户名/密码组合.另外,其他插件也可以提供其他类型的凭据.

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管道脚本中隐藏密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆