Change Windows shell in Jenkins (from Cygwin to Git Bash/msys)
Asked Answered
F

4

13

I have a Windows 7 and a Windows Server 2012 slave with the Jenkins agent and Cygwin already set up. I want to avoid Cygwin and just use the Git Bash shell that comes with Git for Windows (I think it's called msysgit). So I renamed C:\cygwin64 to C:\cygwin64.bak, removed C:\cygwin64\bin from the path, and rebooted.

The Windows 2012 box now works fine, (Unix) shell scripts run, $OSTYPE = msys, and uname = MSYS_NT-6.3 (indicating that the Git Bash shell is running).

The Windows 7 box won't run anything, and gives the following error:

Building remotely on win7 in workspace C:\Users\Jenkins\workspace\TEST
[win7] $ sh -xe C:\Users\jenkins\AppData\Local\Temp\hudson5047939025129374618.sh
The system cannot find the file specified
FATAL: command execution failed
java.io.IOException: Cannot run program "sh" (in directory "C:\Users\Jenkins\workspace\TEST"): CreateProcess
error=2, The system cannot find the file specified.
       at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)

So my question is, how do I configure Jenkins to use C:\Program Files\Git\bin\sh.exe, or C:\Program Files\Git\usr\bin\bash.exe to run shell scripts?

Freund answered 27/1, 2016 at 17:6 Comment(0)
F
21

I've worked it out. Jenkins will simply pick the first sh.exe in the path. Git Bash has an exe at C:\Program Files\Git\usr\bin, so if you add that to the beginning of your path it will be picked up by Jenkins.

You have to restart your slave's connection with Jenkins after making this change, otherwise the Path won't propagate. If you're using the Java Web Start, just close the java window and relaunch it.

Freund answered 27/1, 2016 at 18:51 Comment(8)
C:\Program Files\Git\bin also has a sh.exe in it. Any reason not to use that?Anatomist
@Anatomist No idea. Is that a symlink?Freund
Symlink on windows? Doubt it. :) Either way, I'm just wondered if you'd tried it with the shorter path.Anatomist
So I just had a look on a machine, and both bash.exe and sh.exe in Git\binare 32kb, so I suspect they're just pointers to the real location. I do think Git\bin is a better default, that seems like the path you're supposed to use. I tried another machine with an older Git installation (in Program Files (x86)) and \usr\bin wasn't actually there.Freund
You can create symlinks in Windows with mklink. (bonus information nugget)Hickerson
Jenkins requires nohup when using sh as part of Pipeline builds (see JENKINS-33708) so I had to use C:\Program Files\Git\usr\bin in the PATH in order for it work, since nohup.exe is there.Domoniquedomph
Right @Nick. Ref: issues.jenkins-ci.org/browse/JENKINS-33708 I switched to cygwin & just configured windows path env. it worked like charmSoothsay
I had to add both C:\Program Files\Git\bin and C:\Program Files\Git\usr\bin to system env vars to get this to work, and also reboot the machine (just restarting the java web start didn't do it)Northwards
K
11

If you have only Windows agents and they all the have Git for Windows installed to the same location you can set the shell executable for all agents in the Jenkins System Configuration.

Go to Manage Jenkins > Configure System, scroll down to Shell and set the Shell executable to point to whatever shell you want to start with the Execute shell build step.

Here's an example how to set the shell which is installed with Git for Windows:

Jenkins shell configuration

Note: This won't work if you have a mixture of Windows and non-Windows agents (JENKINS-38211). It will cause similar issues if you have Windows agents where sh.exe is installed to different locations (such as a mixture of 32-bit and 64-bit Windows using the default install location for those platforms). Use this only when your environment contains only identically configured Windows nodes.

Kassia answered 6/10, 2016 at 13:20 Comment(9)
Thanks for that, that's really interesting. Do you know what that setting applies to? It's a global setting on the master machine, so I'm unclear whether it just applies to the master or to all the slaves.Freund
The global setting seems to apply to all slaves too, @gibfahn. I have three slaves, I installed Git for Windows on all of them (and master) and set the Shell executable to point to sh.exe. Now I can run jobs with Execute shell build steps on all slaves.Kassia
So if you run a jenkins job that echoes the default windows path on the machines does that not include C:\Program Files\Git\bin?Freund
No, the path has only C:\Program Files\Git\cmd (even on the slaves). I configured the Shell executable with full path to Git's sh.exe.Kassia
Okay great, sounds like that would work in any deployment as long as you always have the shell in the same place (and you want to use the same shell on all machines). Good to know, thanks!Freund
@Kassia - This will break if you have linux machines that don't have sh.exe, right?Anatomist
@Anatomist Yeah, I'd expect it to break, this works properly in Win-only environment. If you could configure the shell executable per slave you could have a mixed environment but it can't be done in Jenkins v. 1.651.3 (not from the UI at least).Kassia
@Kassia - I submitted an edit to highlight this problems with this method.Anatomist
And yes - this DOES break in mixed-OS-slave env (just added the first linux slave here...). It's best to add Git usr/bin to the PATH on Windows slaves as instructed in the accepted answer.Kassia
C
7

Install git-bash

Ensure the Git\bin folder (i.e.: C:\Program Files\Git\bin) is in the global search path, in order for Jenkins to find sh.exe

to update path in windows use following command

setx path "%path%;C:\Program Files\Git\bin"

or have a look here https://www.windows-commandline.com/set-path-command-line/

to make nohup available for Jenkins

  • mklink "C:\Program Files\Git\bin\nohup.exe" "C:\Program Files\git\usr\bin\nohup.exe"

  • mklink "C:\Program Files\Git\bin\msys-2.0.dll" "C:\Program Files\git\usr\bin\msys-2.0.dll"

  • mklink "C:\Program Files\Git\bin\msys-iconv-2.dll" "C:\Program Files\git\usr\bin\msys-iconv-2.dll"

  • mklink "C:\Program Files\Git\bin\msys-intl-8.dll" "C:\Program Files\git\usr\bin\msys-intl-8.dll"

That's it now you can run shell commands

Have fun

Crutch answered 28/8, 2020 at 12:42 Comment(2)
This answer is better than the next one, because it allows to run both Windows batch and Unix shell scripts in your Jenkinsfile.Chit
I'm still having an issue with Jenkins seeing the bash command, even after restarting there seems to be an issue for me. FATAL: Cannot run program "bash":Trixy
G
0

on the node agent configuration: check the Tool Locations, add the home path of git, for e.g C:\Program Files\Git\cmd\git.exe and save reconnect the client.it will work

Garay answered 29/9, 2021 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.