a speical character issue on jenkins jobdsl plugin

a speical character issue on jenkins jobdsl plugin

Issue

I change one jobDsl groovy file for jenkins, try to add some shell script there :

<br /> shell (
"""
#!/bin/bash
pid=$(netstat -tulnp | awk -vport=28189 '$4~":"port"{print 0+$7}')
echo "$pid"
if [[ -n $pid ]]; then
  kill -9 "$pid"
fi
cd app/sdk
/opt/maven/bin/mvn clean deploy
“”"

but when I tried to regnerate the jenkins job, I got this error :

<br />ERROR: Build step failed with exception
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 13: illegal string body character after dollar sign;
   solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 13, column 11.
       shell (
             ^

1 error

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:302)
    at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:149)
    at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:119)
    at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:131)
    at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:359)
    at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:137)
    at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:108)
    at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:236)
    at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:161)
    at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:846)
    at org.codehaus

the problem is here : `

either escape a literal dollar sign “\$5” or bracket the value expression “${5}” @ line 13, column 11.
`

Solution

the problem caused by the $ sign, to fix it, here is the solution:

<br />#!/bin/bash
pid=\$(netstat -tulnp | awk -vport=28189 '\$4~":"port"{print 0+\$7}')
echo "\$pid"
if [[ -n \$pid ]]; then
  kill -9 "\$pid"
fi

see this link dollar slashy ring

test

<br />$ ./gradlew clean jobDsl
$ ls
README.md             gradle                gradlew               run.jenkins.docker.sh
build.gradle          gradle.properties     gradlew.bat           src
Then you can find your generated job xml here:
$ less build/jobs/qbo-test-master-build-commit.xml

About jsdom

leading software engineer
This entry was posted in devops and tagged . Bookmark the permalink.

Leave a comment