简体   繁体   English

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

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

I am trying to access a environment variable created and used that in powershell block and changed that value and want to access again in grovvy section?我正在尝试访问在 powershell 块中创建和使用的环境变量并更改该值并想在 grovvy 部分再次访问? Is this feasible?这可行吗?

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 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}"
            }
        }
    }
}

or you could use something like this to return multiple values或者你可以使用类似这样的东西来返回多个值

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}"
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将变量传递给 jenkins 管道中的 powershell 脚本块 - Passing variables to powershell script block in a jenkins pipeline 如何在 Jenkins 的声明性管道上的 powershell 脚本中获取更新的环境变量值 - How to get updated environment variable value inside powershell script on Jenkins' declarative pipeline Powershell:在脚本块中使用变量来引用 $_ 的属性 - Powershell: Use a variable to reference a property of $_ in a script block 如何在Powershell tr​​y / catch块中放置变量 - How to place variable in Powershell try/catch block jenkins 管道 - powershell 命令中的 groovy 和 powershell 变量 - jenkins pipeline - both of groovy and powershell variables in powershell command Hashtable中的PowerShell管道脚本块 - PowerShell Pipeline Script Block inside Hashtable 如何在jenkins声明式管道中从powershell中获取变量? - how do i get a variable out of powershell in jenkins declarative pipeline? 如何在 Azure Devops 发布管道中使用 PowerShell 设置环境变量? - How to set an environment variable with PowerShell in Azure Devops release pipeline? Jenkins Powershell块不会等到完成 - Jenkins powershell block does not wait until it is finished 如何在我的Powershell脚本中使用Jenkins变量 - How to use a Jenkins variable in my Powershell Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM