Home Tutorials Training Consulting Products Books Company Donate Contact us









NOW Hiring

Quick links

Share

This article describes how to use the Jenkins continuous integration build server.

1. Using the Jenkins build server

Continuous integration is a process in which all development work is integrated as early as possible. The resulting artifacts are automatically created and tested. This process allows to identify errors as early as possible.

Jenkins is a popular open source tool to perform continuous integration and build automation. The basic functionality of Jenkins is to execute a predefined list of steps, e.g. to compile Java source code and build a JAR from the resulting classes. The trigger for this execution can be time or event based. For example, every 20 minutes or after a new commit in a Git repository.

Possible steps executed by Jenkins are for example:

  • perform a software build using a build system like Apache Maven or Gradle

  • execute a shell script

  • archive a build result

  • running software tests

Jenkins monitors the execution of the steps and allows to stop the process, if one of the steps fails. Jenkins can also send out notification in case of a build success or failure.

Jenkins can be extended by additional plug-ins. For example, you can install plug-ins to support building and testing Android applications.

2. Installation and setup of Jenkins

For most platforms you have native packages, see the Jenkins Homepage.

2.1. Installing of the Jenkins server on Ubuntu

Jenkins provides Debian/Ubuntu packages which install Jenkins and register Jenkins as start service. See the Install Jenkins on Ubuntu description The Linux installation creates a /etc/init.d/jenkins script which starts Jenkins automatically at boot time.

Jenkins stores all the settings, logs and build artifacts in its home directory. The default installation directory is /var/lib/jenkins under Ubuntu.

2.1.1. Using the .war file to start Jenkins

Download the jenkins.war file from Jenkins Homepage. From this file you can start Jenkins directly via the command line with java -jar jenkins*.war.

If you start it locally, you find it running under the following URL: http://localhost:8080/

To run it in your Tomcat server, put the .war file into the webapps directory. If you start Tomcat, your Jenkins installation will be available under

http://localhost:8080/jenkins

If the jenkins.war is deployed in your webapps directory, but cannot be started and the tomcat manager says FAIL - Application at context path /jenkins could not be started, you may need to grant the permissons for JENKINS_HOME.

sudo mkdir .jenkins
sudo
chown tomcat7:nogroup .jenkins

This makes the .jenkins folder writable and Jenkins can use it.

2.2. Connect to Jenkins for the initial configuration

After installation, open a browser and connect to it. The default port of Jenkins is :8080, therefore on your local machine you find it under the following URL:

http://localhost:8080/

You will need to copy the initial password from the file system of the server.

Afterwards you can select to install plug-ins. Select the Install suggested plug-ins to get a typical configuration.

Create an admin user and press Save and Finish.

3. Configure Jenkins

3.1. Secure Jenkins

It is recommended to secure Jenkins. Select Manage Jenkins and then Configure Global Security. Select the Enable security flag. The easiest way is to use Jenkins own user database. Create at least the user "Anonymous" with read access. Also create entries for the users you want to add in the next step.

Access restrictions

On the login page, select Create an account to create the users you just gave access.

Create Jenkins account
Sign up a new Jenkins user

3.2. Create a new user

Go to Manage Jenkins, Manage and Assign Roles and then Assign Roles to grant the newly created user additional access rights.

Sign up a new Jenkins user

Navigate to Manage Roles to define access restrictions in detail. Pattern is a regex value of the job name. The following grants unregistered users read-only access to your build jobs that start with the C-MASTER or M-MASTER prefix and only those.

Sign up a new Jenkins user

3.3. Generate ssh key for Jenkins user

If you want to access a private Git repo, for example at Github, you need to generate an ssh key-pair. Create a SSH key with the following command.

sudo -u jenkins ssh-keygen

The public key must be uploaded to the service you are using, e.g., Github.

4. Jenkins management

4.1. Plug-in management

Jenkins can be extended via additional plug-ins with more functionality. You can configure your plug-ins via the Manage Jenkins ▸ Manager Plugins link.

To install plugins in Jenkins select use the Manage Jenkins ▸ Manager Plugins link and search for the plugin you want to install. Select it from the list and select to install it and restart Jenkins.

The following table is a summary of commonly used plug-ins.

Table 1. Jenkins plug-ins
Plug-in name Description URL

Git Plugin

This plugin allows use of Git as a build SCM.

https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin

Xvnc plugin

This plugin allows projects to run xvnc during a build. This allows for example to run tests which requires a display to run on a virtual display. To use this plug-in you need to connect once to your vncserver on the command line to provide a password. Use for example the following commands.

# install vncserver
apt-get install vnc4server

# switch to jenkins user
sudo su jenkins

