且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取Jenkinsfile(Groovy)中给定文件夹中的文件名列表

更新时间:2022-06-07 22:27:42

您不能真正使用new File和常规的Groovy/Java方式遍历文件系统.默认情况下,对呼叫进行了安全检查(请参阅 JENKINS-38131 ),并且不会甚至由于Jenkins Pipelines如何执行您的管道代码而正常工作.

You can't really make use of the new File and normal Groovy/Java ways to traverse file systems. The call is security checked by default (see JENKINS-38131) and won't even generally work because of how Jenkins Pipelines executes your pipeline code.

您可以使用 findFiles 步骤" rel ="noreferrer"> Pipeline Utility步骤插件.它返回一个

One way you could do this would be to use the findFiles step from the Pipeline Utility Steps plugin. It returns a FileWrapper[] which can be inspected/used for other purposes.

node {
  // ... check out code, whatever
  final foundFiles = findFiles(glob: 'dockerfiles/**/*')
  // do things with FileWrapper[]
}

另一种选择是掏出外壳并捕获标准件:

Another option is to shell out and capture the standard out:

node {
  // ... check out code, whatever
  final foundFiles = sh(script: 'ls -1 dockerfiles', returnStdout: true).split()
  // Do stuff with filenames
}