如何在 Jenkins 管道中的 powershell 块和 groovy 块中使用环境变量

how to use environment variable in powershell block and groovy block in Jenkins Pipeline

我正在尝试访问在 powershell 块中创建和使用的环境变量并更改该值并想在 grovvy 部分再次访问?这可行吗?

pipeline {  
       //adding environment varialbe
        environment {
        var1 = "somvalue"
           }

       stage ('accessvariables'){
          steps{
               script{
                      powershell '''

                       write-host "Prining environment variable here $env:var1"                                                                                                                      
                       $env:var1 = "changedValue"

                       '''
                        echo "${env.var1}" //its printing orginal somevalue I want changed value               

                      }
                  }
              }      

      }

https://www.jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#powershell-windows-powershell-script

pipeline {
    environment {
        var1 = "somvalue"
    }

    stage('accessvariables') {
        steps {
            script {
                env.var1 = powershell returnStdout: true, ''' write-host "changedValue" '''
                echo "${env.var1}"
            }
        }
    }
}

或者您可以使用类似的东西来 return 多个值

pipeline {
    environment {
        var1 = "somvalue"
    }

    stage('accessvariables') {
        steps {
            script {
                env.var1 = powershell returnStdout: true, ''' write-host "changedValue" '''
                echo "${env.var1}"
            }
            
            script {
                def newEnv = powershell returnStdout: true, '''
                    @{
                        var1 = $env:var1.ToUpper()
                        var2 = "hohoho"
                    } | ConvertTo-Json -Depth 10
                '''
                newEnv = readJSON text:newEnv
                env.putAll(newEnv)
                echo "${env.var1}"
                echo "${env.var2}"
            }
        }
    }
}