Jenkinsfile:美元符号后的非法字符串体字符;解决方案:要么转义一个字面上的美元符号"\$5“;或将值表达式括起来

Jenkinsfile:美元符号后的非法字符串体字符;解决方案:要么转义一个字面上的美元符号"\$5“;或将值表达式括起来,jenkins,groovy,jenkins-pipeline,Jenkins,Groovy,Jenkins Pipeline,我的管道在Jenkins文件的sh“”元素失败。知道哪里出了问题吗 stage('Install dependencies') { when { expression { return params.dependencies } } } steps { sh """ apt-get update apt-get install -y

我的管道在Jenkins文件的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的字符串插值,可以保留双引号,但必须在子shell表达式中转义美元符号,因为groovy不知道如何处理美元符号后面的圆括号:

更改:

$(lsb_release -cs)
致:


我注意到错误信息表明线路错误。就我而言:

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

echo“message:${env.message}”#这是最简单的答案!+1,用于指出Groovy编译器并不总是在源代码中报告错误的正确位置。同样,报告的错误行号是罪魁祸首(即Groovy中的错误)
sh """
   echo "message: ${env.MESSAGE}" # <-- error message points here
   pwd
   echo "ls: $(ls)" <-- this line has the problem
"""