2
votes

I have a file 'README.txt' which contains the line -

"version": "1.0.0-alpha-test.7"

Using a Jenkins Pipeline, I want to replace this line with

"version": "1.0.0-alpha-test.{BUILD_NUMBER}"

The following sed command works when I try it on a linux cluster

sed -i -E "s#(\"version\"[ ]*:[ ]*\".+alpha-test\.)[0-9]+\"#\1${BUILD_NUMBER}#g" README.txt

The same command does not work using a Jenkins Pipeline.

Tried with the following query but it doesn't work -

sh """
sed -i -E "s|([\"]version[\"][ ]*:[ ]*[\"].+alpha-test\\.)[0-9]+\"|\1${BUILD_NUMBER}|g" README.txt
cat README.txt
"""

/home/jenkins/workspace/test/test-pipeline@tmp/durable-eb774fcf/script.sh: 3: /home/jenkins/workspace/test/test-pipeline@tmp/durable-eb774fcf/script.sh: Syntax error: ")" unexpected

1
You say you want {BUILD_NUMBER} in the output but your code is trying to insert ${BUILD_NUMBER} - what should really appear in the output - the text {BUILD_NUMER} or the value of the shell variable ${BUILD_NUMBER} or something else?Ed Morton
Syntax errors are 99% caused by bad escapes or no escapes. Using QUOTES within TRIPLE QUOTES will usually trip you up. Replace them with single ticks. " to ' for the sed command: sed -i -E 's|([\"]version[\"][ ]*:[ ]*[\"].+alpha-test\\.)[0-9]+\"|\1${BUILD_NUMBER}|g' README.txt cat README.txtOnur Gokkocabas

1 Answers

-3
votes

Best to use perl command instead...

script{

old_version = (sh (returnStdout: true, script:'''old_version=`cat version.cfg |grep VERSION=|cut -d "=" -f2`
echo $old_version''')).toString().trim()

sh """

     if [ "$old_version" != $new_version ]; then
        perl -pi -e "s,$old_version,$new_version,g" version.cfg
        ##-- git push operation --##
     fi  

"""

}