6
我想用java.nio.file。*中的方法在Jenkins管道中執行一些基本的文件操作。無論存在代碼的節點塊如何,代碼都會在主節點上執行。在流水線中,我已經驗證了各種節點塊是正確的 - 它們唯一地標識特定的節點。但是,pathExists(以及移動,複製或刪除文件的其他代碼)始終在主節點上執行。任何想法發生了什麼或如何解決它?Jenkins管道和java.nio.file。*方法問題
import java.nio.file.*
String slavePath = 'C:\\Something\\only\\on\\slave\\node'
String masterPath = 'D:\\Something\\only\\on\\master\\node'
def pathExists (String pathName)
{
def myPath = new File(pathName)
return (myPath.exists())
}
stage('One')
{
node ('slave')
{
bat returnStatus: true, script: 'set'
println (pathExists(slavePath)) // Should be true but is false.
println (pathExists(masterPath)) // Should be false but is true.
}
node ('master')
{
bat returnStatus: true, script: 'set'
println (pathExists(slavePath)) // false
println (pathExists(masterPath)) // true
}
}
我錯過了在文檔中。因此,如果我理解正確,在Windows上的工作空間內部和外部的主節點和從節點上使用單一方法會涉及使用Groovy運行bat文件來返回答案,可能通過運行PowerShell Test-File? – gaccardo
嗯我沒有明白你的意思。是不是與java.io.File問題無關的另一個問題?我在答案中增加了一個例子,它會按照您的預期運行以防萬一。 – arasio
根據[文檔](https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#code-fileexists-code-verify-if-file-exists-in-workspace),僅fileExists適用於作業工作區中的文件。當我最初嘗試時,情況就是如此。即使文件不在工作區中,運行示例也能正常工作。我已經用Jenkins 2.24和2.28驗證了這一點。 – gaccardo