如何在jenkins声明式管道中从powershell中获取变量? [英] how do i get a variable out of powershell in jenkins declarative pipeline?

查看:354
本文介绍了如何在jenkins声明式管道中从powershell中获取变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

        steps {
            script{
                env.StorysTested = ''

                try{
                    powershell('''
                     //some code here
                     foreach ( $item in $Comments ) 
                     {  
                     //some code here
                     //assigning a new value to StoryTested env variable
                     $env:StorysTested = "some value"   
                     }
                     //below line works fine and displays the value
                     Write-Output "Stories tested : $env:StorysTested"
                     ''')
                     //below null value is displayed for StorysTested`` 
                     echo " From Grrovy : ${env.StorysTested}"
                    }
                    catch(err)
                    {
                     throw err
                    }
           }

我正在使用詹金斯声明性管道. 在上面的代码中,我尝试使用groovy中$ env:StorysTested的值,该值在powershell中分配.在powershell执行结束后,有什么方法可以保留在powershell中分配的变量值.将其存储在env变量中是我想到的一种方法,但显然那没有用.

I am using a jenkins declarative pipeline. In the above code i m trying to use the value of $env:StorysTested in groovy which was assigned in powershell. Is there any way i can retain a variable value that was assigned in powershell, after the powershell execution is over. storing it in env variable was one way i thought of but clearly that didnt work.

推荐答案

如果使用$env:StorysTested = "some value"设置环境变量,则该变量将存储在 powershell进程中,并且在外部不是永久或可见的这个过程.

If you set an environment variable using $env:StorysTested = "some value", this variable is stored for the powershell process and is not permanent or visible outside this process.

要创建更多永久性环境变量(即用户级别或机器级别),您需要使用.NET Framework和SetEnvironmentVariable方法:

To create more permanent environment variables (i.e., user-level or machine-level) you need to use the .NET Framework and the SetEnvironmentVariable method:

[Environment]::SetEnvironmentVariable("StorysTested", "some value", "User")

[Environment]::SetEnvironmentVariable("StorysTested", "some value", "Machine")


要从PowerShell中删除,请使用相同的.NET方法,并为变量分配$null值,如下所示:


To delete from within PowerShell, you use the same .NET method and assign a $null value to the variable like this:

[Environment]::SetEnvironmentVariable("StorysTested",$null,"User")  # or "Machine" of course

希望有帮助

这篇关于如何在jenkins声明式管道中从powershell中获取变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