Install the latest Gerrit 2.7 on Ubuntu 12.04 - lifuzu/cafe GitHub Wiki

###prerequisite: Java
Mysql
with DB: reviewdb, and USER: gerrit2

sudo mysql -u root -p
CREATE USER 'gerrit2'@'%' IDENTIFIED BY '*******';

CREATE DATABASE git01_reviewdb;
ALTER DATABASE git01_reviewdb charset=latin1;
GRANT ALL ON git01_reviewdb.* TO 'gerrit2'@'%';

FLUSH PRIVILEGES;

###Installation

# create gerrit2 user/group
sudo addgroup --gid 400 gerrit2
sudo adduser --home /data/home/gerrit2 --uid 400 --gid 400 gerrit2
# with sudo permissions
sudo visudo
gerrit2 ALL=(ALL:ALL) NOPASSWD: ALL
# create repos folder
sudo su gerrit2
sudo mkdir /data/repos/git -p
sudo chown gerrit2:gerrit2 /data/repos -R
# install with gerrit war package
java -jar ~/downloads/gerrit-2.7.war init -d /data/home/gerrit2/review_site
# install gerrit container: jetty
tar -zxf jetty-distribution-7.6.8.v20121106.tar.gz
mv jetty-distribution-7.6.8.v20121106 /data/home/gerrit2/jetty
# modify several config files: jetty/etc/webdefault.xml jetty/etc/jetty.xml jetty/contexts/gerrit.xml jetty/etc/realm.properties
# set it boot up when restart system
sudo update-rc.d gerrit-jetty.sh defaults

###Integrate gitweb with Gerrit

sudo apt-get install gitweb
git config --file review_site/etc/gerrit.config gitweb.cgi /usr/lib/cgi-bin/gitweb.cgi
git config --file review_site/etc/gerrit.config --unset gitweb.url
sudo service gerrit-jetty.sh restart

To integrate Gitweb with Gerrit, here we need to pay attention on permission.

  1. We need add the group to 'refs/*' on 'Read' permission, AND
  2. If there is 'exclusive' on 'refs/meta/config' but don't include the group which we want them to view code through Gitweb, we also need to uncheck the 'exclusive' check box, OR add the group into 'refs/meta/config'.

###Integrate 'review' subcommand with Gerrit, and Git client

sudo apt-get install git-review

go to the root of the repo

cat .gitview
[gerrit]
host=gerrit.domain.com
port=29418
project=PROJECT_NAME

and then

git review -s

###Integrate 'note' subcommand with Git client

###Integrate Jenkins with Gerrit

  1. Select 'Gerrit Trigger' after filtering with 'gerrit' in the Jenkins plugin manager;

  2. Install 'Gerrit Trigger';

  3. And when going back to the Jenkins home page the 'Query and Trigger Gerrit Patches' menu entry has been added to the left menu;

  4. Configure gerrit using <JENKING_URL>/jenkins/gerrit-trigger/

  5. A jenkins user is created in gerrit:

root@jenkins ~# ssh -p 29418 [email protected] gerrit create-account \
  --ssh-key - jenkins < /var/lib/jenkins/.ssh/id_rsa.pub
  1. It must be able to clone the repository from the jenkins machine:
