[英]How to integrate Jenkins pipeline jobs and pass dynamic variables using Groovy?
我想通過基於觸發作業的項目傳遞動態變量來使用 Groovy 集成 Jenkins 作業。 任何人都可以建議如何進行此操作嗎?
看起來您希望在兩個 jenkins 作業或同一 jenkins 作業的兩次運行之間保留數據。 在這兩種情況下,我都可以使用文件來做到這一點。 您可以使用寫入文件使用groovy
或重定向運算符(>)
來執行此操作,以僅使用 bash。
在第一份工作中,您可以像這樣寫入文件。
node {
// write to file
writeFile(file: 'variables.txt', text: 'myVar:value')
sh 'ls -l variables.txt'
}
在第二個工作中,您可以從該文件中讀取並在讀取后清空內容。
stage('read file contents') {
// read from the file
println readFile(file: 'variables.txt')
}
該文件可以位於文件系統上的任何位置。 在/tmp
文件夾中創建文件的示例如下。 您應該能夠通過復制粘貼來運行此管道。
node {
def fileName = "/tmp/hello.txt"
stage('Preparation') {
sh 'pwd & rm -rf *'
}
stage('write to file') {
writeFile(file: fileName, text: "myvar:hello", encoding: "UTF-8")
}
stage('read file contents') {
println readFile(file: fileName)
}
}
您還可以將此文件用作屬性文件並更新存在的屬性並附加不存在的屬性。 執行此操作的快速示例代碼如下所示。
node {
def fileName = "/tmp/hello.txt"
stage('Preparation') {
sh 'pwd & rm -rf *'
}
stage('write to file') {
writeFile(file: fileName, text: "myvar:hello", encoding: "UTF-8")
}
stage('read file contents') {
println readFile(file: fileName)
}
// Add property
stage('Add property') {
if (fileExists(fileName)) {
existingContents = readFile(fileName)
}
newProperty = "newvar:newValue"
writeFile(file: fileName, text: existingContents + "\n" + newProperty)
println readFile(file: fileName)
}
}
如果您想刪除它,您可以輕松刪除具有屬性的行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.