简体   繁体   English

Jenkins 注入环境变量适用于批处理命令,但不适用于 powershell

[英]Jenkins Inject Environment variable works with batch command but not powershell

I'm dealing with a very weird anomaly in Jenkins that makes absolutely no sense to me.我正在处理 Jenkins 中一个非常奇怪的异常情况,这对我来说完全没有意义。 Essentially Jenkins is behaving differently for a powershell command than for a batch command.本质上,Jenkins 对于 powershell 命令的行为与批处理命令的行为不同。

My goal is to pass an environment variable (or parameter) from one Jenkins Job to another.我的目标是将环境变量(或参数)从一个 Jenkins 作业传递到另一个。 However this variable to be passed is generated during the runtime of the first job.然而,要传递的这个变量是在第一个作业的运行时生成的。

I made a fake project to test passing variables and I was able to do so by adding a build step to echo out the variable into an env.props file on the node and then used the parameterized trigger plugin to call the next job.我做了一个假项目来测试传递的变量,我能够通过添加一个构建步骤来将变量回显到节点上的 env.props 文件中,然后使用参数化触发器插件调用下一个作业。 I was able to get this to work great in this test scenario but when I tried to implement the same steps in the actual build job (which relies on powershell scripts) it did not work.我能够在这个测试场景中让它很好地工作,但是当我尝试在实际构建作业(依赖于 powershell 脚本)中实现相同的步骤时,它不起作用。

After, a lot of trial and error I have found that when I use a windows batch command to echo the variable into a props file and then inject the variable into the job - it works perfect.之后,经过大量的反复试验,我发现当我使用 Windows 批处理命令将变量回显到 props 文件中,然后将变量注入作业时 - 它工作得很好。 But when I do the exact same thing with a powershell command it does not inject the variable back in to the job even though I use the exact same line of code.但是,当我使用 powershell 命令执行完全相同的操作时,即使我使用完全相同的代码行,它也不会将变量注入回作业中。 It still writes the variable to the file but Jenkins will not "reinject" this variable back into the job's env variable even though I am using the exact same step to do so.它仍然将变量写入文件,但 Jenkins 不会将该变量“重新注入”到作业的 env 变量中,即使我使用完全相同的步骤来执行此操作。

The command is essentially this:该命令本质上是这样的:

echo Testvar=Somevalue > C:\Jenkins\env.props

Both sucesffuly write the string to the props file, but when done with a powershell command, Jenkins will not absorb the txt from the run.两者都成功地将字符串写入 props 文件,但是当使用 powershell 命令完成时,Jenkins 不会从运行中吸收 txt。 Almost as if powershell is encoding it in a way that Jenkins cannot read but looks the exact same to me.几乎就好像 powershell 正在以 Jenkins 无法读取但在我看来完全相同的方式对其进行编码。

Any ideas?有任何想法吗?

Turns out, it was the encoding!原来,这是编码!

echo "string" > file.txt 

does not produce the same result in batch as powershell.在批处理中不会产生与 powershell 相同的结果。

Switching to切换到

echo "string" | out-file -encoding ASCII file.txt 

did the trick.成功了。

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

相关问题 Jenkins Windows批处理命令Powershell环境变量 - Jenkins Windows Batch Command Powershell Environment Variables 将批处理命令重写为 Jenkins 构建步骤的 Powershell - Rewrite Batch command as Powershell for Jenkins build step FFMPEG 命令在 PowerShell 中有效,但在批处理文件中无效 - FFMPEG command works in PowerShell but not in a batch file jenkins bat命令中powershell的路径变量 - path variable for powershell in jenkins bat command 使用批处理变量以管理员身份运行 powershell 命令 - Run powershell command as admin with a batch variable 通过在cmd中设置环境变量在PowerShell中执行命令 - Executing a command in PowerShell by setting an environment variable in cmd PowerShell:仅为单个命令设置环境变量 - PowerShell: Setting an environment variable for a single command only 如何在 powershell 块和 Jenkins 管道中使用 groovy 块中的环境变量 - how to use environment variable in powershell block and groovy block in Jenkins Pipeline Powershell 命令手动工作,但在 Jenkins 中执行时不起作用 - Powershell command works manually, but does not work when executed in Jenkins Powershell Invoke-Command 远程手动工作,但不是来自 Jenkins - Powershell Invoke-Command remote works manually, but not from Jenkins
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM