This article describes how to use the Jenkins continuous integration 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.
For most platforms you have native packages, see the Jenkins Homepage.
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.
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
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
This makes the .jenkins folder writable and Jenkins can use it.
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:
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.
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.
On the login page, select Create an account to create the users you just gave access.
Go to Manage Jenkins, Manage and Assign Roles and then Assign Roles to grant the newly created user additional access rights.
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
M-MASTER prefix and only those.
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.
Jenkins can be extended via additional plug-ins with more functionality. You can configure your plug-ins via thelink.
To install plugins in Jenkins select use thelink 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.
This plugin allows use of Git as a build SCM.
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.
This plugin allows to run Gradle builds, e.g., as required for Android, via Jenkins.
This plugin allows to run Maven builds.
This plugin integrates Jenkins with Github projects.
Publish Over SSH Plugin
This plugin allows to publish build artifacts via ssh
Workspace Cleanup Plugin
This plugin allows to delete the workspace before the build or when a build is finished and artifacts saved.
Github Pull Request Builder
This plugin allows to build Github Pull Requests
You can manually restart Jenkins by adding
restart as URL parameter.
The build of a project is handled via jobs in Jenkins. Select. Afterwards, enter a name for the job and select Freestyle Job and press OK.
Enter a description for the job and configure how many old jobs should be retained.
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.
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.
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.
Press Save to finish the job definition. Press Build Now on the job page to validate the job works as expected.
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.
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
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.
See the following link for details: https://wiki.jenkins-ci.org/display/JENKINS/Administering+Jenkins
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
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
|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.