位于/opt/apps/workspace/build32/target/site/result.htmlLinux 作業程式節點上的檔案。構建在/opt/apps/workspace/build32檔案夾下運行。下面是我正在使用的代碼片段。
if (fileExists("/opt/apps/workspace/build32/target/site/result.html")) {
echo "result.html file exist"
}
else {
echo " File does not exist"
}
def file = new File("/opt/apps/workspace/build32/target/site/result.html").collect{it}
def index = file.findIndexOf{ it ==~ /.*Result.*/ }
echo "file[index]"
輸出:
Running in /opt/apps/workspace/build32.
[pipeline] result.html file exist
[pipeline] build status UNSTABLE build message there was an error on stage Test, result.html (No such file or directory)
uj5u.com熱心網友回復:
問題
您需要注意,運行任意 Groovy(或 Java)代碼總是在您的 Jenkins master 上執行,無論哪個作業節點在您的管道中執行代碼。Jenkins Pipeline 步驟是此規則的一個例外,它們在當前作業節點上執行。因此:
fileExists("/opt/apps/workspace/build32/target/site/result.html")
列印預期輸出,因為fileExists它是標準 Jenkins 流水線步驟,因此它在作業節點上執行,該作業節點在其作業區中創建檔案。值得一提的是,管道步驟可以在steps { }塊內或script { }宣告性管道的塊內使用,也可以直接在腳本化的管道代碼中使用。
你打電話的時候:
def file = new File("/opt/apps/workspace/build32/target/site/result.html").collect{it}
您收到錯誤是因為new File(...)在 Jenkins 主節點上執行,并且其作業區不包含在作業節點的作業區中創建的檔案。
解決方案
您可以使用readFile管道步驟而不是執行任意 Groovy 代碼- 一個使用當前作業節點及其作業區讀取檔案的步驟。您可以使用相對路徑訪問作業空間內的檔案,因此應該讀取檔案的內容,如下所示:
def file = readFile(file: 'target/site/result.html')
請記住,該readFile步驟將檔案的內容作為單個回傳String,因此如果您想逐行處理它,您可能必須執行以下操作:
def lines = readFile(file: 'target/site/result.html').readLines()
以下代碼將從List<String>檔案中生成一行。根據您的 Jenkins 配置,該String.readLines()方法可能需要在您的 Jenkins 管道代碼中使用之前將其列入白名單。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/406662.html
標籤:
下一篇:無法單擊使用Pythonselenium并出現錯誤ElementNotInteractableException的按鈕
