手把手教你搭建自己的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】

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

    相关文章
    |
    10天前
    |
    Linux 开发工具 数据安全/隐私保护
    搭建 Git 私人服务器完整指南
    本文详细介绍了如何从零开始搭建一个私人的 `Git` 服务器。首先,文章解释了 `Git` 的概念及其优势,并阐述了搭建私人 `Git` 服务器的重要性,包括数据隐私、定制化需求及成本效益。接着,文章分步骤指导读者完成服务器的准备工作,包括操作系统、硬件和网络要求。随后,详细介绍了在不同操作系统上安装 `Git` 的方法,并演示了如何创建 `git` 用户、部署仓库以及设置免密登录。此外,还提供了客户端连接远程仓库的具体步骤,包括 Linux 和 Windows 的操作方式。最后,文章探讨了迁移仓库的方法以及搭建 `Git` 服务器的一些进阶选项。
    45 0
    搭建 Git 私人服务器完整指南
    |
    1月前
    |
    开发工具 git iOS开发
    服务器配置Huggingface并git clone模型和文件
    该博客提供了在服务器上配置Huggingface、安装必要的工具(如git-lfs和huggingface_hub库)、登录Huggingface以及使用git clone命令克隆模型和文件的详细步骤。
    69 1
    |
    3月前
    |
    Shell 网络安全 开发工具
    git实现服务器自动push拉取代码--webhooks
    git实现服务器自动push拉取代码--webhooks
    271 1
    |
    4月前
    |
    JavaScript 持续交付 Go
    如何实现Git Push之后自动部署到服务器?
    如何实现Git Push之后自动部署到服务器?
    116 0
    |
    Java Go API
    Git2.29让Git成功“牵 手”Gerrit
    Git 2.29.0 于 2020年10月发布,其中包含了两个阿里巴巴贡献的新特性。阿里巴巴贡献的新特性让 Git 牵手 Gerrit,让 GitHub 模式的代码平台可以像 Gerrit 一样工作。
    10848 0
    Git2.29让Git成功“牵 手”Gerrit
    |
    27天前
    |
    存储 开发工具 git
    |
    22天前
    |
    开发工具 git
    【GIT 第二篇章】GIT常用命令
    Git常用命令涵盖初始化、状态管理、提交、分支处理、远程操作等关键流程。`git init`启动本地仓库,`git clone`下载远程仓库。通过`git status`和`git diff`检查工作状态与差异。利用`git add`暂存文件,`git commit`保存更改。借助`git branch`、`git checkout`、`git merge`和`git rebase`管理分支。使用`git fetch`、`git pull`和`git push`同步远程仓库。通过`git reset`、`git revert`和`git checkout`实现版本回退。
    44 0
    |
    14天前
    |
    存储 开发工具 git
    Git常用命令汇总
    这是Git命令速查表,涵盖从版本库创建、文件添加与提交、状态查询到分支管理、标签创建及撤销操作的各项常用指令。同时介绍了如何通过GitHub进行代码仓库的创建与同步,帮助用户高效地使用Git进行版本控制和协作开发。
    Git常用命令汇总
    |
    26天前
    |
    存储 安全 开发工具
    掌握这5个Git高级命令,让你的开发效率飞速提升!
    掌握这5个Git高级命令,让你的开发效率飞速提升!
    |
    30天前
    |
    缓存 Shell 开发工具
    Git Bash⭐一、安装软件,与Git Bash基础命令
    Git Bash⭐一、安装软件,与Git Bash基础命令