I'm trying to list files in the workspace in a Jenkins Pipeline, so that I can use that to produce appropriate parallel tasks.
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()) }
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).
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.
Go to Script Console under Manage Jenkins, this script will print the name of all jobs including jobs inside of a folder and the folders themselves: Jenkins. instance. getAllItems(AbstractItem.
The workspace directory is where Jenkins builds your project: it contains the source code Jenkins checks out, plus any files generated by the build itself. This workspace is reused for each successive build.
For pwd you can use pwd
step.
As for list of files in main workspace dir you could use findFiles
:
files = findFiles(glob: '*.*')
Here's an example of how I'm finding json files in my project for processing.
sh "ls *.json > listJsonFiles" def files = readFile( "listJsonFiles" ).split( "\\r?\\n" ); sh "rm -f listJsonFiles"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With