5

I have a groovy variable that i would like to pass to a shell block for further processing, but i keep getting the error pasted bellow:

stages {      
    stage('First Stage - echo out available variables'){
        steps{

            script {
                def string_var = "im a groovy variable"
                echo "${string_var}"   // This will print "im a groovy variable" just fine
                sh """
                    echo """ + string_var + """
                """  // This will error

                sh """
                    echo ${string_var} 
                """  // This will error

                sh ''' echo '''+ string_var +''' '''

                sh "echo ${string_var}" // This will error
            }
        }
    }
}

My error:

an exception which occurred:
    in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
    in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@1ecb37ba
    in field com.cloudbees.groovy.cps.impl.CallEnv.caller
    in object com.cloudbees.groovy.cps.impl.FunctionCallEnv@32070c3b
    in field com.cloudbees.groovy.cps.Continuable.e
    in object org.jenkinsci.plugins.workflow.cps.SandboxContinuable@641113f0
BLang
  • 930
  • 3
  • 16
  • 35
  • You need to define as `sh """ echo string_var: ${string_var} """` – Rmahajan Jan 14 '19 at 00:32
  • possible duplicate of https://stackoverflow.com/questions/41553303/pass-groovy-variable-to-shell-script – Rmahajan Jan 14 '19 at 00:35
  • What are the parameter types of the sh method? You may need to do this to get immediate evaluation of the GString: sh "echo $string_var".toString() – emilles Jan 14 '19 at 00:40
  • @RohitMahajan i tried that, same error, and on the link you posted, i tried the additional triple " and still got the same error., i will edit the code above to show what i just tried. – BLang Jan 14 '19 at 00:54
  • Triple double quotes is working for me. Can you please try `sh ''' echo '''+ string_var +''' '''` – Rmahajan Jan 14 '19 at 01:23
  • @RohitMahajan I just edited my code to show full example, i still am getting that error. – BLang Jan 14 '19 at 01:54
  • your script is broken. please fix it, missing triple single quotes – Rmahajan Jan 14 '19 at 10:05

1 Answers1

11

I tried to use your pipeline (just closed ''' scopes) and everything works fine:

pipeline {
    agent any
    stages {      
        stage('First Stage - echo out available variables'){
            steps{
                script {
                    def string_var = "im a groovy variable"
                    echo "${string_var}"

                    sh """
                        echo """ + string_var + """
                    """

                    sh """
                        echo ${string_var} 
                    """ 

                    sh ''' echo '''+ string_var +''' ''' // added '''

                    sh "echo ${string_var}"
                }
            }
        }
    }
}

Output for this pipeline:

[Pipeline] {
[Pipeline] stage
[Pipeline] { (First Stage - echo out available variables)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
im a groovy variable
[Pipeline] sh
+ echo im a groovy variable
im a groovy variable
[Pipeline] sh
+ echo im a groovy variable
im a groovy variable
[Pipeline] sh
+ echo im a groovy variable
im a groovy variable
[Pipeline] sh
+ echo im a groovy variable
im a groovy variable
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

So, probably the problem is in some other place of script, or you need to update Pipeline Plugin (I use 2.6 version) / Jenkins (I use 2.150.1 version).

biruk1230
  • 3,042
  • 4
  • 16
  • 29
  • I use pipeline plugin: 2.5 and jenkins 2.107.1 know of any bug fixes for your version? – BLang Jan 14 '19 at 14:53
  • Don't find any possible fixed bug yet (tried to find in [changelogs](https://jenkins.io/changelog-stable/)), but you can install local Jenkins with new version and check if the issue resolved. – biruk1230 Jan 14 '19 at 15:15