Issue
I have a parameterized pipeline, accorindg to the user input, then some values are assigned to the variables, if you see the code there are 2 echo sections, one just after the switch statement, and another one within the pipeline steps in "preparation" stage, the first set of "echos" is displaying the correct values of the vars, while the second set of "echos" withing the pipeline steps is not, so it looks like the variables are not being received in the pipeline, how can I get this done?, (in the example below "Stress_Test" was seleted).
def local_path
def branch
def script_file
def datagen_file
switch (TEST_TYPE) {
case "Smoke_Test":
branch = "SmokeTest"
local_path = "/e/jmeter/apache-jmeter-5.3/bin/tcp-performance-engineering/${BRANCH}"
script_file = "PE_TCP_RESTAPI_June2020_SMK.jmx"
datagen_file= "TCP_JMeter_DataFiles_smk.yaml"
break
case "Regular_Load":
branch = "Regular"
local_path = "/e/jmeter/apache-jmeter-5.3/bin/tcp-performance-engineering/${branch}"
script_file = "PE_TCP_RESTAPI_July2020_Regular.jmx"
datagen_file= "TCP_JMeter_DataFiles_regular.yaml"
break
case "Peak_Load":
branch = "PeakTest"
local_path = "/e/jmeter/apache-jmeter-5.3/bin/tcp-performance-engineering/${branch}"
script_file = "PE_TCP_RESTAPI_July2020_Peak.jmx"
datagen_file= "TCP_JMeter_DataFiles_peak.yaml"
break
case "Stress_Test":
branch = "StressTest"
local_path = "/e/jmeter/apache-jmeter-5.3/bin/tcp-performance-engineering/${branch}"
script_file = "PE_TCP_RESTAPI_July2020_Stress.jmx"
datagen_file= "TCP_JMeter_DataFiles_stress.yaml"
break
default:
println "Test type was not set!"
break
}
echo "test type selected ${TEST_TYPE}"
echo "branch to checkout ${branch}"
echo "path in local ${local_path}"
echo "script name ${script_file}"
echo "datagen file name ${datagen_file}"
pipeline {
agent any
stages {
stage('Preparation...') {
steps{
sh '''
echo "test type selected ${TEST_TYPE}"
echo "branch to checkout ${branch}"
echo "path in local ${local_path}"
echo "script name ${script_file}"
echo 2datagen file name ${datagen_file}"
echo "****************************************"
*rest of the code is not relevant.
and this is what I'm getting in the output...
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] echo
test type selected Stress_Test
[Pipeline] echo
branch to checkout StressTest
[Pipeline] echo
path in local /e/jmeter/apache-jmeter-5.3/bin/tcp-performance-engineering/StressTest
[Pipeline] echo
script name PE_TCP_RESTAPI_July2020_Stress.jmx
[Pipeline] echo
datagen file name TCP_JMeter_DataFiles_stress.yaml
[Pipeline] node
Running on Jenkins in E:\jenkins\workspace\TCP_Performance_Test_V1
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Preparation...)
[Pipeline] sh
+ echo 'test type selected Stress_Test'
test type selected Stress_Test
+ echo 'branch to checkout '
branch to checkout
+ echo 'path in local '
path in local
+ echo 'script name '
script name
This is my first pipeline, any help will be really appreciated.
Solution
Jenkins pipelines are written in groovy and in groovy, '
is used for literal strings that do not support variable interpolation. In the question, you are using sh ''' ... '''
that makes everything within that block literal and executing as is. Meaning, all the variables are looked in the shell environment and not in the pipeline.
To fix, either change sh ''' ... '''
to sh """ ... """
or remove the sh
block altogether.
Have a look at 4. Strings section of groovy documentation.
Answered By - Moon