# connect to vncserver which creates the password
vncserver :10

https://wiki.jenkins-ci.org/display/JENKINS/Xvnc+Plugin

Gradle Plugin

This plugin allows to run Gradle builds, e.g., as required for Android, via Jenkins.

https://wiki.jenkins-ci.org/display/JENKINS/Gradle+Plugin

Maven Plugin

This plugin allows to run Maven builds.

https://wiki.jenkins-ci.org/display/JENKINS/Maven+Project+Plugin

GitHub plugin

This plugin integrates Jenkins with Github projects.

https://wiki.jenkins-ci.org/display/JENKINS/Github+Plugin

Publish Over SSH Plugin

This plugin allows to publish build artifacts via ssh

https://wiki.jenkins-ci.org/display/JENKINS/Publish+Over+SSH+Plugin

Workspace Cleanup Plugin

This plugin allows to delete the workspace before the build or when a build is finished and artifacts saved.

https://wiki.jenkins-ci.org/display/JENKINS/Workspace+Cleanup+Plugin

Github Pull Request Builder

This plugin allows to build Github Pull Requests

https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin

4.2. Restart your Jenkins

You can manually restart Jenkins by adding restart as URL parameter.

How to manually restart Jenkins

5. Setting up a Jenkins job

The build of a project is handled via jobs in Jenkins. Select New Item. Afterwards, enter a name for the job and select Freestyle Job and press OK.

Setting up a Jenkins job

Enter a description for the job and configure how many old jobs should be retained.

Setting up a Jenkins job

Configure how the source code can be retrieved. If you for example using Git, enter the URL to the Git repository. If the repository is not public, you may also need to configure the credentials.

Setting up a Jenkins job

Specify when and how your build should be triggered. The following example polls the Git repository every 15 min. It triggers a build, if something has changed in the repo.

Setting up a Jenkins job

I typically delete the workspace before a build to avoid any side-effect. In the Build section you can add a build step, e.g., a Maven build.

Setting up a Jenkins job

Press Save to finish the job definition. Press Build Now on the job page to validate the job works as expected.

Setting up a Jenkins job

After a while the job should go to green or blue (depending on your configuration), if successful. Click on the job and afterwards on Console Output to see the log file. Here you can analyze the build errors.

6. Jenkins backup and copying files

Jenkins stores all the settings, logs and build artifacts in its home directory. For example, in /var/lib/jenkins under the default install location of Ubuntu.

To create a backup of your Jenkins setup, just copy this directory.

The jobs directory contains the individual jobs configured in the Jenkins install. You can move a job from one Jenkins installation to another by copying the corresponding job directory. You can also copy a job directory to clone a job or rename the directory.

You only need to copy the config.xml file. If you are using Git in the jobs directory you can use the following .gitignore file to exclude everything except this file.

# Ignore everything in /jobs
*
# Reinclude all folders in /jobs
!*/
# Ignore everything in the subfolders of /jobs
*/*
# Reinclude config.xml files in the first-level subfolders of /jobs
!*/config.xml

Click Reload Configuration from Disk button in the Jenkins web user interface to force Jenkins to reload configuration from the disk.

reload jenkins

7. Deleting Jenkins log file

Since you can’t delete a file that is currently opened by a program you need to stop all applications accessing the log file. If you are running Ubuntu the applications are Jenkins itself and the rsyslog service.

sudo service jenkins stop
sudo rm /var/log/jenkins/jenkins.log
sudo service rsyslog restart
sudo service jenkins start

If restarting your services is not an option you can truncate the deleted file.

$ sudo rm /var/log/jenkins/jenkins.log
$ sudo lsof | grep deleted | grep jenkins.log
daemon    28791                jenkins    5w      REG                8,1 166199259136    4589589 /var/log/jenkins/jenkins.log (deleted)
$ sudo su root
$ cd /proc/28791/fd
$ ls -l | grep deleted
lrwx------ 1 jenkins jenkins 64 Jul 10 19:30 3 -> /run/jenkins/jenkins.pid (deleted)
l-wx------ 1 jenkins jenkins 64 Jul 10 19:30 4 -> /var/log/jenkins/jenkins.log (deleted)
l-wx------ 1 jenkins jenkins 64 Jul 10 19:30 5 -> /var/log/jenkins/jenkins.log (deleted)
$ > 4
$ > 5

8. About this website

9.1. vogella GmbH training and consulting support

TRAINING SERVICE & SUPPORT

The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”.

The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.

Copyright © 2012-2017 vogella GmbH. Free use of the software examples is granted under the terms of the EPL License. This tutorial is published under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany license.

See Licence.