I’m trying to create a json file from groovy code that is called from Jenkinsfile. The groovy code seems to work (i.e at the end of the code I list the files in the directory and I see the file is listed there), but when I try try to access that file from a later stage from shell or python then the file isn’t there.
stage {
steps {
script {
if (foo.bar()) {
echo "set_var: " + datastore.set_var(WORKSPACE, "meow")
sh(script: "pwd ; ls -l")
}
}
}
}
datastore.groovy:
@NonCPS
def static set_var(workspaceDir, String text) {
def filePath = "${workspaceDir}/datastore.json"
def data = [:]
def file = new File(filePath)
if (file.exists()) {
data = JSON_SLURPER.parse(file)
} else {
file.getParentFile().mkdirs()
file.createNewFile()
}
data["text"] = text
def json = JsonOutput.toJson(data)
def files = ""
try {
file.write(json)
file.getParentFile().eachFile { f -> files += f.getName() + "n" }
} catch (Exception e) {
return "Error writing to file: ${file.getAbsolutePath()}: ${e}"
}
return "Wrote text: ${text} to file: ${file.getAbsolutePath()}; files: ${files}"
}
When I run the job in jenkins I see this:
set_var: Wrote key: FAIL_REASON, text: meow to file: /home/ubuntu/workspace/jobname/91/datastore.json; files: datastore.json
13:39:34 [Pipeline] sh
13:39:34 + pwd
13:39:34 /home/ubuntu/workspace/jobname/91
13:39:34 + ls -l
13:39:34 total 24
13:39:34 -rw-rw-r-- 1 1000 1000 78 Jul 9 10:39 Dockerfile
13:39:34 -rw-rw-r-- 1 1000 1000 230 Jul 9 10:39 README.md
13:39:34 drwxrwxr-x 6 1000 1000 4096 Jul 9 10:39 jenkins
13:39:34 -rw-rw-r-- 1 1000 1000 1209 Jul 9 10:39 pipeline.py
13:39:34 -rw-rw-r-- 1 1000 1000 27 Jul 9 10:39 requirements.txt
13:39:34 drwxrwxr-x 4 1000 1000 4096 Jul 9 10:39 src
So it looks like the /home/ubuntu/workspace/jobname/91 directory that the groovy sees is different from the /home/ubuntu/workspace/jobname/91 that shell/python see.