Integrating Jenkins with Gerrit to build CI Environment

CI(Continuous Integration)
Continuous Integration is a software development practice, where the code changes are frequently, integrated into a shared repository and each integration the builds are validated through automated tests.

Gerrit is a free and OpenSource web-based collaborative code review tool, which was integrated with git. It also has the feature to maintain the history of work and allows developers to merge the code changes after the Core Review. It was developed by Shawn Pearce at Google which is written in Java, Servlet, GWT(Google Web Toolkit). Install Gerrit by as follows,

1) Installing Java 
Java is required to run the Gerrit application. So, install java as follows 
sudo apt install openjdk-8-jdk 
Check the version of java as follows 
java -version 
javac –version 
Set the variable for your system as follows 
echo "JAVA_HOME=$(which java)" | sudo tee -a /etc/environment 
source /etc/environment 
echo $JAVA_HOME 
2) Installing Git 
Install git as follows 
apt install git 
If git already installed skip this step 
3) Installing haveged 
Install haveged as follows 
apt-get install haveged 
update-rc.d haveged defaults 
service haveged start 
4) Download Gerrit
5) Instal Gerrit
java -jar gerrit*.war init --batch --dev -d ~/pyro_gerrit
After executing above command you may get a following message,
Initialized /root/pyro_gerrit
Reindexing projects: 100% (2/2)
Reindexed 2 documents in projects index in 0.4s (5.4/s)"
Note : ~/pyro_gerrit is a folder where gerrit to be installed.
6) Start Gerrit
bash pyro_gerrit/bin/ start
After executing above command you may get a following message,
Starting Gerrit Code Review: OK
7) Check Gerrit Installation
Check gerrit installation by using url by as follows,
Gerrit WebUI

Installing Jenkins
Install Jenkins by following below link,

Gerrit Trigger Plugin
This plugin integrates Jenkins to Gerrit code review for triggering builds when a “patch set” is created.

Configuring Gerrit Trigger Plugin in Jenkins

Prerequisites for Gerrit Trigger Plugin 
i) Gerrit registered user  
ii) Jenkins user’s SSH key(/var/lib/jenkins/.ssh/id_rsa)
should be added to Gerrit Server.
1) Installing Gerrit Trigger Plugin  
Open a Plugin manager in Gerrit by as follows,  
Manage Jenkins —> Manage Plugins 
In Plugin manger’s page, Select the available tab and search
for Gerrit Trigger Plugin and install that plugin.

Gerrit Trigger Installation
2) Configuring Gerrit Trigger Plugin 
i) Login to the jenkins server as a jenkins user and generate ssh-keypair
using following command ssh-keygen.
ii) copy the generated ssh public key and add that into the gerrit server's ssh keys
iii) Open a Gerrit trigger by as follows, 
Manage Jenkins ----> Gerrit Trigger

iv)  After Opening Gerrit trigger PlugIn select the Add New Server Option and add 
the name for the server and select Gerrit Server with Default Configuration.
Continuous Integration Gerrit jenkins by Pyronoid Ninja
Continuous Integration by Gerrit Trigger
v) Add the Gerrit Hostname, Gerrit Frontend URL, Gerrit Username,SSH Keyfile
Path(/var/lib/jenkins/.ssh/id_rsa) , SSH Keyfile Password (if required). After
adding these values click on the Test Connection button, if the Result status is
Success. The Gerrit Integration with Jenkins is done. After this
Save the Server Details
Note: In Gerrit by default admin user will be there, use that as a Gerrit Username
or create a new user. ssh-keyfile and ssh keyfile password which was created
in first step. 
Continuous Integration Gerrit jenkins by Pyronoid Ninja
Continuous Integration by Gerrit Trigger Configuration
v) Refresh Server Status  
After Saving the Server Status will show as Red button.
Refresh that status by clicking on the red button, after that will be changed to blue 
Continuous Integration Gerrit jenkins by Pyronoid Ninja

Now Gerrit is successfully integrated with Gerrit. It connects to the Gerrit server using ssh and uses the Gerrit stream-events command