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
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.
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.
The Jenkins file is a base code for Jenkins which executes it as a Groovy script in Jenkins script console.
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}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With