我正在使用 gradle 和 bash 腳本在 docker 映像中構建多個 android 應用程式。該腳本由運行 docker 映像的 jenkins 觸發。在 bash 腳本中,我收集有關構建成功的資訊。我想將該資訊傳遞給 jenkinsfile 的 groovy 腳本。我試圖在docker容器中創建一個txt檔案,但是jenkinsfile中的groovy腳本找不到那個檔案。這是我的 jenkinsfile 的 groovy 腳本:
script {
try {
sh script:'''
#!/bin/bash
./jenkins.sh
'''
} catch(e){
currentBuild.result = "FAILURE"
} finally {
String buildResults = null
try {
def pathToBuildResults="[...]/buildResults.txt"
buildResults = readFile "${pathToBuildResults}"
} catch(e) {
buildResults = "error receiving build results. Error: " e.toString()
}
}
}
在我的 jenkins.sh bash 腳本中,我執行以下操作:
[...]
buildResults =" $appName: Build Failed!" //this is done for several apps
echo "$buildResults" | cat > $pathToBuildResults //this works I checked, if the file is created
[...]
該檔案已創建,但 groovy 找不到它。我認為原因是 jenkins 腳本不在 docker 容器內運行。
如何在我的 groovy jenkins 腳本中訪問 bash 腳本的字串 buildResults?
uj5u.com熱心網友回復:
為了避免需要讀取結果檔案,您可以選擇修改jenkins.sh腳本以將結果列印到輸出而不是將它們寫入檔案,然后使用該sh步驟捕獲該輸出并使用它而不是檔案。
就像是:
script {
try {
String buildResults = sh returnStdout: true, script:'''
#!/bin/bash
./jenkins.sh
'''
// You now have the output of jenkins.sh inside the buildResults parameter
} catch(e){
currentBuild.result = "FAILURE"
}
}
這樣您就無需處理輸出檔案并直接獲得您需要的結果,然后您可以根據需要對其進行決議和使用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/314546.html
上一篇:檢查命令輸出中是否存在字串
下一篇:Shell:重命名檔案
