手把手教你搭建自己的git+gerrit代码评审服务器

简介: 搭建自己的git+gerrit代码评审服务器

你可能遇到过这样的问题,不知道如何管理自己的代码。自己开发的代码,过了几天,忘记修改了什么,忘了上次改到哪了,代码突然找不到了等等,甚至容易消磨自己的耐心,成就感不高。今天就教你用git+gerrit管理自己的代码,这也是大公司的开发模式。

先来看效果吧~

e6c9ecd04c987fac6e69936445f0d32.pngccca23a56d3692c94579648fe2e75aa.png

5f2674b2b5b05e7aaacc8019ad3e932.png

下面正式开始手把手搭建教学,教程及其简单,1小时内能搭建完成,墙裂推荐搭一个!!!

我的环境:

  • 服务器:阿里云(自己的虚拟机也是可以的)
  • Ubuntu 16.04 TLS (64bit)
  • 代码版本管理工具:Git
  • web服务器:Apache
  • gerrit版本:2.13.4

安装JAVA环境

https://www.oracle.com/java/technologies/downloads/#java8

我使用的JDK为jdk-8u311-linux-x64.tar.gz,可以从获取。

安装过程:

通过xftp或者filezilla等传输工具将安装包传到虚拟机上

e280e40bb30c1a19ad61b824a61852f.png

输入以下命令解压缩到/usr/local

tar -zxvf jdk-8u311-linux-x64.tar.gz -C /usr/local/

接着设置JAVA环境变量,输入vi ~/.profile打开文件,跳到文件末尾输入一下三行(vim编辑器可以通过shift + g直接跳转到行末尾)

export JAVA_HOME=/usr/local/jdk1.8.0_311export JRE_HOME=$JAVA_HOME/jreexport CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH

a8ee65a0f9da9879eb6edac82d8f56d.png

到此JAVA环境就设置好了,输入java -version检验一下是否配置成功。

出现以下内容,说明成功了~

640.png

安装git

sudo apt-get install gitgit --version

640.png

安装成功~如果遇到问题的话自行百度吧,多半是需要更新软件源, 软件包之类的。

Apache web服务器安装

sudo apt-get install apache2sudo /etc/init.d/apache2 start

640.png

安装完成~

安装gerrit & 配置

https://www.gerritcodereview.com/

从gerrit官网下载安装包,需要翻墙才能访问,也可以到网上自己找安装包下载,或者直接加我发你安装包都可以。我用的是gerrit-2.13.4.war

640.png

紧接着安装

/* * 创建gerrit服务器根目录,用于存放gerrit相关数据 * 如数据库文件,日志文件等等 */mkdir ~/review_site
// 安装java -jar gerrit-2.13.4.war init --batch -d ~/review_site

配置gerrit

vi ~/review_site/etc/gerrit.config

没有注释部分保持默认即可。

[gerrit]        basePath = git        serverId = f9036676-7b5a-4366-b616-24423e2d6226        canonicalWebUrl = http://172.17.34.15:28888/ #gerrit服务器的管理页面,监听8081端口[database]        type = h2        database = /root/review_site/db/ReviewDB[noteDb "changes"]        disableReviewDb = true        primaryStorage = note db        read = true        sequence = true        write = true[index]        type = LUCENE[auth]        type = HTTP #使用HTTP Auth方式,需要使用Apache做反向代理[receive]        enableSignedPush = false[sendemail]        smtpServer = smtp.163.com #用于推送通知邮件的smtp服务器        smtpServerPort = 465        smtpEncryption = ssl        smtpUser = xxx@qq.com #邮箱用户名        smtpPass = 123456  #邮箱密码        sslVerify = false        from = CodeReview<xxx@qq.com> #用于显示推送邮件的发件人地址[sendemail]        smtpServer = localhost[container]        user = root        javaHome = /usr/local/jdk1.8.0_311/jre[sshd]        listenAddress = *:29418[httpd]        listenUrl = proxy-http://172.17.34.15:28888/ #Gerrit服务器监听该url[cache]        directory = cache


配置apache2反向代理

开启 Apache的SSL、Proxy、Rewrite等模块

cd /etc/apache2/mods-enabledln -s ../mods-available/proxy.loadln -s ../mods-available/proxy.confln -s ../mods-available/proxy_http.loadln -s ../mods-available/proxy_balancer.confln -s ../mods-available/proxy_balancer.loadln -s ../mods-available/rewrite.loadln -s ../mods-available/ssl.confln -s ../mods-available/ssl.loadln -s ../mods-available/socache_shmcb.loadln -s ../mods-available/slotmem_shm.load

输入cd /etc/apache2/sites-enabled/,再ls -al看一下,可以看到这里有个链接文件000-default.conf,可以像我这样先将其备份,然后修改它。

  • 640.png
ServerName 172.17.34.15<VirtualHost *:8080>    ProxyRequests Off    ProxyVia Off    ProxyPreserveHost On    AllowEncodedSlashes On    RewriteEngine On    RewriteRule ^/(.*) http://172.17.34.15:28888/$1 [NE,P]
    <Proxy *>          Order deny,allow          Allow from all    </Proxy>
    <Location /login/>        AuthType Basic        AuthName "Gerrit Code Review"        Require valid-user        AuthBasicProvider file        AuthUserFile /etc/apache2/passwords    </Location>
    ProxyPass / http://172.17.34.15:28888/        # 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
  ServerAdmin webmaster@localhost  DocumentRoot /var/www/html
  # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,  # error, crit, alert, emerg.  # It is also possible to configure the loglevel for particular  # modules, e.g.  #LogLevel info ssl:warn
  ErrorLog ${APACHE_LOG_DIR}/error.log  CustomLog ${APACHE_LOG_DIR}/access.log combined
  # For most configuration files from conf-available/, which are  # enabled or disabled at a global level, it is possible to  # include a line for only one particular virtual host. For example the  # following line enables the CGI configuration for this host only  # after it has been globally disabled with "a2disconf".  #Include conf-available/serve-cgi-bin.conf</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

