How to assign a groovy variable to a shell variable

+5 votes

I have the following code snippet in my Declarative Jenkins pipeline wherein I am trying to assign a groovy array variable 'test_id' to a shell array variable 't_id'. 

script{

def test_id

                  sh """

                  t_id=($(bash -c \" source ./get_test_ids.sh ${BITBUCKET_COMMON_CREDS_USR} ${BITBUCKET_COMMON_CREDS_PSW} ${env.StoryId} \"))

                  test_id=${("${t_id[@]}")}

                  """

                   echo "${test_id[0]}"

                   echo "${test_id[1]}"

}

The shell commands work in the bash shell but when I add the multi line sh step, I am getting the below syntax error when I run it in Jenkins. Can you please help?

WorkflowScript: 46: illegal string body character after dollar sign;
   solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 46, column 20.
                     	sh """
                      ^
Sep 11, 2019 in Jenkins by Abhilash
• 380 points
89,898 views

See if this could help you @Abhilash https://github.com/nextflow-io/nextflow/issues/69 This might have something to do with String interpolation I believe. Try adding the shebang line as well, just in case.

Hi Karan,

Thanks for your inputs. But, the string interpolation as provided in the link works for accessing the groovy variable in the shell script. I am actually interested in assigning the groovy array list variable 'test_id' with the shell script array variable 't_id'. I tried it but it is not working. Is there a way we can achieve this?

How to read the  shell variable in groovy / how to assign shell return value to groovy variable. 

Requirement : Open a text file read the lines  using shell and store the value in groovy  and get the parameter for each line .

Here , is delimiter

Ex: releaseModule.txt

./APP_TSBASE/app/team/i-home/deployments/ip-cc.war/cs_workflowReport.jar,configurable-wf-report,94,23crb1,artifact

./APP_TSBASE/app/team/i-home/deployments/ip.war/cs_workflowReport.jar,configurable-temppweb-report,394,rvu3crb1,artifact

========================

Here want to get  module name 2nd Parameter (configurable-wf-report) ,  build no 3rd  Parameter (94), commit id 4th (23crb1)

def  module = sh(script: """awk -F',' '{ print \$2 "," \$3 "," \$4 }' releaseModules.txt  | sort -u """, returnStdout: true).trim()

echo module

List lines = module.split( '\n' ).findAll { !it.startsWith( ',' ) }

def buildid

def Modname

lines.each {

List det1 =  it.split(',')

buildid=det1[1].trim() 

Modname = det1[0].trim()

tag= det1[2].trim()

               

echo Modname               

echo buildid

                echo tag

                        

}

Hi@veerappan,

Can you explain your requirement a little bit more? Also, share your complete script?
I had provided the solution, those who want to assign shell array value to groovy variable.

Just pass the shell array value file, and use pipeline, stages, stage, step, script. Inside the script pass my code.

2 answers to this question.

+2 votes
Best answer

I have tried the following in my script and it worked 

stage ('Test about page')
{
echo '===========Testing============'
test_output = sh (script: 'java -jar Final_test.jar ', , returnStdout:true).trim()
if(test_output=="PASS")
{
echo 'test passed !!! '
// if the test is successful push the image to docker registry
sh 'sudo docker push smartbond/simple-php-website:v${BUILD_NUMBER}'
test_result="PASS"
}

Please try the underlined statement to convert the groovy variable to shell script.

<groovy variable> = sh (script: '<shell command> ', , returnStdout:true).trim()

The output is a string and you can assign this to a shellscript ${<variable name>} 

reference : https://github.com/Raveendiran-RR/simple-php-website/blob/master/Jenkinsfile

Regards

RRR

PS: If this post has helped you, please upvote it 

answered Dec 4, 2019 by Raveendiran
• 980 points

selected Dec 4, 2019 by Kalgi

Hi @Raveendiran! I understand your answer but I have a small doubt. If the command docker push command fails(I always end up with errors while pushing an image to the Docker registry), what will be the value of "test_result"?

0 votes

Hi,

To use a template string, where variables are substituted into a string, use double-quotes. So here, you can use \$ only for shell environment variables and use $ for groovy variables.

$ sh "echo \$some_var" 
answered Dec 14, 2020 by MD
• 95,460 points

Related Questions In Jenkins

0 votes
2 answers

how to execute a shell script post build step in Jenkins?

You could use the Post Build Task Plugin ...READ MORE

answered Jul 31, 2019 in Jenkins by Sirajul
• 59,230 points
29,463 views
+1 vote
1 answer

Unable to extract elements from a ArrayList and assign to a variable

I got the solution for this: pipeline ...READ MORE

answered Sep 5, 2019 in Jenkins by Abhilash
• 380 points
10,807 views
+4 votes
7 answers

How to set the pipeline name and description for a Jenkinsfile?

You can rename pipeline jobs in jenkins ...READ MORE

answered Oct 11, 2018 in Jenkins by Hannah
• 18,520 points
36,360 views
0 votes
2 answers

How to start Jenkins on a different port rather than 8080?

This is simple, use the following command: java ...READ MORE

answered Jul 4, 2018 in Jenkins by Sophie may
• 10,620 points
35,833 views
+5 votes
3 answers

How to trigger a Jenkins pipeline A in another Jenkins pipeline B?

You can run the following code, its ...READ MORE

answered Jul 5, 2018 in Jenkins by Sophie may
• 10,620 points
119,643 views
+2 votes
4 answers
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,032 views
+2 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP