Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins pipeline multiline shell with escape character

I'm running into a weird issues with a pipeline script. I have a multi line sh blob like

sh """
   git tag -fa \\"${version}\\" -m \\"Release of ${version}\\"
"""

And this somehow runs as:

+ git tag -fa '"1.0-16-959069f'
error: Terminal is dumb, but EDITOR unset
Please supply the message using either -m or -F option.

So its dropping the -m and message. I've tried single escapes, double escapes, nothing seems to work.

like image 858
devshorts Avatar asked Dec 15 '16 21:12

devshorts


People also ask

How to handle single quotes in Jenkins pipeline arguments?

The safest way to handle this is usually to single quote the argument: In a Jenkins pipeline, this whole thing has to be put into a String, meaning it has to be wrapped in quotes itself. At first glance, we can just wrap it in double quotes so single quotes don’t need to be escaped, and we get string interpolation for the input:

What is escaping quotes in Jenkins?

Escaping quotes is a special nightmare in shell scripts generally, but Jenkins adds yet another layer of confusion on top of that. I’m going to cut through that layer and find out what Jenkins is actually executing. Today I was trying to format a shell command that included a user argument that could contain special characters.

How to start a bash shell from a Jenkin script?

Then there is Jenkin's sh command (plus the shell process the command will start), then ssh, then the remote shell which ssh will start, then the bash which you start. One of them is removing the \ before $4 which breaks your AWK script.

How to format a shell argument with special characters in Jenkins?

Today I was trying to format a shell command that included a user argument that could contain special characters. The safest way to handle this is usually to single quote the argument: In a Jenkins pipeline, this whole thing has to be put into a String, meaning it has to be wrapped in quotes itself.


1 Answers

I have no idea why this worked but this did

def tagGithub(String version) {
    def exec = """
    git tag -d ${version} || true
    git push origin :refs/tags/${version}

    # tag new version
    git tag -fa ${version} -m "Release of ${version}"
    git push origin --tags
    """

    sh exec
}

Something with the inline jenkins groovy interpolation seems busted, doing the interpolation in another var and then executing it worked

like image 153
devshorts Avatar answered Nov 15 '22 05:11

devshorts