Jenkins + Gerrit + Git

Source: Internet
Author: User
Tags ssh access git clone
Gerrit is a code review system that is widely used by Android open source projects. A common user clones a project from a remote git library to the local, modifies the code, and cannot push directly to the Code center, but is submitted to the GIT library in Gerrit for review through Git review, which requires a separate installation. Gerrit the relevant auditor to see the review information, judge whether to pass, through that commits commit. The Gerrit code then completes the synchronization with the remote Git library.
The essence of Gerrit is that it is not allowed to synchronize local modifications directly to the remote repository. The client must first push to the refs/for/* branch of the remote warehouse for approval.

Gerrit can also compare the state of content before and after the code is audited.

For a lot of time, we want to run the Code analysis and unit tests before review the code, and then do the human review, which can be implemented using Jenkins automated deployment.

When the user git review commits, the code is submitted to Gerrit's temporary git library. So need when the Gerrit Project warehouse has the change, first through the Jenkins to carry on the automation test and the code analysis, after Jenkins test passes, hits the verified, then hand over to the artificial review, after the artificial review passes, automatically synchronizes to the remote git storehouse. Gerrit with a Replication function, at the same time we install Gerrit when the default installation of this Plugin, by adding replication.config to Gerrit can be.


User git Review submit code-> submit to Gerrit Library-> trigger Jenkins Automated test, test through verified-> manual audit Review,review through-> Gerrit execute replica tion-> push Git remote


Gerrit is a web-based service that provides the review functionality of the code, Gerrit is sure to start the Web server and install Gitweb.


1. When the user git review, the code is automatically submitted to the Gerrit Library instead of the remote git library: Add the. Gitreview configuration information in the engineering directory, specify the Gerrit library, and the user needs access to the Gerrit Library, through SSH access

2. Jenkins to integrate the trigger function of Gerrit: Install Gerrit Trigger Plugin plug-in, and configure Trigger timing conditions

3. Jenkins configuration Automation test and Code Analysis function: Implement code analysis through Sonarqube

4. Jenkins the code after passing the test Verified:gerrit to allow Jenkins to communicate directly with Gerrit by starting the SSH service Jenkins to add Jenkins users and configure Gerrit Server information and can request success

5. The Gerrit Configuration code review takes two steps: Jenkins verified and Artificial review: Adds Jenkins user to the designated None-interactive-group and has verified function.

6. Gerrit configuration replication.confg, after code review is passed, automatically merges patch into Master of remote Git libraries: You need to add SSH public Key on a remote git library. and configure config and known_hosts on the Gerrit server.


1. Create User SSH

#ssh-keygen-t rsa-c michaeltang@openmore.org

# Cat ~/.ssh/id_rsa.pub

Add id_rsa.pub to the SSH public key of the newly created user on Gerrit.

Add. Gitreview Configuration for Git project

# vim. Gitreview

1 2 3 4 [Gerrit] host=xx.xx.x.xx port=29418 project= test-project1.git

2. Jenkins installation Gerrit Trigger

jenkins-> System Management-> Management plugin-> available plugins-> search Gerrit trigger-> Installation

Configure Gerrit Trigger

Jenkins System Management->gerrit Trigger


Can connect Gerrit Server under test.

Automatic Scanner Shell

Cd/tmp
# Place code in TMP
if [D-sonar_tmp];then
        echo ' sonar_tmp exist '
        rm-rf sonar_tmp/*
Else
        mkdir sonar_tmp
fi

cd sonar_tmp
# clone code
git clone http://xx.xx.xx.xx/p/ipottery.git
CD *
# Get all the changes
git fetch http://xx.xx.xx.xx/p/ipottery.git refs/changes/*:refs/changes/*
# get the latest modifications
commitno= ' tail-n1. Git/fetch_head | awk ' {print$1} '
# merged into the current temporary branch
git merge $CommitNo
/home/ gerrit/sonar-scanner-3.0.3.778-linux/bin/sonar-scanner-dsonar.host.url=http://sonar.wenxy.cc- Dsonar.sourceencoding=utf-8-dsonar.projectname=pottery_code_review-dsonar.projectversion=1.0.1- Dsonar.projectkey=pottery_code_review-dsonar.sources=src-dsonar.projectbasedir=.



3. Jenkins Automation test and Code Analysis

1> Create Jenkins Job

2> git Select repository URL to fill Gerrit Library address: Http://xx.xx.xx.xx:7081/p/testgerrit.git

3> Build the trigger, select Gerrit Event, select the server name in the front 2 configuration, select Plain/testproject/plain/master in Dynamic Trigger configuration, In the build select Execute Sonarqube Scanner


4. Jenkins verified

1> creates the Jenkins user in the Gerrit and generates the SSH key for Jenkins, adding the public key to Jenkins's Gerrit account.

2> support for all Gerrit add verified features

$ git Remote add origin ssh://gerrit@103.10.86.30:29418/all-projects
$ Git pull origin refs/meta/config
$ vim Pro Ject.config
[Label "verified"]
    function = Maxwithblock
    value =-1 fails
    value = 0 No score
    value = +1 Verified
$ git commit-a-m ' Updated permissions '
$ git push origin Head:refs/meta/config

5. Jenkins Verified and Artificial review

1> Add Jenkins users to the Non-interactive user group

Projects->list->all-projects
Projects->access
Global capabilities->stream Events Click Non-interactive Users
Add Jenkins@zjc.com users to ' non-interactive users ' group

2> added support for Jenkins verified in Gerrit

Projects-> List-> all-projects
Projects-> Access-> Edit-> Find reference:refs/heads/* item-> Add Permission-> Label verified-> Group Name Enter non-interactive Users-> carriage Return or click the Add button-> Save the changes at the bottom click Save Changes.


6. Configure Replication.config

# cd/home/gerrit/gerrit_site/etc/
# Vim Replication.config

1 2 3 4 5 6 7 [Remote "Test-project1"] projects = test-project1 URL = git@xx.xx.xx.xx:dev-group

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.