像我这样修改即可,只需把那些ip地址改成自己对应的就行,如果是云主机要用内网的ip地址,虚拟机的话用ifconfig查看IP地址就行。


配置Apache监听端口

直接vi /etc/apache2/prots.conf

640.png640.png

在这里添加监听8080端口

添加Gerrit登录账号

touch /etc/apache2/passwordshtpasswd -b /etc/apache2/passwords zrc 123456

注意:gerrit以第一次登陆的用户作为管理员,管理员才有资格创建project,也可以再设置其他管理员。

动Gerrit服务

sudo ~/review_site/bin/gerrit.sh start

成功回如下:

Starting Gerrit Code Review: OK

sudo /etc/init.d/apache2 start

成功回显如下:

[ ok ] Starting apache2 (via systemctl): apache2.service

登陆gerrit

在浏览器中输入http://192.168.x.x:8080,进入Login页面,输入账号密码登录Gerrit系统。注意:第一次登陆账号为管理员

640.png

管理员才有这个Create New Project

配置公钥

cd ~#生成rsa公钥对ssh-keygen -t rsa
#查看rsa公钥cat ~/.ssh/id_rsa.pub

640.png

640.png

创建project

640.png

点击crete project完成创建。

    #配置git用户名和email,注意此处email必须和gerrit上注册的email一致,否则无法上传代码.git config --global user.name "zrc"git config --global user.email xxx@qq.com
    #拉项目代码到本地git clone ssh://zrc@192.168.xxx.xxx:29418/imx6ull
    #以下命令用来在commit-msg中加入change-id,gerrit流程必备gitdir=$(git rev-parse --git-dir); scp -p -P 29418 admin@192.168.1.168:hooks/commit-msg ${gitdir}/hooks/

    使用Gerrit评审代码

    git push origin HEAD:refs/for/master

    通常到这一步,大公司的步骤是需要门禁+1,也就是label,然后是评审人+1,评审人+2, 然后合入。所以,你也可以找小伙伴帮你看代码规范,你也可以自己merge。

    640.png

    搭建教程完毕,更多的git命令用到自行网上搜索吧~

    640.png

    号主:一枚机械专业本科生,经历了转行,从外包逆袭到芯片原厂的Linux驱动开发工程师,深入操作系统的世界,贯彻终身学习、终身成长的理念。平时喜欢折腾,寒冬之下,抱团取暖,期待你来一起探讨技术、搞自媒体副业,程序员接单和投资理财。【对了,不定期送闲置开发板、书籍、键盘等等】。

    如果你想了解我的转行经验,欢迎找我交流~gongzhong号【哆哆jarvis】

    一起不断探索自我、走出迷茫、找到热爱,希望和你成为朋友,一起成长~

    相关文章
    |
    3月前
    |
    存储 Java 开发工具
    WinServer服务器上搭建Git代码库
    本文介绍如何在WinServer服务器上搭建Git代码库。
    73 0
    |
    5月前
    |
    开发工具 git
    服务器定时自动拉取Git仓库代码自动部署
    服务器定时自动拉取Git仓库代码自动部署
    130 0
    |
    2月前
    |
    安全 Shell 网络安全
    Git学习---Git快速入门、Git基础使用、Git进阶使用、Git服务器使用(IDEA集成GitHub、Gitee、GitLab)、GitHub Desktop客户端
    Git学习---Git快速入门、Git基础使用、Git进阶使用、Git服务器使用(IDEA集成GitHub、Gitee、GitLab)、GitHub Desktop客户端
    131 0
    |
    4月前
    |
    存储 Linux 网络安全
    Git - Centos7下安装GitLab服务器
    Git - Centos7下安装GitLab服务器
    97 1
    |
    6月前
    |
    Shell Linux 网络安全
    git实战—Gerrit配置SSH key & 下载代码到本地 & 使用VScode编辑器编辑提交代码——2023.07
    git实战—Gerrit配置SSH key & 下载代码到本地 & 使用VScode编辑器编辑提交代码——2023.07
    306 0
    |
    6月前
    |
    网络安全 开发工具 git
    如何搭建你自己的 Git 服务器呢?
    假如您既不想公开源代码,又舍不得给GitHub交保护费,那就只能自己搭建一台Git服务器作为私有仓库使用。
    如何搭建你自己的 Git 服务器呢?
    |
    Java Go API
    Git2.29让Git成功“牵 手”Gerrit
    Git 2.29.0 于 2020年10月发布,其中包含了两个阿里巴巴贡献的新特性。阿里巴巴贡献的新特性让 Git 牵手 Gerrit,让 GitHub 模式的代码平台可以像 Gerrit 一样工作。
    10740 0
    Git2.29让Git成功“牵 手”Gerrit
    |
    14天前
    |
    缓存 数据可视化 网络安全
    Git命令大全
    Git命令大全
    46 1
    |
    18天前
    |
    开发工具 git
    Git教程:深入了解删除分支的命令
    【4月更文挑战第3天】
    37 0
    Git教程:深入了解删除分支的命令
    |
    1月前
    |
    存储 Shell Linux
    【Shell 命令集合 文件管理】Linux git命令使用教程
    【Shell 命令集合 文件管理】Linux git命令使用教程
    34 0