Adventures with Jenkins CI on Mac OSX & Linux

Abstract:

In an agile software development, Continuous integration became integral part of agile process and agile team won’t be considered as effective without effective and visible continuous integration(CI). Continuous integration server runs automated test after each time developers check-in into the source code repository and notify team in case build failed. Continuous integration won’t allow developers to build legacy code which in turn increase technical debt. Continuous integration helps a lot in quality of the product.

Continuous Integration
Continuous Integration

Jenkins:

Jenkins is a most popular continuous integration tool. Jenkins has been developed and maintained by Kohsuke Kawaguchi who is currently working at cloudbees. Jenkins became so powerful CI server due to it’s nice looking web interface and it’s hundred’s of plugins.

In this short tutorial, we will learn how to install jenkins on your computer.

Jenkins On Mac OSX:

  • Using Jenkins-OSX installer package

Simplest way of installing Jenkins on Mac OSX is to download 0sx-installer package () from Jenkins websiteand following the instructions. You can find this package in your ‘Downloads’ directory called ‘ jenkins-{version}.pkg’. Now current version is ‘jenkins-1.478.pkg’ .

Double click on the package, you will get installer window and follow the steps which includes Introduction, License, Destination Select (location of Jenkins), Installtion Type, Installtion and Summary. Add jenkins package in your Applications and you are done! So simple isn’t it?

Remember, this package is broken and you may get get a 404 ‘not found’ message from the winstone-server that is bundled. If you dig in your syslog, you’ll find a reviling stack trace that indicates that a file can’t be found.
Solution for this issue is to create dedicated users to run jenkins. Run following commands from your terminal

sudo dscl . create /Users/jenkins
sudo dscl . create /Users/jenkins PrimaryGroupID 1
sudo dscl . create /Users/jenkins UniqueID 300
sudo dscl . create /Users/jenkins UserShell /bin/bash
sudo dscl . create /Users/jenkins home /Users/Shared/Jenkins/Home/
sudo dscl . create /Users/jenkins NFSHomeDirectory /Users/Shared/Jenkins/Home/
sudo dscl . passwd /Users/jenkins

Then update at /Library/LaunchDaemons/org.jenkins-ci.plist file to use ‘jenkins’ user
UserName
jenkins

After doing that, the Home folder of jenkins, /Users/Shared/Jenkins/Home needs a new owner.

sudo chown -R jenkins /Users/Shared/Jenkins

Finally, unload and reload jenkins service

sudo launchctl unload -w /Library/LaunchDaemons/org.jenkins-ci.plist
sudo launchctl load -w /Library/LaunchDaemons/org.jenkins-ci.plist

If you need to access your git repos using a public ssh key, you can generate one now by logging in as the jenkins user and running ssh-keygen

Shashi-MacBook-Pro:~ user$ sudo -su jenkins
bash-3.2$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/Shared/Jenkins/Home//.ssh/id_rsa):
Created directory '/Users/Shared/Jenkins/Home//.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/Shared/Jenkins/Home//.ssh/id_rsa.
Your public key has been saved in /Users/Shared/Jenkins/Home//.ssh/id_rsa.pub.
The key fingerprint is:
42:74:b0:02:e1:09:77:32:38:c4:34:fa:d6:9c:7f:6a _jenkins@Shashi-MacBook-Pro.local
The key's randomart image is:
+--[ RSA 2048]----+
|==*.. o.. |
|+=.* . o |
|..o . o |
| . o + |
| o + . S |
| . . . |
| . . |
| Eo |
| .. |
+-----------------+

Now, open your web browser and type ‘ http://localhost:8080/’ and see jenkins installed in your Mac.

  • Downloading War file (standalone)

