詹金斯(Jenkins)错误:54:期望除``\ n''以外的任何内容;反正

时间:2019-02-01 09:02:50

标签: shell jenkins jenkins-groovy

我正在jenkins管道中使用以下apache groovy脚本将我的工件(dev.ear)部署到服务器。我在groovy中嵌入了shell脚本,以将dev.ear从jenkins slave安全地复制到目标服务器(unix服务器)。

node('linux') {
    stage('Checkout/Download/Deploy') {
        timeout(time: 30, unit: 'MINUTES') {

        def ziptmp = '.ziptmp'
        output = sh returnStdout: true, script:"/bin/rm -rf ${ziptmp}; /bin/mkdir ${ziptmp}; cd ${ziptmp}; /usr/bin/unzip -qq ${tempdir}/${artifactFilename}; ls -ltr; echo *; 
            if [ -e dev.ear ]
            then
                scp dev.ear lsfi@${serverName57}:/apps/wls/dev/applications;
                echo "COPIED DEV ARTIFACT TO SERVER"

            else
                echo "DEPLOYMENT PACKAGE DOESNT CONTAIN DEV ARTIFACT"
            fi"
        echo "RESULT::: ${output}"
        }
    }
}

我触发Jenkins工作时遇到以下错误

WorkflowScript: 54: expecting anything but ''\n''; got it anyway @ line 54, column 171.
   ctFilename}; ls -ltr; echo *; 

我删除了shell脚本中的新行并更新了以下代码:

def ziptmp = '.ziptmp'
output = sh returnStdout: true, script:"/bin/rm -rf ${ziptmp}; /bin/mkdir ${ziptmp}; cd ${ziptmp}; /usr/bin/unzip -qq ${tempdir}/${artifactFilename}; ls -ltr; echo *; if [ -e dev.ear ] then scp dev.ear lsfi@${serverName57}:/apps/wls/dev/applications; fi;"
echo "RESULT::: ${output}"

但是我收到以下错误:

line 2: syntax error near unexpected token `fi'

如何解决此错误。

1 个答案:

答案 0 :(得分:2)

Groovy不喜欢GString中的换行符。根据{{​​3}},您可以使用'''Your multiline String'''"""Your multiline ${GString}"""制作多行字符串。

对于bash语法,我不太确定,但是根据the Grails cookbook,您似乎在if [ -e dev.ear ]之后缺少分号。

将它们放在一起:

output = sh returnStdout: true, script: """/bin/rm -rf ${ziptmp}; /bin/mkdir ${ziptmp}; cd ${ziptmp}; /usr/bin/unzip -qq ${tempdir}/${artifactFilename}; ls -ltr; echo *; 
    if [ -e dev.ear ];
    then
        scp dev.ear lsfi@${serverName57}:/apps/wls/dev/applications;
        echo "COPIED DEV ARTIFACT TO SERVER"
    else
        echo "DEPLOYMENT PACKAGE DOESNT CONTAIN DEV ARTIFACT"
    fi"
echo "RESULT::: ${output}"""