Jenkins 脚本化管道 - switch 语句中变量的更改未反映在管道步骤中

Jenkins scripted pipeline - Changes in variables within switch statement is not reflected in pipeline steps

我有一个参数化的管道,根据用户输入,然后一些值被分配给变量,如果你看到代码有 2 个回显部分,一个在 switch 语句之后,另一个在管道内在“准备”阶段的步骤中,第一组“echos”显示了正确的变量值,而第二组“echos”在管道步骤中没有显示,所以看起来变量没有被接收到管道,我该如何完成?(在下面的示例中选择了“Stress_Test”)。

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.

这就是我在输出中得到的...

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 

这是我的第一个管道,非常感谢任何帮助。

Jenkins pipelines写在groovy中,在groovy中,'用于不支持变量插值的文字串。在问题中,您正在使用 sh ''' ... ''' 使该块文字中的所有内容都按原样执行。意思是,所有变量都在 shell 环境中查找,而不是在管道中查找。

要修复,请将 sh ''' ... ''' 更改为 sh """ ... """ 或完全删除 sh 块。

查看 groovy 文档的 4. Strings 部分。