43

I recently installed Jenkins, using Homebrew. I also installed Mercurial using Homebrew.

I can successfully clone an hg repo just fine - from Terminal. But if I try and do the same thing as part of a job in Jenkins, it fails.

So, in by job, I told Jenkins to run a shell script echo $PATH. Sure enough, the path /usr/local/bin is not there. If I execute the same command from Terminal, it's there.

So, what is the best way to modify PATH so that Jenkins is pulling the same PATH that I'm using, when I echo it from Terminal?

Note, Jenkins is running from the same user account that I'm logged into doing these tests, so I can't fathom why this is happening.

Stefan Crain
  • 2,010
  • 3
  • 21
  • 22
Dan Morrow
  • 4,433
  • 2
  • 31
  • 45
  • also, I'm having jenkins run from my account using launchd. So, whenever my account "logs in", it powers up Jenkins. – Dan Morrow Mar 25 '13 at 17:27
  • 2
    +1, I thought I was losing my marbles, glad it's not just me. This never used to happen in older versions, is it a new "feature"?? – funkybro Apr 03 '13 at 12:28

6 Answers6

23

In your launchd .plist file for Jenkins, you can set the PATH environment variable by using the following:

<key>EnvironmentVariables</key>
<dict>
    <key>PATH</key>
    <string>(insert your path value here)</string>
</dict>

That should set the PATH to whatever you need.

gaige
  • 17,263
  • 6
  • 57
  • 68
  • 2
    Can the value be "$PATH:/my/other/path"? I need to append to path. – Dan Morrow Mar 25 '13 at 18:33
  • I don't believe so. As far as I know the $PATH interpretation is done by the shell and that's not going to get run though for this setting method, so you need to give a full path list here. – gaige Mar 25 '13 at 19:11
  • To clarify my above comment. As far as I know "$PATH" interpretation is something that is normally done by the shell, and thus won't be used as expansion for the launchd .plist file. Therefore, you should give a full path list of the form "/foo/bar/zot:/usr/local/bin:/usr/bin:/whatever/you/want/" – gaige Mar 25 '13 at 19:25
  • Thank you! It's amazing this is not listed more often. I was having so many issues trying to get the jenkins path set right. – chourobin Jul 29 '13 at 21:06
  • Basically at the top level of the plist. If you don't know the format of the file, you should `man plist` from terminal to see the details, but basically it alternates between key and value. – gaige Oct 03 '13 at 00:03
  • 2
    I tried this for a Jenkins slave on OSX 10.10, but the for whatever reason the PATH wouldn't change. I could set other variables... but not PATH. Eventually, I just set PATH as an environment variable in the Jenkins node configuration. – cgmb Jun 15 '15 at 21:04
  • 2
    After executing `brew services restart jenkins`, the configuration will be reset. – AechoLiu Feb 04 '16 at 10:15
  • 2
    @gagarine You'd insert this just inside the root level `` tag. A good example is here: http://serverfault.com/a/128693/1687 – Kip Apr 25 '16 at 17:21
  • This helped when I changed my `JENKINS_HOME` path. Thanks! – Cole Jun 01 '18 at 05:50
  • This answer is good, but it could be improved by adding the full snipped of the plist, so people knows exactly where to insert the `EnvironmentVariables` key and don't get it wrong. Also it could mention where the plist is located, at least if a brew installation was meant. Mine was at `~/Library/LaunchAgents/homebrew.mxcl.jenkins-lts.plist` – ceztko May 05 '21 at 07:50
14

For some reason, Jenkins doesn't keep /usr/local/bin in the PATH when connecting to a slave.
You can add it to the PATH either by

  • Adding an environment variable on the Node Configuration, or

  • Adding a .bashrc file on the user folder with

     PATH="/usr/local/bin:${PATH}"
    

Note: The Mac client must be disconnected and then reconnected via the Jenkins web app after editing ~/.bashrc

Community
  • 1
  • 1
Maxime
  • 1,095
  • 1
  • 11
  • 20
  • Editing the .bashrc file didn't seem to work for me. – jxramos Jan 03 '19 at 19:08
  • However, editing .bash_profile for the jenkins user on the Node did the trick for getting the path visible in the `sh` block of a Jenkinsfile – jxramos May 14 '19 at 19:29
  • I spent total 3 days, and at the end this worked, and helped me to run my jenkins job from windows(master) to macos(slave). Thanks a lot. – Pankaj Parkar Aug 13 '19 at 13:53
4

I found that even setting the PATH environment variable for the node didn't work for the hombrew Mercurial installation. The path WOULD get set, but only for the script build phase, not for the VCS checkout phase. Here's what I wound up doing.

  1. Go into Manage Jenkins -> Configure System
  2. Add a new Mercurial Installation
  3. Name it whatever you want (I named mine build-mac)
  4. For Installation Directory I put /usr/local
  5. The Executable parameter was pre-set to INSTALLATION/bin/hg, so I just left it at that.
  6. Everything else in here can be left blank
  7. Go into your job and edit the configuration
  8. Under Source Code Management set Mercurial Version to the mercurial installation you just added.
  9. Save
  10. $$$

Hope that helps anyone else running into this same problem, now that we're not allowed (by default anyways) to do anything inside of /usr/bin anymore. Previously I would have just symlinked hg there, but now with the new "System Integrity Protection" "feature", that's no longer as trivial of task, and even more difficult if your Mac slave is headless.

Jordan
  • 4,133
  • 1
  • 27
  • 43
3

You can set PATH in launchd.conf file. See here for details. Note that man launchctl says that 'commands can be stored in $HOME/.launchd.conf or /etc/launchd.conf to be read at the time launchd starts', so you probably can create '.launchd.conf' in your home directory and use instructions from the link with this file. But as far as I know in launchd.conf file you can't add directory to PATH, you can just rewrite PATH.
You can also see here for the solution using /etc/paths.d directory

Community
  • 1
  • 1
cody
  • 3,233
  • 1
  • 22
  • 25
  • `/etc/launchd.conf` appears to be deprecated as of OSX 10.10. http://stackoverflow.com/a/26311753/331041 – cgmb Jun 15 '15 at 21:06
3

There should be no space on either side of = in following PATH modification: PATH="/usr/local/bin:$PATH"

I added it to my pre-build step on Jenkins installed on macOS.

Mayank
  • 981
  • 7
  • 9
3

Manage Jenkins -> Configure System -> Environment variables -> Add

  • Name = PATH+EXTRA
  • Value = /usr/local/bin

enter image description here

Erkki Nokso-Koivisto
  • 1,203
  • 15
  • 19
  • This is absolutely brilliant and worked for me easily and flawlessly! Where is this documented? I can't find it anywhere but it worked like a charm. Was struggling with this for some time and this was a solution like none other. I was hesitant to try any modification at the macOS or Homebrew levels since I upgrade Jenkins almost weekly. How did you discover this? So excited to find out that Jenkins has this type of "secret menu" configurations! :D – Michael J May 20 '23 at 22:54