Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List files on the workspace on a Jenkins Pipeline

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.

like image 260
Daniel C. Sobral Avatar asked Jul 31 '16 01:07

Daniel C. Sobral


People also ask

How do I get a list of all jobs in Jenkins?

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.

What is Jenkins workspace directory?

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.


2 Answers

For pwd you can use pwd step.

As for list of files in main workspace dir you could use findFiles:

files = findFiles(glob: '*.*') 
like image 196
Krzysztof Krasoń Avatar answered Sep 30 '22 06:09

Krzysztof Krasoń


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" 
  • Use ls to find the files and write that to another temporary file
  • Read the temporary file and split on new lines to give an array
  • Delete the temporary file
like image 40
vanappears Avatar answered Sep 30 '22 06:09

vanappears