bash 如何使凭据可用于 jenkins 中的 shell 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41013222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:30:12  来源:igfitidea点击:

How to make credentials available to shell script in jenkins

bashshelljenkinscredentials

提问by blueFast

We have freestyle projects configured in Jenkins, running shell scripts as build steps. In some cases we need access to credentials for third-party services. We have solved this by providing the credentials as:

我们在 Jenkins 中配置了自由式项目,运行 shell 脚本作为构建步骤。在某些情况下,我们需要访问第三方服务的凭据。我们通过提供以下凭据解决了这个问题:

USER=theuser
PASS=thepass

in the project environment (Prepare an environment for the run -> Properties Content)

在项目环境 ( Prepare an environment for the run -> Properties Content)

This is working fine, but it is a bad solution because:

这工作正常,但这是一个糟糕的解决方案,因为:

  • credentials are not stored securely
  • they are visible to anybody having access to the project configuration
  • they leak in the Jenkins console
  • 凭据未安全存储
  • 任何有权访问项目配置的人都可以看到它们
  • 它们在 Jenkins 控制台中泄漏

We have researched a bit and found a promising plugin, but we do not know how to make the credentials managed by the plugin available to our scripts, ideally as environment variables.

我们进行了一些研究,发现了一个很有前途的插件,但我们不知道如何使插件管理的凭据可用于我们的脚本,最好是作为环境变量。

How can we access the credentials managed by the Jenkins plugin from a script?

我们如何从脚本访问 Jenkins 插件管理的凭据?

回答by MaTePe

We are using this one: https://wiki.jenkins-ci.org/display/JENKINS/Mask+Passwords+Plugin

我们正在使用这个:https: //wiki.jenkins-ci.org/display/JENKINS/Mask+Passwords+Plugin

then you specify the environment-variables you want the passwords to have

然后您指定您希望密码具有的环境变量

So if you specify something like this: enter image description here

因此,如果您指定如下内容: 在此处输入图片说明

You can use $PASSWORD in your shell later on.

稍后您可以在 shell 中使用 $PASSWORD。

回答by Amityo

withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: yourCredentialsId, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
    // user name and password will be stored in USERNAME and PASSWORD envs
}