5

I need to access Groovy-defined variables (e.g. var1) in a multi-line shell script in Jenkins. I need to use double-quote """ in sh. (I refer to here)

But I also need to read and change os system variable (e.g. aws_api_key) too. It needs to use single-quote ''' in sh and use \ to escape dollar $ sign. (I refer to here)

How can I use both of them? any help will be much appreciated.

e.g.

node ("Jenkins-Test-Slave") {
stage ("hello world") {
    echo 'Hello World'
}

def var1="bin"
stage ("test") {
    withEnv(["aws_api_key='define in withEnv'","service_url=''"]) {
        echo var1
        sh '''
            echo the groovy data var1 is "${var1}",'\${var1}',\$var1,${var1},var1!!!
            echo default value of aws_api_key is \$aws_api_key
            aws_api_key='changed in shell'
            echo new value of aws_api_key is \$aws_api_key

            export newvar='newxxx'
            echo the new var value is \$newvar
        '''
    }
}
}

The result is:

+ echo the groovy data var1 is ,${var1},,,var1!!!
the groovy data var1 is ,${var1},,,var1!!!
+ echo default value of aws_api_key is 'define in withEnv'
default value of aws_api_key is 'define in withEnv'
+ aws_api_key=changed in shell
+ echo new value of aws_api_key is changed in shell
new value of aws_api_key is changed in shell
+ export newvar=newxxx
+ echo the new var value is newxxx
the new var value is newxxx
Orionpax
  • 993
  • 5
  • 13
  • 27

2 Answers2

5

Instead of multi-line String (''') use multi-line GString (""") to make string interpolation work, then ${var1} will be interpolated as expected:

sh """
    echo the groovy data var1 is "${var1}",'\${var1}',\$var1,${var1},var1!!!
    echo default value of aws_api_key is \$aws_api_key
    aws_api_key='changed in shell'
    echo new value of aws_api_key is \$aws_api_key

    export newvar='newxxx'
    echo the new var value is \$newvar
"""
Gergely Toth
  • 6,638
  • 2
  • 38
  • 40
  • I have tried that. But the problem is that the os system variable such as \$aws_api_key can't be accessed. – Orionpax Jun 08 '17 at 10:23
  • @Orionpax have you checked? Because for the above this is what I get: `+ echo the groovy data var1 is bin,${var1},,bin,var1!!! the groovy data var1 is bin,${var1},,bin,var1!!! + echo default value of aws_api_key is 'define in withEnv' default value of aws_api_key is 'define in withEnv' + aws_api_key=changed in shell + echo new value of aws_api_key is changed in shell new value of aws_api_key is changed in shell + export newvar=newxxx + echo the new var value is newxxx the new var value is newxxx` Which looks what is expected – Gergely Toth Jun 08 '17 at 11:18
5
def var1 = 'bin'

//the following are `groovy` strings (evaluated)

sh "echo var1 = $var1"
//outputs> echo var1 = bin

sh "echo var1 = ${var1}"
//outputs> echo var1 = bin

sh """echo var1 = ${var1}"""
//outputs> echo var1 = bin

sh "echo var1 = \$var1"
//outputs> echo var1 = $var1

//the following are usual strings (not evaluated)

sh 'echo var1 = $var1'
//outputs> echo var1 = $var1

sh '''echo var1 = $var1'''
//outputs> echo var1 = $var1

so, in your case just use

def var1 = 'bin'
sh """
   echo var1 = $var1
   shell_var = 'yyy'
   echo shell_var = \$shell_var
""""
daggett
  • 26,404
  • 3
  • 40
  • 56