Search code examples
groovyjenkins-pipeline

How to append to a file in Jenkins pipeline?


I have the following simple pipeline where I just want to append multiple lines into a file

node("jenkins-builder") {
    stage("test") {
        String s = "JIRA-1234 | Use "&#x27" in place of apostrophes in patient's names when exporting to CSV."
        dir(env.WORKSPACE) {
            writeFile(file: "./out.txt", text: "First line")
            writeFile(file: "./out.txt", text: s)
            def f = readFile(file:"./out.txt")
            println f
        }
    }
}

The way I have it, I only get the last string I wrote.

I don't want to use the echo shell command to do this.


Solution

  • def content = readFile(file:"out.txt")
    content+="new line\n"
    writeFile(file:"out.txt", content) 
    

    or try java/groovy file class if allowed for you

    new File("${env.WORKSPACE}/out.txt").append("new line")