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?

4 Answers 4

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.

8
  • C:\Program Files\Git\bin also has a sh.exe in it. Any reason not to use that?
    – BitwiseMan
    Commented Dec 7, 2016 at 22:47
  • @BitwiseMan No idea. Is that a symlink?
    – gib
    Commented Dec 9, 2016 at 19:17
  • Symlink on windows? Doubt it. :) Either way, I'm just wondered if you'd tried it with the shorter path.
    – BitwiseMan
    Commented Dec 13, 2016 at 23:30
  • 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.
    – gib
    Commented Dec 15, 2016 at 11:57
  • 5
    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.
    – Nick Jones
    Commented Mar 23, 2018 at 17:50
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.

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.
    – gib
    Commented Oct 6, 2016 at 14:57
  • 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.
    – apa64
    Commented Oct 7, 2016 at 10:02
  • 1
    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!
    – gib
    Commented Oct 10, 2016 at 10:37
  • 1
    @apa64 - This will break if you have linux machines that don't have sh.exe, right?
    – BitwiseMan
    Commented Dec 7, 2016 at 22:48
  • 1
    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.
    – apa64
    Commented Jun 9, 2017 at 10:14
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

2
  • 2
    This answer is better than the next one, because it allows to run both Windows batch and Unix shell scripts in your Jenkinsfile. Commented Jan 14, 2021 at 14:19
  • 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":
    – bnns
    Commented Jul 26, 2021 at 13:03
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.