likes
comments
collection
share

Building Your Team-Gerrit源码仓储环境搭建

作者站长头像
站长
· 阅读数 2

工欲善其事,必先利其器。

本文主要总结团队协助中,Gerrit源码仓储环境搭建。

下文将分docker环境以及单机环境分别说明。

简介

Gerrit是一个用于代码审查和版本控制的Web基础软件。它最初是为了支持Android开发而创建的,后来成为许多团队和项目进行代码审查的首选工具之一。

以下是Gerrit的一些主要特点和功能:

  1. 代码审查:Gerrit的核心功能是代码审查,允许开发团队对提交的代码进行评审和讨论。审阅者可以提出评论、建议和修改请求,从而改善代码质量和合并流程。
  2. Git支持:作为一个基于Git的工具,Gerrit完全支持Git的版本控制功能,能够管理代码的版本、分支和合并请求。
  3. 权限控制:提供灵活的权限管理系统,可以对不同用户和团队设置不同的权限,控制代码审查和合并的流程。
  4. 集成:Gerrit可以与其他工具(如Jenkins、Jira等)进行集成,支持CI/CD流水线的自动触发和协同工作。
  5. Web界面:拥有直观友好的Web界面,使得代码审查过程更加可视化和易于管理。

Gerrit的主要目标是提供高效的代码审查和管理工具,以确保代码质量和团队合作。它被广泛用于许多开源项目和企业内部团队,特别是那些重视代码质量和团队合作的项目。

Docker版安装

环境准备

Docker 环境安装

开放端口号

#开放9003端口号 
firewall-cmd --zone=public --add-port=9003/tcp --permanent  
#重载防火墙规则 s
udo firewall-cmd --reload

jenkins工作目录

/data/platform/03_gerrit/workspace

安装配置

Docker环境下安装gerrit

参考github.com/openfrontie…

获取gerrit镜像

 docker pull openfrontier/gerrit  

运行gerrit

compose

version: '3'
services:
  web:
    container_name: gerrit-web
    image: httpd
    ports:
      - '9002:80'
    privileged: true
    volumes:
      - './httpd.conf:/usr/local/apache2/conf/httpd.conf'
      - '/data/platform/03_gerrit/workspace/etc/passwords:/usr/local/apache2/conf/passwords'
    restart: always
  core:
    image: openfrontier/gerrit
    ports:
      - '29418:29418'
      - '9003:8080'
      - '25:25'
    container_name: gerrit
    volumes:
      - '/data/platform/03_gerrit/workspace:/var/gerrit/review_site'
      - '/etc/localtime:/etc/localtime'
      - '/etc/timezone:/etc/timezone'
    environment:
      - 'WEBURL=http://local_ip:9003'
      - 'AUTH_TYPE=HTTP'
      - 'SMTP_SERVER=smtp.avit.com.cn' 
    networks:
      - gerrit
    restart: always

启动

docker-compose -p gerrit --compatibility up -d

停止

docker-compose --compatibility down 

访问gerrit容器

docker exec -it gerrit-core bash

查看日志

docker logs -f gerrit-core

查看镜像与容器信息

docker images  # 查看镜像
docker ps -a  # 查看所有容器服务的状态
docker port gerrit-core  # 查看端口映射
docker inspect gerrit-core #查看容器的具体信息

登录及基础配置

登录Gerrit

Docker运行Gerrit成功后,访问地址:http://local_ip:9002

httpd.conf


<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com
    ServerName localhost

    ProxyRequests Off
    ProxyVia Off
    ProxyPreserveHost On
    <Proxy *:8080>
          Order deny,allow
          Allow from all
    </Proxy>

    <Location /login/>
      AuthType Basic
      AuthName "Avit Gerrit Code Review"
      Require valid-user
      AuthBasicProvider file
      AuthUserFile  /data/platform/03_gerrit/workspace//etc/passwords
    </Location>

    AllowEncodedSlashes On

    ProxyPass / http://127.0.0.1:8080/ nocanon
</VirtualHost>

单机版安装

环境说明

安装服务器:Ubuntu 18.04.5 LTS

安装路径: /home/gerrit/gerrit_site

配置路径: /home/gerrit/gerrit_site/etc/gerrit.config

安装步骤

创建gerrit账号

sudo useradd -m gerrit
sudo passwd gerrit 

或者

sudo adduser gerrit

官网下载war包

https://www.gerritcodereview.com/releases-readme.html

由于最新版本需要java10支持,目前选择支持java8的gerrit-3.1.11.war

安装

java -jar gerrit.war init -d gerrit_site

设置鉴权方式

编辑/home/gerrit/gerrit_site/etc/gerrit.config修改auth如下

[auth]
        type = HTTP
