I cannot create a simple list of filenames in a given directory in a scripted Jenkins Pipeline. I tried many Groovy Script examples posted on SO and other forums but either the feature is blocked or I get method not found or whatever.
This seems to be the easiest
def DOCKER_FILES_DIR = './dockerfiles'
// ...
def dir = new File(DOCKER_FILES_DIR);
def dockerfiles = [];
dir.traverse(type: FILES, maxDepth: 0) {
dockerfiles.add(it)
}
But this resolves the relative path incorrectly, so I get this error:
java.io.FileNotFoundException: /./dockerfiles
I also tried to wrap it in dir
with:
def DOCKER_FILES_DIR = './dockerfiles'
// ...
def dockerfiles = [];
dir("${env.WORKSPACE}"){
def dir = new File(DOCKER_FILES_DIR);
dir.traverse(type: FILES, maxDepth: 0) {
dockerfiles.add(it)
}
}
But got the same error:
java.io.FileNotFoundException: /./dockerfiles
This does not give an error, but only adds one file to the list:
def DOCKER_FILES_DIR = './dockerfiles'
// ...
def dockerfiles = [];
def dir = new File("${env.WORKSPACE}/${DOCKER_FILES_DIR}");
dir.traverse(type: FILES, maxDepth: 0) {
dockerfiles.add(it.getName())
}
The contents of dockerfiles
is then missing all the files but the first one:
['py365']
Here is a minimal Pipeline to reproduce it in Jenkins:
#!groovy
import static groovy.io.FileType.FILES
node('master') {
FILES_DIR = './foo'
cleanWs()
sh """
mkdir foo
touch foo/bar1
touch foo/bar2
touch foo/bar3
"""
def filenames = [];
def dir = new File("${env.WORKSPACE}/${FILES_DIR}");
dir.traverse(type: FILES, maxDepth: 0) {
filenames.add(it.getName())
}
for (int i = 0; i < filenames.size(); i++) {
def filename = filenames[i]
echo "${filename}"
}
}
And the output, showing that only bar1
is printed:
Started by user Tamas Gal
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/TestHome
[Pipeline] {
[Pipeline] cleanWs
[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] done
[Pipeline] sh
[TestHome] Running shell script
+ mkdir foo
+ touch foo/bar1
+ touch foo/bar2
+ touch foo/bar3
[Pipeline] echo
bar1
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
def files = dir.listFiles()
?for each
iteration bugs. No idea what's going on there. This whole Jenkins Pipeline is a mess...new File("${env.WORKSPACE}/${FILES_DIR}")
and execute with it (see some info in this thread). The pipeline Groovy code executes on the Jenkins master while the step actions and commands will be executed will to the agents/executors which usually are not on the same machine.