2017-08-06 115 views
1

我試圖使用groovy從Jenkinsfile訪問文件系統。我下面這個SO線程的建議:在腳本審批 Recursive listing of all files matching a certain filetype in Groovy試圖從jenkinsfile訪問文件系統

我已經獲准進入

new java.io.File java.lang.String 
staticField groovy.io.FileType FILES 

new File('.').eachFileRecurse(FILES) { 

這裏所討論的更廣泛的代碼塊:

然而這行不顯示錯誤信息失敗

stage("Install") { 
    print "here a" 
    new File('.').eachFileRecurse(FILES) { 
    print "here c" 
    if(it.name.endsWith(".sh")) { 
     print "here d" 
     println it 
    } 
    } 
    print "here b" 

下面是從該部分輸出的控制檯:

[Pipeline] stage 
[Pipeline] { (Install) 
[Pipeline] echo 
here a 
[Pipeline] } 
[Pipeline] // stage 
[Pipeline] echo 
Email Recipients: [email protected], [email protected] 
[Pipeline] emailext 
messageContentType = text/html; charset=UTF-8 
Adding recipients from project recipient list 
Adding recipients from trigger recipient list 
Setting In-Reply-To since last build was not successful 

回答

1

更好地使用findFiles步驟,而不是使用Java API d文件。這被腳本安全列入白名單。

each由於Jenkins的CPS,循環不能正常工作。相反(如果你真的喜歡它舊的for循環),將其包裝在@NonCPS註釋的方法中。