Jenkins:如果管道为空,则从管道中删除特定参数

Jenkins: remove specific parameter from pipeline if it's empty

在通用管道中,我有以下内容:

pipeline {
    agent any
    parameters {
        string(name: "TEST_VAR", description:  "testvar")
    }
    stages {
        stage("My stage") {
            agent {
                docker {
                    image 'myimage'
                    args "-u root:root"
                }
            }
            steps {
                script {
                    params.each {param -> 
                        if (param.value == "") { sh "unset ${param.key}" }
                    }
                    sh 'printenv'
                }
            }
        }
    }
}

但是,即使我将参数留空,printenv 步骤也会将 TEST_VAR 打印为现有的空环境变量,就好像未设置步骤不起作用一样。我怎样才能正确取消设置,使其完全不被声明?

事实证明,jenkins 参数是不可变的。我通过将参数称为 TEST_VAR_PARAM 然后检查它是否为空并根据其值创建 TEST_VAR env 值来解决此问题。