Reputation: 297275
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.
Upvotes: 35
Views: 143286
Reputation: 27516
For pwd you can use pwd
step.
As for list of files in main workspace dir you could use findFiles
from the Pipeline Utility Steps plugin:
files = findFiles(glob: '*.*')
Upvotes: 43
Reputation: 1014
This is the easiest & uncomplicated groovy solution that has worked for me.
def fileList = "ls /path/to/dir".execute()
def files= []
fileList.text.eachLine {files.add(it)}
return files
Upvotes: 5
Reputation: 2473
A solution that works in all cases without the use of 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}<<<"
}
Upvotes: 11
Reputation: 1145
This 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);
}
}
Upvotes: 1
Reputation: 157
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"
Upvotes: 9
Reputation: 3018
you can try following which uses pwd() if you are running the script on master.
sh "ls -la ${pwd()}"
Upvotes: 5