如何在Jenkins上采用男性常规方式来掩盖变量的输出,就像处理凭证一样? [英] How can I male groovy on Jenkins mask the output of a variable the same way it does for credentials?

查看:49
本文介绍了如何在Jenkins上采用男性常规方式来掩盖变量的输出,就像处理凭证一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Jenkins上是否有一种方法可以采用任意String变量(例如,对另一个服务的API调用的结果),并使Jenkins在控制台输出中对其进行掩盖,就像它自动处理从凭据管理器读取的值一样?

Is there a way in groovy on Jenkins to take an arbitrary String variable - say the result of an API call to another service - and make Jenkins mask it in console output as it does automatically for values read in from the Credentials Manager?

推荐答案

更新的解决方案: 要隐藏变量的输出,可以使用Mask Password Plugin

UPDATED SOLUTION: To hide the output of a variable you can use the Mask Password Plugin

这是一个例子:

String myPassword = 'toto'
node {
  println "my password is displayed: ${myPassword}"
  wrap([$class: 'MaskPasswordsBuildWrapper', varPasswordPairs: [[password: "${myPassword}", var: 'PASSWORD']]]) {
   println "my password is hidden by stars: ${myPassword}"
   sh 'echo "my password wont display: ${myPassword}"'
   sh "echo ${myPassword} > iCanUseHiddenPassword.txt"
  }
  // password was indeed saved into txt file
  sh 'cat iCanUseHiddenPassword.txt'
}

https://wiki.jenkins.io/display/JENKINS/Mask +密码+插件

带有正则表达式解决方案的原始答案:

ORIGINAL ANSWER with regex solution:

假设您要隐藏引号之间包含的密码,以下代码将输出My password is "****"

Let's say you want to hide a password contained between quotes, the following code will output My password is "****"

import java.util.regex.Pattern

String myInput = 'My password is "password1"'

def regex = Pattern.compile( /(?<=\")[a-z0-9]+(?=\")/, Pattern.DOTALL);

def matchList = myInput =~ regex

matchList.each { m ->
  myInput = myInput.replaceAll( m, '****')
}
println myInput

您需要用密码中允许的字符模式替换a-z0-9

you need to replace a-z0-9 by the pattern of allowed characters in your password

这篇关于如何在Jenkins上采用男性常规方式来掩盖变量的输出,就像处理凭证一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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