Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append a text to a file in jenkinsfile

How to append a text to a file in Jenkinsfile injecting Jenkins BUILD_ID

I wish to see

version := "1.0.25"

where 25 is BUILD_ID

Here is my attempt

import hudson.EnvVars

node {

  stage('versioning'){
    echo 'retrieve build version'
    sh 'echo version := 1.0.${env.BUILD_ID} >> build.sbt'
  } 
}

Error:

version:=1.0.${env.BUILD_ID}: bad substitution

Note the file is in the current directory

like image 495
QGA Avatar asked Jan 27 '17 18:01

QGA


People also ask

How do I write to a file in Jenkins pipeline?

In the first stage we create a variable called data that holds some text and the we use the writeFile function to write it out to a file. Then we execute ls as an external program using sh. In the second stage we use the readFile function to read in the content of the file.

Is Jenkinsfile a text file?

As discussed in the Defining a Pipeline in SCM, a Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline and is checked into source control.

Is Jenkinsfile a Groovy file?

The Jenkins file is a base code for Jenkins which executes it as a Groovy script in Jenkins script console.


1 Answers

The pipeline built in writeFile is also very useful here but requires a read+write process to append to a file.

def readContent = readFile 'build.sbt'
writeFile file: 'build.sbt', text: readContent+"\r\nversion := 1.0.${env.BUILD_ID}"
like image 163
Stefan Crain Avatar answered Sep 19 '22 15:09

Stefan Crain