Ramonskis Blog

A Microblog powered by Octopress

Continuous Integration With Jenkins

| Comments

Jenkins is a opens source continuous integration server.

This blog post will show you how

  • to set up Jenkins in an Ubuntu 11.04 Virtual Box
  • create a simple Git controlled Project with Jenkins
  • run the build job

Build-Up

We are going to put Jenkins into a virtual Ubuntu 11.04 (Natty) Box. Therefore we use vagrant with our self made base box

1
2
3
4
5
$ mkdir ~/develop/jenkins
$ cd ~/develop/jenkins
$ git init
$ vagrant init 'myubuntubox'
$ git add Vagrantfile

Note The myubuntubox is a self created base box made with VeeWee. For a complete instruction, how to quickly build a Vagrant base box, see Stefan’s Blogpost about VeeWee

We need to customize the Vagrantfile a little bit for our needs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# -*- mode: ruby -*-

Vagrant::Config.run do |config|

    config.vm.define :jenkins do |jenkins|
        jenkins.vm.box = "myubuntubox"
        jenkins.vm.network "192.168.50.10"

        # jenkins port
        # XXX: obsolete for Vagrant 0.9
        # jenkins.vm.forward_port("jenkins", 8080, 8080)
        # use this for Vagrant 0.9:
        jenkins.vm.forward_port 80, 8080


        # postinstall script
        jenkins.vm.provision :shell, :path => "postinstall.sh"

    end

end

# vim: set ft=ruby :

Since we configured a postinstall.sh script provisioner, we need to create it as well:

1
2
3
4
5
6
$ cat postinstall.sh
#!/bin/bash

echo "postinstall script ran!" > /vagrant/postinstall.log

$ git add postinstall.sh

And finally start the virtual box:

1
2
3
4
5
$ vagrant up
[jenkins] Importing base box 'myubuntubox'...
...
[jenkins] Running provisioner: Vagrant::Provisioners::Shell...
[jenkins] stdin: is not a tty

Check if our provisioner run:

1
2
$ cat postinstall.log
postinstall script ran!

Installation

Next we are going to install Jenkins into our Ubuntu box. Therefore we are going to add the following lines into the postinstall.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/bash

log=/vagrant/postinstall.log

echo "postinstall script started!" > $log


function info {
    echo $(date +%D\ %T) $1 >> $log
}

info "Adding convenient packages..."
apt-get install -q -y vim | tee -a $log

# Jenkins Installation
info "Adding Jenkins key to system..."
wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | apt-key add - | tee -a $log

info "Adding Jenkins source to /etc/apt/sources.list..."
if grep "jenkins" /etc/apt/sources.list
then
    info "Jenkins source already in source.list"
else
    echo "deb http://pkg.jenkins-ci.org/debian binary/" >> /etc/apt/sources.list
fi

info "Updating apt source..."
apt-get update | tee -a $log

info "Installing Jenkins..."
apt-get install -q -y jenkins | tee -a $log

This Script runs now every time on:

1
$ vagrant up

or, if the box is already running:

1
$ vagrant provision

Recap – What happened so far

  • We created a new Ubuntu 11.04 (Natty) Server Box with Vagrant
  • We created a Vagrant Shell Provisioner
  • We installed Jenkins from the provided Ubuntu Package Repository
  • Jenkins will be launched as a daemon /etc/init.d/jenkins {start|stop|status|restart|force-reload}
  • Logs will be located in /var/log/jenkins/jenkins.log
  • The config is in /etc/default/jenkins
  • Jenkins listens on port 8080

If everything went good so far, we should be able to open up our favorite browser and go to http://localhost:8080.

Congratulations, Jenkins is at your command!

If something went wrong, check the postinstall.log and login to the virtual box with:

1
$ vagrant ssh

Setting up your first Project

Our first project will be on GitHub, thus, we need to install the Git Plugin first. Go to the Jenkins Plugin Manager and select the Git Plugin and the Github Plugin.

Click on “Download now and Install after restart”. On the Installation Site, click the checkbox to restart Jenkins after Installation.

Now we are ready to start our first Project.

Click on New Job and select Build a free-style software project. Set the Job Name to MyJob

Set the following Values on the Job configuration Site

  • Projectname: MyJob
  • Description: A simple test Job
  • GitHub Project: https://github.com/ramonski/jenkins-server
  • Git Repository Url: https://github.com/ramonski/jenkins-server.git
  • Important Check “Skip internal tag” Option, since we make anonymous checkouts
  • Build trigger: Build when a change is pushed to GitHub and activate the checkbox to check the source code management system periodically. Add ther a line with * * * * * which basically means to check every minute.
  • Select “Shell Script” for build

The Build Script

You can customize how to build your Project. Therefore you can use Environment Variables

For testing, we echo the values of the build number and the build id into a file:

1
2
cd $WORKSPACE
echo "I'm build number $BUILD_NUMBER with the ID $BUILD_ID" > buildinfo

First build

Ok, lets start our first build! Click on build now

The Build should be successful.

You should be able to view the buildinfo inside your workspace.

Troubleshooting

If your build failed, check the build log

If you get the following error:

1
2
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

you probably set the Read+Write Repository URL in the Project Config. Log out from GitHub and take then the (Read Only) Repository URL.

Another error could tell you this:

1
2
Command "git tag -a -f -m Jenkins Build #3 jenkins-MyJob-3" returned status code 128:
*** Please tell me who you are.

then you probably forgot to set the “Skip internal tag” on the Job configuration Site

Next Steps

Now it’s time to prepare Jenkins for the real world. You probably don’t want that everyone can maintain Jenkins through the Web. So you need to create some Users and give them the propert rights, or you can configure Jenkins to talk to your companies LDAP.

If you plan to hook the Vagrant Box into your local Network, you can enable Bridged Networking in your Vagrant Config.

There are also a lot of other cool Plugins available for Jenkins, so check out your the Plugin Manager.

A collection of some plugins we use:

  • Jenkins IRC Plugin This plugin is an IRC bot that can publish build results to IRC channels.

  • ChuckNorris Plugin Displays a picture of Chuck Norris (instead of Jenkins the butler) and a random Chuck Norris ‘The Programmer’ fact on each build page.

  • Python Plugin Adds the ability to execute python scripts as build steps.

  • Jython Plugin Adds the ability to execute Jython script as a build step in free-style software projects from within the JVM.

  • many more…

Changenotes

  • updated Vagrant File for Vagrant 0.9

Comments