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

Viewed 45595

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

4 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

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

sh("ssh root@my.server.com \"su user -c \\\"mkdir ${newDirName}\\\"\"")

Update: How I think it gets interpreted

1] sh extension strips first escaping (\" becomes " and \\ becomes \, first and last " are not part of input)

ssh root@my.server.com "su user -c \"mkdir ${newDirName}\""

2] ssh command strips second level of escaping (\" becomes ", while outer " also not part of input)

su user -c "mkdir ${newDirName}"

I had double quotes inside the variable, so escaped single quotes worked for me:

sh "git commit -m \'${ThatMayContainDoubleQuotes}\'"

I needed the output to be with trailing \\ so I had to do something like this

echo 'key1 = \\\\"__value1__\\\\"' > auto.file
File looks like
cat auto.file
key1 = \\"__value1__\\"
Dependent Script
            export value1="some-value"
            var=${value1}

            # Read in template one line at the time, and replace variables
            tmpfile=$(mktemp)
            sed -E 's/__(([^_]|_[^_])*)__/${\\1}/g' auto.file > ${tmpfile}

            while read auto
            do
              eval echo "$auto"
            done < "${tmpfile}" > autoRendered.file

            rm -f ${tmpfile}
Rendered File looks like
cat autoRendered.file
key1 = "some-value"
Related