29

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

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

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/):

enter image description here

(Due to a permissions moving COMPOSER_HOME outside of the /root directory was also needed to another one, accessible for Jenkins, was also needed.)

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

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 PATH environment variable in Jenkins configs correctly working on Windows?

automatix
  • 14,018
  • 26
  • 105
  • 230
  • The last one that you tried should work. However, confused why do you want to add jenkins bin here? Instead you should it on the command line where you start jenkins itself. Also check if there are any `spaces` in the existing path variable. Check [this](https://issues.jenkins-ci.org/browse/JENKINS-12365) please. – Rao Apr 07 '16 at 11:28
  • Thanks for your comment! To your question about "Jenkins bin": By default there is no `bin` folder in the Jenkins root directory. This `bin` is just a custom one I've created for storing there the binaries of the [`global`ly](https://getcomposer.org/doc/03-cli.md#global) installed Composer packages. – automatix Apr 07 '16 at 11:47
  • Just tried it out again with `%PATH%;D:\path\to\Jenkins\bin`. It doesn't work. And there are no spaces in the path. – automatix Apr 07 '16 at 11:48
  • May be you want to set it on the command line and restart jenkins and see? – Rao Apr 07 '16 at 11:50
  • Just checked this: Added `D:\path\to\Jenkins\bin;` to the `Path`, logged out, and restarted Jenkins. It hasn't helped. – automatix Apr 07 '16 at 12:27
  • Check out the comments in [this Q&A](http://stackoverflow.com/questions/23502306/modify-path-variable-in-jenkins-master), it probably answers your question. – Dominik Gebhart Apr 07 '16 at 12:50
  • Thank you for the hint. I read the post, but there is no the `Prepare jobs environment` setting, there is no such option in my configs (maybe due to the newer version). – automatix Apr 08 '16 at 11:44
  • Possible duplicate of [modify PATH variable in jenkins master](https://stackoverflow.com/questions/23502306/modify-path-variable-in-jenkins-master) – CJCombrink Jul 11 '18 at 12:24

12 Answers12

26

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.

Roderick
  • 1,205
  • 11
  • 24
22

The issue I had was caused not by a wrong Path configuration. %PATH%;D:\path\to\COMPOSER_HOME\vendor\bin is correct.

enter image description here

automatix
  • 14,018
  • 26
  • 105
  • 230
4

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

pocketrocket
  • 365
  • 2
  • 8
3

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

//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.

hakuna
  • 6,243
  • 10
  • 52
  • 77
2

I had a similar requirement to customize Path variable 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.

Here is what I did:

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

Nodes -> WindowsNode -> Configure

2) Disconnected my Jenkins node.

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

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

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 Command build step:

git --version

joshm
  • 81
  • 7
2

In Windows, there is a User's Path variable and a System's Path variable. Jenkins uses the System's Path Variable by default. And you have to restart the service after updating the path.

enter image description here

Nick Cordova
  • 724
  • 6
  • 16
0
  • 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.

Nagama Inamdar
  • 2,851
  • 22
  • 39
  • 48
0

Couldn't get other it to work by the other answers. Ended up setting the variable in my slave node launcher (batch) script:

SET PATH=C:\cygwin64\bin;%PATH%
java -jar agent.jar -jnlpUrl ...
Andreas
  • 5,086
  • 3
  • 16
  • 36
0

If setting a global in Jenkins hurts OS-specific agents or the need to accommodate differences across same OS agents, setting Path in the agent's system variables helps.

reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d "C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0;..." /f

net stop jenkins_agent

net start jenkins_agent
eel ghEEz
  • 1,186
  • 11
  • 24
0

Just to add to this as we had a similar problem and looked here for an answer.

In our case Environment variable set Globally didn't work, but defining it the exact same way on the node configuration did. Our issue was on Pipeline projects. Maven projects worked fine either way. see screen shot of definition.

If this definition was in global then in scripted pipeline $path didn't expand to current path on build server. It just stayed as constant value $path. If this same definition was on the node configuration $path expanded as expected.

image

Shunya
  • 2,344
  • 4
  • 16
  • 28
Andrew
  • 1
0

What I did to fix my problems with Jenkins not being able to find 'cmd', 'python' or 'poetry' was modify the Environment variables section of Global properties: enter image description here

Name = Path

Value = C:\Users\yourusername\AppData\Local\Programs\Python\Python39;C:\Windows\System32;C:\Users\yourusername\AppData\Roaming\Python\Scripts

I was getting error messages like these:

'cmd' is not recognized as an internal or external command,

and

'python' is not recognized as an internal or external command,
avariant
  • 2,234
  • 5
  • 25
  • 33
Scott Siddall
  • 51
  • 1
  • 5
-1

THIS SOLVES IT :

Go To

Jenkins -> Configure System -> Global properties -> Environment variables: name=Path, value=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin/

  • 1
    No it does not! If you have linux nodes and mac nodes executables are on different paths on them. – hicnar Aug 29 '22 at 14:59