问题描述
我正在从凭证存储中提取用户名/密码,并将其分配给变量。我想将此值写入文件中。 我正在使用以下代码:
for (creds in jenkinsCredentials) {
if(creds.id == "credential"){
def password = creds.password
println(creds.username)
println(password)
writeFile file: '\\Jenkins_Home\\workspace\\test\\test.properties',text: 'pass=${password}'
它打印密码的值。 但是它只写pass = $ {password},而不是密码的值。
如何在文件中写入变量的值?
解决方法
使用双引号(“)代替单引号('),如下所示:
writeFile file: "\\Jenkins_Home\\workspace\\test\\test.properties",text: "pass=${password}"
出于某种原因,管道常规脚本在单引号(')中存在一些问题
,尝试使用Sriram提到的双引号。他是正确的,管道常规脚本确实存在单引号问题。
有时还会发现双引号也是一个问题,必须将其转义(\“)。看看你怎么走!
我还有另一种解决方案,您可以将进入文件的文本定义为变量,并将writeFile参数括在方括号中,如下所示:
in jenkinsCredentials) {
if(creds.id == "credential"){
def password = creds.password
println(creds.username)
println(password)
def pass = "pass=" + password
writeFile (file: '\\Jenkins_Home\\workspace\\test\\test.properties',text: pass)