Search code examples
javajenkinsgradlejenkins-pipelinegradlew

Spring Boot: Jenkins removes escaped double quote character from commands


I'm running jobs on Jenkins for my Spring Boot project and recently I have come across a problem, where Jenkins is removing double quotes from the commands it's supposed to run. Doesn't matter if it's inside the Gradle job or the Pipeline, escaped double quotes are being removed. Example:

Gradle job:

proc = "git log --pretty=format:\"%cd - ${tag} (backend) - %s \" -${distance} --no-merges --date=short".execute()

Works fine locally, however it fails on Jenkins with the error: fatal: ambiguous argument '(backend)': unknown revision or path not in the working tree.

Jenkins Pipeline:

sh "git log --pretty=format:\"%cd - ${tag} (backend) - %s \" -${distance} --no-merges --date=short"

Fails with Syntax error: "(" unexpected which boils down to the same as I have determined through this experiment:

sh "echo git log --pretty=format:\"%cd - ${tag} (backend) - %s \" -${distance} --no-merges --date=short"

This returns the command without escaped double quotes:

git log --pretty=format:%cd - 2.0.4 (backend) - %s -8 --no-merges --date=short

Anyone knows how to get around that? Thanks!


Solution

  • I was not able to force Jenkins to take the double quotes. I found this guide about escaping characters in Jenkinsfile:

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

    however, even if the command seems to have the double quotes, it still fails as if it didn't have them.

    I ended up writing the command to a file and running it from Jenkins like this:

    def command = $/echo 'cd logs/backend/ && git log --pretty=format:"%cd - ${tag} (backend) - %s" -${distance} --no-merges --date=short > src/main/resources/changeLog.txt' > cmd/$
    sh command
    sh "./cmd"