How to assign a groovy variable to a shell variable?

+2 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 in Jenkins by Abhilash
• 320 points
1,288 views
Hey @Abhilash  I guess the groovy templating is giving you this issue.

Try to use triple single quotes ''' instead of '' '' '' or escape the dollar \$.

Thanks @Sirajul! Escape sequence works. But, currently, I am unable to get the value assigned to the groovy array test_id from the Shell array variable t_id. How can I access it?

script{

def test_id

                  sh """

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

                    echo "\${t_id[*]}" //Returns the array's elements 

                    test_id=\${("\${t_id[@]}")} // throws an error as Bad substitution

                    """

                    echo "${test_id[0]}" //Does not return any value

                    echo "${test_id[1]}" //Does not return any value
}
Hey @Abhilash, Can you access the groovy array variable alone? Try accessing only the groovy array just to be sure if it's correctly declared in the groovy syntax.
Hey @Sirajul, tried the following as you have suggested:

def test_id = []
println test_id.getClass() //class java.util.ArrayList

test_id = ["17372", "17374"]
echo "${test_id[0]}" //returns 17372
echo "${test_id[1]}" //returns 17374

Please let me know your comments.

@Abhilash try adding this array declaration in your script, something like this, you might have missed on groovy array syntax, Let me know if this works, if not I will try replicating this issue on my side.

script{

def test_id = []

                  sh """

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

                    echo "\${t_id[*]}" //Returns the array's elements 

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

                    """

                    echo "${test_id[0]}" 

                    echo "${test_id[1]}" 
}
@Sirajul, I tried the same as you have suggested except by additionally removing the comment but I am still facing the same 'Bad Substitution' error. Please let me know if you get anything from your end.

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?

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

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 in Jenkins by Sirajul
• 38,820 points
658 views
+1 vote
1 answer
+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
• 16,590 points
8,376 views
0 votes
2 answers

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

In case you want to change the ...READ MORE

answered Jul 31 in Jenkins by Sirajul
• 38,820 points
2,208 views
0 votes
2 answers

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

If you are talking about executing another ...READ MORE

answered Jul 31 in Jenkins by Sirajul
• 38,820 points
6,559 views
+13 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 26, 2018 in DevOps & Agile by DragonLord999
• 8,380 points
216 views
0 votes
1 answer