Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape double quotes in a Jenkins pipeline file's shell command

Below is a snippet from my Jenkins file -

stage('Configure replication agents') {
            environment {
                AUTHOR_NAME="XX.XX.XX.XX" 
                PUBLISHER_NAME="XX.XX.XX.XX"
                REPL_USER="USER"
                REPL_PASSWORD="PASSWORD"
                AUTHOR_PORT="4502"
                PUBLISHER_PORT="4503"
                AUTHOR="http://${AUTHOR_NAME}:${AUTHOR_PORT}"
                PUBLISHER="http://${PUBLISHER_NAME}:${PUBLISHER_PORT}"
                S_URI= "${PUBLISHER}/bin/receive?sling:authRequestLogin=1"
            }
            steps {
                sh 'curl -u XX:XX --data "status=browser&cmd=createPage&label=${PUBLISHER_NAME}&title=${PUBLISHER_NAME}&parentPath =/etc/replication/agents.author&template=/libs/cq/replication/templates/agent" ${AUTHOR}/bin/wcmcommand'
            }

The above command, in Jenkins console, is printed as

curl -u XX:XX --data status=browser&cmd=createPage&label=XXXX&title=XXX&parentPath =/etc/replication/agents.author&template=/libs/cq/replication/templates/agent http://5XXXX:4502/bin/wcmcommand

Note how the double quotes "" are missing.

I need to preserve the double quotes after --data in this command. How do I do it? I tried using forward slashes but that didnt work.

Cheers

like image 245
Archit Arora Avatar asked Jun 13 '19 14:06

Archit Arora


People also ask

How do you escape double quotes in shell?

' appearing in double quotes is escaped using a backslash. The backslash preceding the ' !

How do you escape a quote in a shell script?

Escape with a backslash every double quote character and every backslash character: " ==> \", \ ==> \\

How do you escape space in Jenkins pipeline?

You need to escape your inner " and not the space, and you do it in the normal way with a backslash.


2 Answers

To expand on my comment, a quick test revealed its the case.

You need to escape twice, once the quote for the shell with a slash, and once that slash with a slash for groovy itself.

node() {
    sh 'echo "asdf"'
    sh 'echo \"asdf\"'
    sh 'echo \\"asdf\\"'
}

Result

[Pipeline] {
[Pipeline] sh
+ echo asdf
asdf
[Pipeline] sh
+ echo asdf
asdf
[Pipeline] sh
+ echo "asdf"
"asdf"
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
like image 96
Dominik Gebhart Avatar answered Sep 20 '22 09:09

Dominik Gebhart


After long time of struggling and googling, this is what has worked for me on similar use case:

sh("ssh [email protected] \"su user -c \\\"mkdir ${newDirName}\\\"\"")
like image 36
teejay Avatar answered Sep 20 '22 09:09

teejay