简体   繁体   English

"在 Jenkins 构建的 Windows Powershell 中设置的变量,在其他构建步骤中不可用"

[英]variable set in Windows Powershell of Jenkins build, not available in other build steps

I have a Jenkins build with one parameter called VERSION.我有一个带有一个名为 VERSION 的参数的 Jenkins 版本。

Based on the length of the variable i am modifying its value in Windows Powershell and then in the next build step, i want to use it.根据变量的长度,我在 Windows Powershell 中修改它的值,然后在下一个构建步骤中,我想使用它。

But the modified value is not being reflected in the next build step execution, it still refer to the intial value entered as a parameter.但是修改后的值并没有反映在下一个构建步骤执行中,它仍然引用作为参数输入的初始值。 I tried ENV,script,global none of them seems to work.我试过 ENV,script,global 它们似乎都不起作用。

Windows powershell build step Windows PowerShell 构建步骤

input VERSION=1810(via jenkins build)输入 VERSION=1810(通过 jenkins 构建)

           if ("$ENV:VERSION".length -eq 4)
        {
           $ENV:VERSION = "$ENV:VERSION",3 -join ""  (here it will be 18103)
         }

         Write-Output "$ENV:VERSION" (18103 here aswell)

This is a general issue with environment variable scope.这是环境变量作用域的普遍问题。 Every process inherits the environment variables from its parent, but has its own copy, and any modifications you make will only be reflected in the current process and child processes.每个进程都从其父进程继承环境变量,但有自己的副本,您所做的任何修改只会反映在当前进程和子进程中。

I think you will have to find some way to pass the value to a future step that doesn't rely on environment variables.我认为您必须找到某种方法将值传递给不依赖于环境变量的未来步骤。

You can try to use EnvInject Plugin and set additional PROJ_VERSION=$ENV:VERSION variable in your job.您可以尝试使用EnvInject 插件并在您的工作中设置额外的PROJ_VERSION=$ENV:VERSION变量。 In this case it should be working correctly.在这种情况下,它应该可以正常工作。 If it isn't working within Properties Content directly, try to use injection via file as in this example.如果它不能直接在Properties Content工作,请尝试像例中那样通过文件使用注入。

I found another option which works in Freestyle project jobs.我发现了另一种适用于 Freestyle 项目工作的选项。

the 1st powershell step:第一个 powershell 步骤:

[Environment]::SetEnvironmentVariable('IMAGE_VERSION', $imageVersion, 'Machine')

the 2nd powershell step:第二个powershell步骤:

$imageVersion = [Environment]::GetEnvironmentVariable('IMAGE_VERSION', 'Machine')

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

相关问题 Jenkins 作业无需构建步骤即可创建 Powershell Jenkins 自动化 - Jenkins Job gets created without Build Steps Powershell Jenkins Automation Github 操作:为 Windows 设置环境变量,使用 PowerShell 构建 - Github actions: set environment variable for Windows build with PowerShell jenkins 管道 - 如何在声明性管道步骤中使用 powershell 命令构建 docker 镜像 - jenkins pipeline - how to build a docker image in a declarative pipeline steps use powershell command 如果Powershell脚本中出现条件,则Jenkins构建失败 - Fail Jenkins build if condition occured in Powershell script Jenkins参数化构建-在Powershell中读取文件 - Jenkins parametrized build - read file in powershell 将批处理命令重写为 Jenkins 构建步骤的 Powershell - Rewrite Batch command as Powershell for Jenkins build step 用PowerShell改变构建变量不起作用 - change build variable with powershell not working 在Windows PowerShell中运行VS 2017构建工具 - Running VS 2017 Build Tools in Windows PowerShell 构建步骤'Windows PowerShell'标记构建为失败,为什么? - Build step 'Windows PowerShell' marked build as failure, why? Powershell将正则表达式值保留在变量中,然后构建字符串 - Powershell to keep regex value in a variable then build string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM