Passing a variable to a bash script in a jenkins pipeline task

I have a Jenkins pipeline job in which I set up my environment using a bash script called setup.sh that looks like this:

#!/bin/bash export ARCH=$1 echo "architecture = " ${ARCH} 

In the Jenkins pipeline of the script, Icall setup.sh script with:

 def lib_arch='linux-ubuntu-14.04-x86_64-gcc4.8.4' sh ". /opt/setup.sh ${lib_arch}" 

Unfortunately, it seems that the NO variable is being passed to setup.sh script, and echo $ {ARCH} returns an empty string! I tried to do so: sh "source / opt / setup.sh $ {lib_arch}" and this also fails with the message "source not found". I also tried changing the first line of my script to

 #!/bin/sh 

but it doesn’t help. So, how can I pass the parameter to my bash script in the Jenkins script pipeline? thank you for your help.

Update: A workaround was suggested by Bert Jan Schrijve in this thread (see below):

 sh "bash -c \" source /opt/setup.sh ${lib_arch}\"" 
+5
source share
2 answers

The example below works:

 void updateApplicationVersionMaven(String version) { sh "mvn -B versions:set -DnewVersion=$version" } 

And the full pipeline script (tested on Jenkins 2.7.3):

 node { stage('test') { def testVar='foo' sh "echo $testVar" } } 

EDIT (after comments): Ah, checked a few more and was able to reproduce the problem. This is because you are using a script with. /opt/setup.sh. This affects the shell environment, in which case it breaks the injection of the Jenkins variable. Interesting.

EDIT2 (after comments): I believe this is a problem with the default shell used (either Jenkins or OS). I could reproduce the problem from the comments and was able to get around it, explicitly using bash as a shell:

 def testVar='foo3' sh "bash -c \". /var/jenkins_home/test.sh $testVar && echo \$ARCH\"" 

The last echo now displays the contents of testVar, which was passed as a script argument and then set to script as an environment variable.

+2
source

1. Try removing curly braces in a script.

 export ARCH=$1 echo "architecture = " $ARCH 

2. In the Jenkins shell:

 def lib_arch='linux-ubuntu-14.04-x86_64-gcc4.8.4' sh ". /opt/setup.sh" $lib_arch 
0
source

Source: https://habr.com/ru/post/1258988/


All Articles