在Jenkins管道上的工作区中列出文件 [英] List files on the workspace on a Jenkins Pipeline
问题描述
我正在尝试在Jenkins管道中列出工作区中的文件,以便可以使用它来生成适当的并行任务.
I'm trying to list files in the workspace in a Jenkins Pipeline, so that I can use that to produce appropriate parallel tasks.
虽然我可以简单地使用sh ls > files
并阅读它,但我希望我可以使用更复杂的逻辑进一步过滤的File
对象.实际上,Files.listFiles(FileFilter)
是理想的选择.
While I could simply use sh ls > files
and read that, I want File
objects which I can filter further with more complex logic. In fact, Files.listFiles(FileFilter)
would be ideal.
但是,我根本无法获得文件列表.首先,我不得不求助于一些奇怪的东西,以简单地找到构建的当前工作目录:
However, I can't get the list of files at all. First, I had to resort to some weird stuff to simply find out the current work directory for the build:
sh 'pwd > workspace'
workspace = readFile('workspace').trim()
现在我称之为检索文件列表:
Now I call this to retrieve the list of files:
@NonCPS
def getFiles(String baseDir) {
Arrays.asList(new File(baseDir).listFiles())
}
并在asList
上获得NPE,这意味着,根据我对javadoc的阅读,new File(baseDir)
不存在(或不是目录).
And get a NPE on asList
, which means, by my read of the javadoc, that new File(baseDir)
does not exist (or is not a directory).
我将其标记为@NonCPS
,因为它是Pipeline上Groovy闭包所必需的,我真的更希望在完整的Java< 1.8语法上使用它.
I'm tagging it @NonCPS
because it's required for groovy closures on Pipeline, which I'd really prefer to use over full java <1.8 syntax.
推荐答案
For pwd you can use pwd
step.
As for list of files in main workspace dir you could use findFiles
:
files = findFiles(glob: '*.*')
这篇关于在Jenkins管道上的工作区中列出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!