简体   繁体   中英

How to create and then write text into a file in Jenkins using Groovy

I write a Jjenkins job, which can firstly create a CODEOWNER file direct in workspace and then write a text into this file.

I have tried:

node {
    writeFile file: 'groovy1.txt', text: 'Working with files the Groovy way is easy.'
    sh 'ls -l groovy1.txt'
    sh 'cat groovy1.txt'
}

However, the above code was not working.

While building, the job was stopping.

I have read the documentation Groovy. There is a method called writeFile(file, text) , this writeFile method overwrites the text, if this method in a loop. My goal is: insert text into the file but this text is a string. I am not sure it is right syntax for CODEOWNERS.

Any solution?

I'm new too using Jenkins and Groovy, and as I can see, scripts like yours seems to works inside the script{} statement. It works for me in this way:

stage('My Stage'){
  steps{
        ...
        script{
               writeFile file: 'groovy1.txt', text: 'Working with files the Groovy way is easy.'
               sh 'ls -l groovy1.txt'
               sh 'cat groovy1.txt' 
        }
   }

}

Hope this can help you: :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM