The life of a developer can get a bit monotonous with the repetitive and complicated tasks that are part of it. Thankfully, we have tools that can regularly handle the boring tasks without ever complaining once you set them up. Since testing and deployment are integral components of web development, some automation makes these regular jobs much easier and manageable. When you add automation to testing and deployment, the solution can be called a “continuous integration”. Such a setup can automatically test and deploy projects, giving you more time to focus on writing code than pushing it to the servers. Here is how you can setup a continuous integration using Gerrit and Jenkins.
Gerrit is a git-based open source code review system that has a web-based interface that lets you push changes from a git client, review changes and auto-merge with the master code. Every time a change is made, patch sets for the same are made. Gerrit allows different patch set comparisons. It also has the option to add feedback or review on specific lines, or on the entire patch. Reviewers can rate the changes with one from below:
+2 – Looks good to me, approved.
+1 – Looks good to me, but someone else must approve.
0 – No score.
-1 – I would prefer that you didn’t submit this.
-2 – Block submit.
A +2 review implies the code can be submitted to the master by default. This can be configured as required.
Jenkins is a popular build server used for continuous integration. It is web based and allows build process automation as and when changes occur. Jenkins can work independently even without Git or Gerrit. It has hooks that allow event triggers, so when a change occurs, a build is made automatically. When Jenkins and Gerrit are used together, the builds will have success/failure mark to indicate if they have passed the tests. Jenkins will run the test regression after performing a build. Once complete, it will publish the test results just like one of the reviewers. In case the build fails, it will reject the patch. You can set either a -1 or a -2 depending on the quality of your CI in an event of test failure. Along with this, other code reviewers can also review the code at this point. If all the reviews have passed, the code can be pushed to a public repository.
Installation of Gerrit:
If you have mysql, you can use it for gerrit database. Create user and databases in mysql for gerrit.
mysql> CREATE USER ‘gerrit2’@’localhost’ IDENTIFIED BY ‘secret’;
mysql> CREATE DATABASE reviewdb;
mysql> GRANT ALL ON reviewdb.* TO ‘gerrit2’@’localhost’;
mysql> FLUSH PRIVILEGES;
Now run the below command to initialize the site.
# java -jar gerrit.war init –batch -d ~/
During this initialization, the gerrit service is started by default. If you want to make any changes i.e mysql database, login authentication, send email server and httpd url, you can edit the ~/
If any settings changes are made, the gerrit service must be restarted before they will take effect.
Copy the ~/
Now load your gerrit url in the browser. You can find the gerrit web url using this command also.
# git config -f ~/
Now Use login authentication using the method which you have configured on gerrit config file to login into Gerrit web interface. By default, it is OpenID. LDAP is one of the mostly used authentication methods.
Once you have successfully logged in to the Gerrit Web Page, you must login to your client machine where you will do the development.
You have to generate rsa keys for your ssh sessions. You can skip the key generation step if you already have the files.
# ls -l ~/.ssh
authorized_keys config id_rsa id_rsa.pub known_hosts
SSH Key generation:
# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
The key’s randomart image is:
| o… |
| .+o . o|
| ..+=. E..|
| .=o=.. . |
| . SB * o |
| = .. * o |
| . = . = . .|
| . o. +o++.+ |
| . o*=*Boo .|
# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCwoWTT6S1pv2LP0l6r8305njSLtoupDCw8DfGnaqW+ppfzNMtRJmAvCQpYuvE154EsGZC5csnRkUYAScOD3nri8NBsXMdD+m1mMyDlA4ssMLhWHfstJGQ5aJ84NIR+zFK9o9QmDCVYU/MYt2SZnmzi+Pdd4QbDWKisewmtun5rpy8vXWukKt9CHGTbv5lKyRVPFLuQCynKbniZLV7aQD6gsVcZFG7Hj3ezBhDAZdBrCWLldy0S61DAmdH5C94xmlSuraqHiX0pkLi4J67msvOUW6dBfeCP1VsMNz0j/91LPlOMf5I2r1fRw1s/J1z/UcsgBDyATwSKocNklkrd8+pp user@localhost
The first user to sign-in and register an account will be automatically placed into the fully privileged Administrators. Now you have privileges to create projects. Click the “Projects” Tab and click ‘Create New Projects”. Now Enter the project Name to start.
There you go, clone the project in your system and start adding your development codes.
# ssh email@example.com -p 29418
| **** Welcome to Gerrit Code Review ****|
Hi selvam, you have successfully connected over SSH.
Unfortunately, interactive shells are disabled.
To clone a hosted Git repository, use:
git clone ssh://firstname.lastname@example.org:29418/REPOSITORY_NAME.git
Connection to 192.168.56.137 closed.
# git clone ssh://email@example.com:29418/Gerrit_Test
Installation of Jenkins:
You can install jenkins using apt-get or deb package.
Jenkins config exists in /etc/default/jenkins, where you can change java location and JAVA_ARGS. By default, jenkins runs on 8080. If you are running gerrit and jenkins on same machine, the default port 8080 is already occupied by gerrit. Here you need to change the jenkins port. To change it, edit the HTTP_PORT value to run on a different port.
Now start the jenkins process.
# /etc/init.d/jenkins start
Go to your browser and the load the jenkins page with your jenkins hostname and port number.
If you have installed Jenkins 2.*, the jenkins webpage will ask the initialAdminPassword. You can find the password in the /var/log/jenkins/jenkins.log file. The log looks like below:
|Jenkins initial setup is required. An admin user has been created and a password generated.|
Please use the following password to proceed to installation:
This may also be found at: /var/lib/jenkins/secrets/initialAdminPassword
Jenkins process is run by jenkins user. So, we need to enable access for jenkins user in gerrit.
Finally, Save the configuration.
Perfect, your Gerrit and Jenkins setup is now complete.