2017-08-06 75 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注释的方法中。