[英]Replace variable value in build.xml using Jenkins file pipeline script
[英]Jenkins pipeline script to write value of variable in file
我正在從憑證存儲中提取用戶名/密碼,並將其分配給一個變量。 我想將此值寫入文件中。 我正在使用以下代碼:
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}"
由於某種原因,管道 groovy 腳本與單引號(')有一些問題
試試 Sriram 提到的雙引號。 他是正確的,管道 groovy 腳本確實存在單引號問題。
有時還發現雙引號也可能是一個問題,並且必須將它們轉義(\“)。看看你怎么做!
我還有另一個解決方案,您可以將進入文件的文本定義為變量,並將 writeFile arguments 括在括號中,見下文:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.