List of files in the workspace on the Jenkins pipeline

I am trying to list files in the workspace in Jenkins Pipeline so that I can use them to create related parallel tasks.

While I could just use sh ls > files and read this, I need File objects that I can filter on with more complex logic. In fact, Files.listFiles(FileFilter) would be ideal.

However, I cannot get the list of files at all. Firstly, I had to resort to some strange things to just find out the current directory of work for the assembly:

 sh 'pwd > workspace' workspace = readFile('workspace').trim() 

Now I call this to get a list of files:

 @NonCPS def getFiles(String baseDir) { Arrays.asList(new File(baseDir).listFiles()) } 

And get NPE on asList , which means that after reading javadoc that new File(baseDir) does not exist (or is not a directory).

I mark it with @NonCPS because it is needed to close groovy on the pipeline, which I would prefer to use over the full java <1.8 syntax.

+14
source share
7 answers

For pwd you can use pwd .

As for the list of files in the main directory of the workspace, you can use findFiles :

 files = findFiles(glob: '*.*') 
+18
source

Here is an example of how I find 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 search for files and write them to another temporary file
  • Read the temporary file and split it into new lines to get an array
  • Delete temporary file
+6
source

A solution that works in all cases without using the JENKINS function

 def FILES_LIST = sh (script: "ls '${workers_dir}'", returnStdout: true).trim() //DEBUG echo "FILES_LIST : ${FILES_LIST}" //PARSING for(String ele : FILES_LIST.split("\\r?\\n")){ println ">>>${ele}<<<" } 
+3
source

you can try using the pwd () command if you use a script for the wizard.

  sh "ls -la ${pwd()}" 
+1
source

It worked for me !!

 node("aSlave") { def files = getAllFiles(createFilePath("${workspace}/path_to_directory_in_workspace")) } @NonCPS def getAllFiles(rootPath) { def list = [] for (subPath in rootPath.list()) { list << subPath.getName() // in case you don't want extension // list << FilenameUtils.removeExtension(subPath.getName()) } return list } // Helps if slave servers are in picture def createFilePath(def path) { if (env['NODE_NAME'].equals("master")) { File localPath = new File(path) return new hudson.FilePath(localPath); } else { return new hudson.FilePath(Jenkins.getInstance().getComputer(env['NODE_NAME']).getChannel(), path); } } 
0
source

This is the simplest and easiest solution that worked for me.

 def fileList = "ls /path/to/dir".execute() def files= [] fileList .text.eachLine {files.add(it)} return files 
0
source

Did you learn all your file logical operations as a shell script and then run a shell script with sh './runScript.sh' ?

Shell scripts are very effective for file manipulation. The script can also be distributed for each slave for parallelization.

-2
source

All Articles