shelljenkinsgroovyjenkins-job-dslconfiguration-as-code

How to escape ${} in Jenkins configuration as code (casc) plugin?


I am using Jenkins configuration as code (CASC) plugin to create Jenkins job during server startup inside docker. I wrote a shell script as a step inside a job configuration.

- script: >
      freeStyleJob('jenkins-job-sample') {
        description('Sample')
        triggers {
            githubPush()
        }
        scm {
              git {
                remote { 
                  url('${GIT_URL}') 
                  credentials('github-credentials')
                }
                branch '*/dev'
                extensions {localBranch('dev')}
              }
          }
        steps {
            shell("""

                  do
                      cat \$OUTPUT | while read line || [[ -n \$line ]];
                      do
                        CAPP_ENTRY=\$line
                        GROUP_ID_PATH=(\${CAPP_ENTRY[0]})
                        ARTIFACT_ID=(\${CAPP_ENTRY[1]})
                        VERSION=(\${CAPP_ENTRY[2]})
                        
                      done
                    done   

                """)   
        }
        publishers {
             
        }
      }

After the job is created I want the final script step to be shown as below.

CAPP_ENTRY=\$line
GROUP_ID_PATH=(${CAPP_ENTRY[0]})
ARTIFACT_ID=(${CAPP_ENTRY[1]})
VERSION=(${CAPP_ENTRY[2]})

But during server startup it throws the following error.

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: script: 31: unexpected char: '\' @ line 31, column 34.
                     GROUP_ID_PATH=(\)
                                    ^

1 error

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)     at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:150)    at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:120)     at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:132)     at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:350)     at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:139)  at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:110)     at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:234)    at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:168)     at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:943)     at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)   at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)     at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)   at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)     at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)     at groovy.lang.GroovyShell.parse(GroovyShell.java:700)  at groovy.lang.GroovyShell$parse.call(Unknown Source)   at javaposse.jobdsl.dsl.AbstractDslScriptLoader.parseScript(AbstractDslScriptLoader.groovy:134)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    at java.lang.reflect.Method.invoke(Method.java:498)     at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)     at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)  at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:174)     at 
javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScriptEngine(AbstractDslScriptLoader.groovy:101)

It seems that ${CAPP_ENTRY[0]} is getting evaluated before the job creation happens which results to be (\). I want to escape ${} from getting evaluated before job creation. I tried in below ways.

None of the above worked. Highly appreciate any suggestions on how I can escape it from getting evaluated.


Solution

  • Found an answer. Adding a ^ solved my issue. :) (^${CAPP_ENTRY[0]})

    Source: https://github.com/jenkinsci/configuration-as-code-plugin/issues/577