I have created a k8s Jenkins agent. In a test job, there is a stage to create a file as below
stage('Test New file') {
steps {
script {
def fileName = "/agent/workspace/testjob/Dockerfile"
def file = new File(fileName)
try {
if (!file.exists()) {
println "inside if"
file.createNewFile()
println "File '${fileName}' created successfully."
} else {
println "inside else"
println "File '${fileName}' already exists."
}
} catch (Exception e) {
println "An error occurred while creating the file:"
println e.message
e.printStackTrace()
}
}
}
}
testjob is the job name. So folder /agent/workspace/testjob does exist. I can see items from Git repo exist in that folder. So there should be no permission issue on Jenkins user account
But Dockerfile just can't be created. Below is the log from Jenkins
[Pipeline] stage
[Pipeline] { (Test New file)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
inside if
[Pipeline] echo
An error occurred while creating the file:
[Pipeline] echo
No such file or directory
[Pipeline] }
Anything missing such that new File and createNewfile() does not work?