如何在 Windows 上的 Jenkins 配置中设置 PATH 环境变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36473479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:32:43  来源:igfitidea点击:

How to set the PATH environment variable in Jenkins configuration on Windows?

windowsjenkinsconfigurationpathenvironment-variables

提问by automatix

When I run my build process on Windows Server 2008, it fails with the error message like

当我在 Windows Server 2008 上运行我的构建过程时,它失败并显示如下错误消息

Cannot run program "foo": CreateProcess: error=2, The system cannot find the file specified

无法运行程序“foo”:CreateProcess:error=2,系统找不到指定的文件

I've already had a similar issue on Ubuntu Server and resolved it by adding the path to the folder with the binaries installed globally by Composer to the PATH variable in Jenkins config (Manage Jenkins -> Configure System -> Global properties -> Environment variables: name=PATH, value=$PATH:$COMPOSER_HOME/vendor/bin/):

我已经在 Ubuntu Server 上遇到了类似的问题,并通过将 Composer 全局安装的二进制文件的文件夹路径添加到 Jenkins 配置 ( Manage Jenkins -> Configure System -> Global properties -> Environment variables: name=PATH, value=$PATH:$COMPOSER_HOME/vendor/bin/) 中的 PATH 变量来解决它:

enter image description here

在此处输入图片说明

(Due to a permissions moving COMPOSER_HOMEoutside of the /rootdirectory was also needed to another one, accessible for Jenkins, was also needed.)

(由于还需要将权限移到目录COMPOSER_HOME之外,/root还需要另一个可供 Jenkins 访问的权限。)

Now I tried the same on Windows, but it doesn't work. So, maybe I'm just setting the PATHwrong. What I've tried:

现在我在 Windows 上尝试了相同的方法,但它不起作用。所以,也许我只是设置PATH错误。我试过的:

PATH
$PATH:D:\path\to\COMPOSER_HOME\vendor\bin

PATH
$PATH;D:\path\to\COMPOSER_HOME\vendor\bin

PATH
%PATH%D:\path\to\COMPOSER_HOME\vendor\bin

PATH
%PATH%;D:\path\to\COMPOSER_HOME\vendor\bin

How to set the PATHenvironment variable in Jenkins configs correctly working on Windows?

如何PATH在 Jenkins 配置中设置环境变量在 Windows 上正确工作?

采纳答案by automatix

The issue I had was caused not by a wrong Pathconfiguration. %PATH%;D:\path\to\COMPOSER_HOME\vendor\binis correct.

我遇到的问题不是由错误的Path配置引起的。%PATH%;D:\path\to\COMPOSER_HOME\vendor\bin是正确的。

enter image description here

在此处输入图片说明

回答by Roderick

It needs to be "Path", not "PATH".

它需要是“路径”,而不是“路径”。

Jenkins treats this special variable in a case-sensitive way, and only "Path" is recognized as being the path variable. "PATH" looks to jenkins like a generic environment variable, even on Windows.

Jenkins 以区分大小写的方式处理这个特殊变量,只有“Path”被识别为路径变量。“PATH”在 jenkins 看来就像一个通用的环境变量,即使在 Windows 上也是如此。

回答by hakuna

If you want to set it locally for the respective job try this in Build -> Execute batch Commandstep:

如果您想为相应的作业在本地设置它,请按以下Build -> Execute batch Command步骤操作:

//append more variables separated by ; if required
SET Path=%PATH%;C:\Program Files\Git\bin;

//run your command here
git --version

This approach works for any type of command you wanted to execute. Just add the respective environmental variable to the Path variable locally as shown above.

这种方法适用于您想要执行的任何类型的命令。只需将相应的环境变量添加到本地的 Path 变量中,如上所示。

回答by pocketrocket

don't get to confused about %PATH% vs $PATH in the Value field. While %PATH% is the correct syntax for windows nodes, you should use /foo/bar:$PATH to extend PATH on unix nodes

不要对 Value 字段中的 %PATH% 与 $PATH 感到困惑。虽然 %PATH% 是 windows 节点的正确语法,但您应该使用 /foo/bar:$PATH 在 unix 节点上扩展 PATH

回答by joshm

I had a similar requirement to customize Pathvariable on a Windows slave with a Windows Jenkins master. I did not want to create a Jenkins global environment variable and wanted this variable to be specific to a particular windows node/agent.

我有一个类似的要求,要在 Windows 从站上使用 Windows Jenkins 主站自定义Path变量。我不想创建 Jenkins 全局环境变量,并希望此变量特定于特定的 Windows 节点/代理。

Here is what I did:

这是我所做的:

1) Created an Environment Variable as shown below in Nodes -> WindowsNode-> Configurescreen:

1) 在Nodes -> WindowsNode-> Configure屏幕中创建如下所示的环境变量:

Nodes ->WindowsNode ->Configure

节点 ->WindowsNode ->配置

2) Disconnected my Jenkins node.

2) 断开我的 Jenkins 节点。

3) Restarted my Jenkins system process directly on the slave.

3)直接在slave上重启我的Jenkins系统进程。

4) Tested it out by clicking on Nodes -> WindowsNode-> System Informationand saw the new environment variable assigned to the node:

4) 通过单击节点 -> WindowsNode-> 系统信息对其进行测试,并看到分配给节点的新环境变量:

enter image description here

在此处输入图片说明

5) Then used it in a Jenkins job that would run on the Windows slave/agent by adding below command to an Execute Windows Batch Commandbuild step:

5) 然后在 Jenkins 作业中使用它,该作业将通过将以下命令添加到执行 Windows 批处理命令构建步骤来在 Windows 从属/代理上运行:

git --version

git --version

回答by Priyanka Rawat

  • For adding path for Slave nodes, like home env variable.

  • It can be directory done from Slave nide configuration.

  • Open the configuration setting for any slave node, add env vaiable information.

  • For example for setting HOME just add the name and location of home directory.

  • 用于为 Slave 节点添加路径,例如 home env 变量。

  • 它可以从 Slave nide 配置目录完成。

  • 打开任何从节点的配置设置,添加环境变量信息。

  • 例如设置 HOME 只需添加主目录的名称和位置。