I have the following jenkinsfile
pipeline {
agent any
environment {
VERSION=""
}
stages {
stage ("release"){
steps {
script {
${VERSION}=sh(returnStdout: true, script:$("./bashscript.sh).trim())
}
}
}
}
What I'm trying to do is simple, I want to call to bash file that return a value to environment variable in my jenkinsfile.
I have 2 questions: 1. how to return value from bash file? 2. how to insert it to environment variable in jenkinsfile?
That's my .sh file:
# !/bin/bash
if [ some condition... ]
then
some commands....
return "value"
else
some commands....
return "other value"
fi
I've searched for long time and found nothing, is it possible at all?
NOTE: I saw many solutions for groovy but I need it in pipeline...
CodePudding user response:
- For setting variable example from shell for jenkins use returnStdout: true . In your script echo what you need
myVal = sh(script: ' ./myFunc.ksh ', returnStdout: true)
- Execute functions from bash as they suppose to return a variable. To make function visible you need to source it file (import) example is below
# for point 2
#/bin/bash
# content of your file ./myfileFunc.ksh
function myFunc {
typeset l_in_param1="$1"
if [[ $l_in_param1 == "A" ]]; then
echo "VAL_A"
elif [[ $l_in_param1 == "B" ]]; then
echo "VAL_B"
else
echo "ERROR VAL" >&2
fi
}
# source it
source ./myfileFunc.ksh
#use :
val1=$(myFunc A)
val2=$(myFunc C)
- Script returns data to the standard and error output so you can store it in variable later by reading from there using , returnStdout: true
myVal = sh(script: ' ./myFunc.ksh ', returnStdout: true)
# content of your file ./myFunc.ksh
typeset l_in_param1="$1"
if [[ $l_in_param1 == "A" ]]; then
echo "VAL_A"
elif [[ $l_in_param1 == "B" ]]; then
echo "VAL_B"
else
echo "ERROR VAL" >&2
fi
l_result1=$( ./myFunc.ksh A )
l_result2=$( ./myFunc.ksh C )
echo $l_result1;
echo $l_result2;
Here l_result2 will be null as we moved error to error output