0

I am new to shell and groovy and I have already googled and read solutions below, but none of it seems to be working

Groovy parameter to shell script Pass groovy variable to shell script read file to variable shell script How to pass a groovy variable to a shell block jenkins How to pass groovy variable to shell script in Jenkinsfile?

Issue: I want to read value from the groovy variable and pass it to shell script but either I get blank value or the literal variable name

Below is my code:

        def Tenant_Instances = ''
       //some more declarations and code
          if(tenant_created)
            {
  Tenant_Instances = "author.${ciTenant}.rally-dev.com,publish.${ciTenant}.rally-dev.com"
       println("tenant instance are -"+Tenant_Instances) //value coming fine here
                //some code here
           println("tenant instance again are -"+Tenant_Instances) //values coming fine here too
                  
                         sh '''#!/bin/bash
                                echo "[MNR] - Beginning deployment of bundle(s) to AEM Package Manager"
                                
                                IFS="," read -ra INSTANCES <<< $Tenant_Instances
                                echo "demo ${INSTANCES}" -->here value does not come properly

I have tried replacing Tenant_Instances with 'Tenant_Instances' or "${Tenant_Instances}" or $Tenant_Instances or '$Tenant_Instances'... but none of it seem to work.

Aakash16
  • 3
  • 1
  • 2
  • Use `env.Tenant_Instances = ...` in groovy. Then `Tenant_Instances` env var will be available in shell – daggett Apr 28 '22 at 12:15
  • 1
    What you show there doesn't look like validGroovy code. The triple quoted String that is passed as an argument to `sh` looks like it opens with `'''` but those are never closed. Separate from that It looks like you are expecting String interpolation to happen, which would require triple double quotes (`"""`) not triple singe quotes (`'''`). – Jeff Scott Brown Apr 28 '22 at 13:51
  • @daggett your solution worked. May I know what was the logic behind this or what was missing in my understanding? – Aakash16 Apr 29 '22 at 07:52
  • `env` is a map of environment variables that automatically passed to any shell script. The other way described by Jeff should also work but in this case you should escape all `$` that must must be passed to shell as `\$`. Note that env could contain only strings. – daggett Apr 29 '22 at 09:20

0 Answers0