詹金斯文件:美元符号后的非法字符串正文字符;解决方案:转义文本美元符号"$5"或将值表达式括起来



我的管道在 Jenkinsfile 的 sh "" 元素处失败。知道哪里出了问题吗?

    stage('Install dependencies') {
                when { expression { return params.dependencies } } }
        steps {
            sh """
              apt-get update
                            apt-get install -y openssh-server net-tools inetutils-ping python-pip rubygems
                            apt-get install -y 
                                apt-transport-https 
                                ca-certificates 
                                curl 
                                gnupg2 
                                software-properties-common
                            curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
                            add-apt-repository 
                               "deb [arch=amd64] https://download.docker.com/linux/debian 
                               $(lsb_release -cs) 
                               stable"
                            apt-get update
                            apt-get install -y docker-ce docker-ce-cli containerd.io
                            curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
                            chmod +x /usr/local/bin/docker-compose
                            gem install serverspec pygmy
            """
        }
    }

错误消息是:

WorkflowScript: 35: illegal string body character after dollar sign;
solution: either escape a literal dollar sign "$5" or bracket the value expression "${5}" @ line 35, column 17.

双引号"""替换为单引号 '''

sh '''
    apt-get update 
    //...
'''

每当 Groovy 在双引号内看到$时,它都会将此字符串视为GString并进行字符串插值。但是,在您的情况下,字符$不会在插值的上下文中使用,并且会失败。或者,您可以转义$但切换到单引号字符串更有意义。

如果你想使用 groovy 的

字符串插值,你可以保留双引号,但你必须在 subshell 表达式中转义美元符号,因为 groovy 不知道如何处理美元符号后面的圆括号:

改变:

$(lsb_release -cs)

自:

$(lsb_release -cs)

我注意到错误消息指示错误的行。 就我而言:

sh """
   echo "message: ${env.MESSAGE}" # <-- error message points here
   pwd
   echo "ls: $(ls)" <-- this line has the problem
"""

查找错误消息指示的行之后的第一个美元符号。

最新更新