Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing variable to bash script in a jenkins pipeline job

I have a Jenkins pipeline job in which I configure my environment with a bash script named setup.sh which looks like:

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

In the Jenkins pipeline script, Icall the 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 NO variable is passed to the setup.sh script, and the echo ${ARCH} return an empty string! I tried to instead do: sh "source /opt/setup.sh ${lib_arch}" and this fails as well with the "source not found" message. I also tried changing the first line of my script to

#!/bin/sh

but it does not help. So how can I pass a parameter to my bash script in a Jenkins pipeline script? thanks for your help.

Update: a workaround was sugggested by Bert Jan Schrijve in this thread (see below):

sh "bash -c \" source /opt/setup.sh ${lib_arch}\"" 
like image 350
NabilG Avatar asked Oct 29 '16 20:10

NabilG


People also ask

How do I run a bash script in Jenkins pipeline?

The Best Answer is The Groovy script you provided is formatting the first line as a blank line in the resultant script. The shebang, telling the script to run with /bin/bash instead of /bin/sh, needs to be on the first line of the file or it will be ignored. And it will execute with /bin/bash.

How do you declare a variable in Jenkins pipeline?

Jenkins pipeline environment variables: You can define your environment variables in both — global and per-stage — simultaneously. Globally defined variables can be used in all stages but stage defined variables can only be used within that stage. Environment variables can be defined using NAME = VALUE syntax.


1 Answers

If you are using multiline shell script with triple apostrophe, you have to use this syntax:

sh '''
 echo '''+varToPrint+'''
 other commands...
'''

(from https://medium.com/devopslinks/a-hacky-hackers-guide-to-hacking-together-jenkins-scripted-pipelines-part-3-aca73bd33eaa)

like image 150
Tony Avatar answered Oct 22 '22 04:10

Tony