brass-a-nova
brass-a-nova

Reputation: 39

sed statement in Jenkinsfile

I am trying to run a sed command

sed -i "5s/.*jenkins_build.*/\"jenkins_build\": \"$BUILD_NUMBER\",/" ./package.json

in a Jenkinsfile in a pipeline, here is my Jenkins statement;

sh 'sed -i \\"5s/.*jenkins_build.*/\\\\\\"jenkins_build\\\\\\": \\\\\\"$BUILD_NUMBER\\\\\\",/\\" ./package.json'

I think I am escaping everything properly as mentioned here

https://gist.github.com/Faheetah/e11bd0315c34ed32e681616e41279ef4

This is the output in the Jenkins Console;

+ sed -i "5s/.*jenkins_build.*/\"jenkins_build\": \"23\",/" ./package.json
  sed: -e expression #1, char 1: unknown command: `"'

The above command works fine in the terminal. What might I be doing wrong?

EDIT: used the snippet generator to generate the script, it works now!

https://jenkins.io/doc/book/pipeline/getting-started/#snippet-generator

this is the output from it.

        sh label: '', script: 'sed -i "5s/.*jenkins_build.*/\\"jenkins_build\\": \\"$BUILD_NUMBER\\",/" ./package.json'

Upvotes: 1

Views: 440

Answers (1)

dash-o
dash-o

Reputation: 14452

Consider using single quote to wrap the sed program. It will reduce the need to escape the backslash.

sed -i '5s/.*jenkins_build.*/"jenkins_build": "'$BUILD_NUMBER'",/' ./package.json

Disclaimer: I do not have access to Jenkins - was not able to test

Upvotes: 1

Related Questions