You can also install jenkins as a standalone application by downloading jenkins.war file from jenkinswebsite. On right hand side of the website, you will find ‘Download Jenkins’ and Java Web Archive (.war). Download the latest version. It will be downloaded in your ‘Downloads’ directory. Now open terminal and run these commands (in this case ‘Shashi-MacBook-Pro:Downloads user$’ is my home directory.

Shashi-MacBook-Pro:~ user$ cd ~/Downloads
Shashi-MacBook-Pro:Downloads user$ java -jar jenkins.war<code> 
</code>

Now, open your web browser and type ‘ http://localhost:8080/’ and see jenkins installed in your Mac.

  • Using Mac Brew package to install Jenkins  (Prefred )

I pearsonally, prefer this method of installing Jenkins on Mac as It will many problems that you may face if you install Jenkins using any of the above method.

You can’t clone private github repositories and you can’t use your own private key if you install Jenkins with above method. So I would definately recommend this method in order to instal Jenkins o Mac. Pre-requisite is You must have homebrew installed on your Mac.

I assume that, you got homebrew installed on local Mac. Now try running these commands

Shashi-MacBook-Pro:~ user$ brew install jenkins
Warning: Your Xcode (4.3.3) is outdated
Please install Xcode 4.4.1.
==> Downloading http://mirrors.jenkins-ci.org/war/1.477/jenkins.war
######################################################################## 100.0%
==> Caveats
If this is your first install, automatically load on login with:
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/jenkins/1.477/homebrew.mxcl.jenkins.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.jenkins.plist
If this is an upgrade and you already have the homebrew.mxcl.jenkins.plist loaded:
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.jenkins.plist
cp /usr/local/Cellar/jenkins/1.477/homebrew.mxcl.jenkins.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.jenkins.plist

Or start it manually:

java -jar /usr/local/Cellar/jenkins/1.477/libexec/jenkins.war

==> Summary
/usr/local/Cellar/jenkins/1.477: 3 files, 47M, built in 18 seconds

Now run following commands in order to create Jenkins user and deamon

sudo mkdir /var/jenkins
sudo /usr/sbin/dseditgroup -o create -r 'Jenkins CI Group' -i 600 _jenkins
sudo dscl . -append /Groups/_jenkins passwd "*"
sudo dscl . -create /Users/_jenkins
sudo dscl . -append /Users/_jenkins RecordName jenkins
sudo dscl . -append /Users/_jenkins RealName "Jenkins CI Server"
sudo dscl . -append /Users/_jenkins uid 600
sudo dscl . -append /Users/_jenkins gid 600
sudo dscl . -append /Users/_jenkins shell /usr/bin/false
sudo dscl . -append /Users/_jenkins home /var/jenkins
sudo dscl . -append /Users/_jenkins passwd "*"
sudo dscl . -append /Groups/_jenkins GroupMembership _jenkins
sudo chown -R jenkins /var/jenkins

Mac OS uses launchd to control daemons and agents. It’s pretty easy to create a launch daemon. Create the file /Library/LaunchDaemons/org.jenkins-ci.plist with the following content, based on the plist from the homebrew jenkins formula. You may need to update the version number in the ProgramArguments.

Shashi-MacBook-Pro:~ user$ sudo touch /Library/LaunchDaemons/org.jenkins-ci.plist<code></code>

Insert following containt into this file. You may need to change the version of jenkins accordingly.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>Jenkins</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/java</string>
<string>-jar</string>
<string>/usr/local/Cellar/jenkins/1.414/lib/jenkins.war</string>
</array>
<key>OnDemand</key>
<false/>
<key>RunAtLoad</key>
<true/>
<key>UserName</key>
<string>jenkins</string>
</dict>
</plist>

Now you need to load the daemon using

sudo launchctl unload -w /Library/LaunchDaemons/org.jenkins-ci.plist
sudo launchctl load -w /Library/LaunchDaemons/org.jenkins-ci.plist<code></code>

Or You can just rebot your Mac.

Create an ssh key

Like I said, I wanted jenkins to have its own ssh identity. This is fairly easy:

sudo -u jenkins ssh-keygen

You will see output like this

Shashi-MacBook-Pro:~ user$ sudo -u jenkins ssh-keygen
Password:
Generating public/private rsa key pair.
Enter file in which to save the key (/var/jenkins/.ssh/id_rsa):
Created directory '/var/jenkins/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /var/jenkins/.ssh/id_rsa.
Your public key has been saved in /var/jenkins/.ssh/id_rsa.pub.
The key fingerprint is:
fa:e9:2f:fa:89:f3:2a:60:d3:da:06:94:b5:63:9b:f7 _jenkins@Shashi-MacBook-Pro.local
The key's randomart image is:
+--[ RSA 2048]----+
| |
| . |
| o . |
| o + |
| . o + S |
| = + .. |
| . * ... |
| . + .oEo |
| . .+BBo. |
+-----------------+

The new key is in /var/jenkins/.ssh/id_rsa.pub and can be copied to github, or wherever you have your source code.

Jenkins on Ubuntu

Setting up Jenkins on ubuntu is fairly simple. You need to run following commands in order to achieve this

wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo aptitude update
sudo aptitude install jenkins

To update Jenkins run

sudo aptitude update
sudo aptitude install jenkins

Jenkins on CentOS

CentOS has yum package for Jenkins. Just run as a root user.

yum install jenkins

Now, open your web browser and type ‘ http://ipaddress:8080/’ and see jenkins installed.

Jenkins Homepage

Enjoy this installed interface of Jenkins. In the next tutorial, we will see how to create job in Jenkins.

Happy CI !!