# su - tomcat6
tomcat6@jenkins:/tmp$ git clone ssh://[email protected]:29418/helloworld.git 
Cloning into helloworld...
The authenticity of host '[gerrit.domain.com]:29418 ([10.145.0.15]:29418)' can't be established.
RSA key fingerprint is ba:52:ed:8b:23:19:62:de:41:7c:17:84:e9:31:c6:c0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[gerrit.domain.com]:29418,[10.145.0.15]:29418' (RSA) to the list of known hosts.
remote: Counting objects: 105, done
remote: Finding sources: 100% (105/105)
remote: Total 105 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (105/105), 73.32 KiB, done.
  1. The jenkins user is then used to configure 'Gerrit Trigger' as shown in the screenshot at step 4. The Test Connection button shows it works by displaying 'success' to the left, with the following configerations:
    'Hostname' -> 'gerrit.domain.com'
    'Frontend URL' -> 'http://gerrit.domain.com:8080'
    'SSH Port' -> '29418'
    'Username' -> 'jenkins'
    'SSH Keyfile' -> '/var/lib/jenkins/.ssh/id_rsa'

  2. The 'Restart' button at the bottom of the page, in the 'Control' box must be clicked before clicking 'Save';

  3. Create a new job, which should be configured by clicking both 'Advanced' buttons to display the required input boxes and following the 'Gerrit Trigger' instructions ( Usage with the 'Git Plugin' section ).
    "Source Code Management" -> "Git" -> "Repository URL": ssh://gerrit.domain.com:29418/repo.git
    "Source Code Management" -> "Git" -> "Branch Specifier": **
    "Source Code Management" -> "Git" -> "Repository browser": gitweb, then type URL: http://gerrit.domain.com:8080/gitweb?p=gitweb.git
    "Build Triggers": Gerrit event (checked)
    "Gerrit Trigger" -> "Trigger on": "Patchset Created" (selected)
    "Gerrit Trigger" -> "Gerrit Project": "Pattern" -> "Plain": repo, "Branches" -> "Pattern" -> "Path": **

  4. Setting jenkins permissions on Gerrit
    The "jenkins" user is expected to "Verify" a patch and all "registered" users are expectd to review them and be able to give them a "+2". When a patch is reviewed, the registered users are expected to "Submit" the patch (i.e. push it to the git repository). The default gerrit permissions are too restrictive and need to be relaxed. The "jenkins" user is added to the group of "Non-Interactive" users.
    The gerrit projects are organized in a hierarchy that where permissions are inherited. The "All-Projects" project is the root of this hierarchy and its permissions are modified so that they are inherited by all children projects.
    The permissions to "Review", "Submit" and "Verify" need to be adjusted for the refs/heads/* Reference. The "Submit" and "Verify" permissions must to be added.
    The "Non-Interactive" can then be adjusted to match: +2 for "Label Code-Review", and +1 for "Label Verified".

  5. Triggering jenkins manually
    The "Query and Trigger Gerrit Patches" menu entry to the left of the jenkins home page allows to manually trigger jenkins on designated patches. Looking for "repo" in the search box will display all the pending reviews.
    After the "Trigger Selected" button is clicked, the corresponding jenkins job is run and its progress shows to the left.
    When the test is successfull, jenkins will notify jenkins with

ssh -p 29418 [email protected] gerrit approve 1,1 \
  --verified 1 --code-review 0

And the result will show in the web interface, in the "Verified" columns on Gerrit.

###Notes:

  1. Two database configuration files:
review_site/etc/gerrit.conf

AND

jetty/contexts/gerrit.xml
  1. Uninstall Gerrit:
sudo service gerrit-jetty.sh stop
rm -fr review_site
  1. Launch Gerrit server:
sudo service gerrit-jetty.sh start
  1. Error log of Gerrit server in review_site/logs/ and jetty/logs/
grep Exception review_site/logs/error_log | tail -1
  1. Reverse proxy (for example)
# Config an apache server
<VirtualHost *:80>
 ...
 ProxyRequests Off
 ProxyVia Off
 ProxyPreserveHost On

 <Proxy *>
  Order deny,allow
  Allow from all
 </Proxy>
 ProxyPass /gerrit/ http://gerrit.domain.com:8081/gerrit/
...
</VirtualHost>

Or

RewriteRule    ^/gerrit/(.*)$   http://gerrit.domain.com:8081/gerrit/$1 [P,QSA,L]
  1. Check the Gerrit version:
ssh -p 29418 [email protected] gerrit version
  1. Create a new project:
ssh -p 29418 [email protected] gerrit create-project new_project_name --description "'new_project_descriptions'"
  1. Sometimes, after upgrading a new version of Gerrit, when start Gerrit Jetty service, a lot of error message, just comment 'cache' sections in review_site/etc/gerrit.conf, then restart; then uncomment these configs, and restart again:
[cache]
        directory = cache
[cache "web_sessions"]
        maxAge = 1 week
[cache "projects"]
        memoryLimit = 2048

###Refereneces:

  1. http://dachary.org/?p=1716
⚠️ **GitHub.com Fallback** ⚠️