#       httpHeader = SM_USER

配置邮箱

编辑/home/gerrit/gerrit_site/etc/gerrit.config,修改sendmail如下:

[sendemail]
        enable = true
        smtpServer = smtp.qiye.163.com
        smtpServerPort = 465
        smtpUser = xxx
        from = 武研CodeReview<pmo@avit.com.cn>
        smtpEncryption = ssl
        sslVerify = true
        smtpPass = xxx

配置Apache反向代理

1.编辑/etc/apache2/ports.conf 文件,新增Listen 9003

gerrit@wh-stb:/etc/apache2$ cat ports.conf 
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 9003

<IfModule ssl_module>
        Listen 443
</IfModule>

<IfModule mod_gnutls.c>
        Listen 443
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
gerrit@wh-stb:/etc/apache2$

2.创建/etc/apache2/sites-available/apache2-gerrit.conf内容如下

<VirtualHost *:9003>
    ServerName localhost
    ProxyRequests Off
    ProxyVia Off
    ProxyPreserveHost On
    <Proxy *:9003>
          Order deny,allow
          Allow from all
    </Proxy>

    <Location /login/>
      AuthType Basic
      AuthName "Gerrit Code Review"
      Require valid-user
      AuthBasicProvider file
      AuthUserFile  /home/gerrit/gerrit_site/etc/passwords
    </Location>

    AllowEncodedSlashes On

    ProxyPass / http://192.168.3.162:8080/ nocanon
</VirtualHost>

3.生效配置文件

 sudo a2ensite apache2-gerrit

(note: here the apache2-gerrit is the name of apache2-gerrit.conf)

重启apache2服务

 sudo service apache2 restart
  1. 编辑/home/gerrit/gerrit_site/etc/gerrit.config,修改canonicalWebUrlhttpd选项

    [gerrit]
            basePath = git
            serverId = e4dfb263-74f0-4175-bb83-16aaa462791d
            canonicalWebUrl = http://192.168.3.162:9003/
    ....
    [httpd]
            listenUrl = http://*:8080/
    

添加Gerrit账号

touch /home/gerrit/gerrit_site/etc/passwords
htpasswd -b gerrit_site/etc/passwords admin 111111

第一次创建登录的用户名密码将视为admin管理员账号

启动

./bin/gerrit.sh start

访问 http://192.168.3.162:9003/

错误排除

Apach2配置报错

a2ensite apache2-gerrit
Invalid command 'ProxyRequests', perhaps misspelled or defined by a module not included in the server configuration

代理模块未打开

修正

a2enmod proxy
a2enmod proxy_http
a2ensite apache2-gerrit
service apache2 reload

gerrit错误日志路径为/home/gerrit/gerrit_site/logs/error_log

启动报错

No index versions for index 'changes' ready; run java -jar /home/gerrit/gerrit_site/bin/gerrit.war reindex --index changes

1 error

修正

java -jar /home/gerrit/gerrit_site/bin/gerrit.war reindex --index changes

项目列表显示不全

重建索引

./bin/gerrit.sh stop
 java -jar gerrit.war reindex-d gerrit_site
./bin/gerrit.sh start

登录出现forbidon

原因分析:创建用户的顺序错了,先通过gerrit create-account命令创建账号,然后再通过htpasswd创建

正确的顺序是

sudo htpasswd -b ~/review_site/passwords youname  youpasswd//创建帐号
sudo ssh -p 29418 admin@localhost gerrit set-account --add-email youname@corp.to8to.com youname   //设置youname 普通用户的邮箱

用htpasswd命令创建HTTP认证时,并没有在gerrit数据库中创建账号信息,当第一次登陆成功后,gerrit会自动创建同名的gerrit用户。

解放方法

使用gerrit的管理员账户,进行如下操作

git clone ./review_site/gitRepo/All-Users.git/ All-Users

git fetch origin refs/meta/external-ids:refs/meta/external-ids    //获取External IDs的分支

git checkout refs/meta/external-ids

git log

找到保错的提交commit号,也就是通过create-account命令创建新用户时,生成的用户信息

git show fa89bc7791e1dfd3fb6c595b3cf9969a0b60beaa

使用git show查看有哪些文件在这次提交中产生。

直接删除相关文件,git push

rm 26bc754712fae1343ffb1498ee735892794032a0
rm 521dd9a3899b76a3e30b8b991c9a8b71da16b2b7
git add .
git commit -am "remove user youname "
git push origin HEAD:refs/meta/external-ids

后还有一步,更新引用,应用到 NoteDb

git update-ref refs/meta/external-ids $(git rev-parse HEAD)

参考

gerrit-documentation.storage.googleapis.com/Documentati…

www.gerritcodereview.com/releases-re…