0

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?

1 Answer 1

1

The File class operates on the Jenkins master, and not the build agents. You need the fileExists and writeFile pipeline step methods instead:

def fileName = "/agent/workspace/testjob/Dockerfile"

try {
  if (!fileExists(filename)) {
    println "inside if"
    writeFile(file: filename, text: '')
    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()
}
Sign up to request clarification or add additional context in comments.

5 Comments

Would this not still fail if pipelines executed in parallel and the workspace ended up in a numbered directory? Should it not use def fileName = "${WORKSPACE}/Dockerfile" or just "Dockerfile"
@IanW The assumption is that the value is an example and not literal, but it is not explicitly stated in the question.
Thanks but I dont quite understand. Why does the File class operates on the Jenkins master, but fileExists and writeFile operates on agent?
@Steve The File class is Java and independent of Jenkins. The pipeline step methods are Groovy and bound to Jenkins classes through the code.
@matthew-schuchard, unfortunately, answers with assumptions don't always help those who assume they can copy/paste to make things work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.