Gerrit

Aug 24, 2017


原因

我在 Git 上创建的 Tag 无法推送到公司的 Gerrit 上,提示权限不足
但是运维人员认为权限已经给了
为了解决问题,遂决定自己搭建 Gerrit ,搞明白是怎么回事

安装

  1. 下载
    下载 Gerrit 包,我下载的是 gerrit-2.14.2.war
    将 war 包放入 gerrit 文件夹
  2. 安装

     java -jar gerrit-2.14.2.war init -d install
    

    安装过程中大部分默认即可,但要注意如下几点:

    • 验证方法选择 HTTP

        Authentication method          [OPENID/?]: http
      
    • 端口使用的 8086

        Listen on port                 [8080]: 8086		
      
    • 安装插件
      插件默认都不安装,但我选择了都安装
      第二个插件可以在项目中提示 git clone 命令
      其他插件的功能没有深究

        Installing plugins.
        Install plugin commit-message-length-validator version v2.14.2 [y/N]? Y
        Installed commit-message-length-validator v2.14.2
        Install plugin download-commands version v2.14.2 [y/N]? Y
        Installed download-commands v2.14.2
        Install plugin hooks version v2.14.2 [y/N]? Y
        Installed hooks v2.14.2
        Install plugin replication version v2.14.2 [y/N]? Y
        Installed replication v2.14.2
        Install plugin reviewnotes version v2.14.2 [y/N]? Y
        Installed reviewnotes v2.14.2
        Install plugin singleusergroup version v2.14.2 [y/N]? Y
        Installed singleusergroup v2.14.2
        Initializing plugins.
      

      安装完毕会自动启动服务并跳转http://localhost:8086/并且会报错,先不管他

  3. 配置
    修改 install/etc/gerrit.config 文件对 Gerrit 进行配置
    下面是我的配置

     [gerrit]
         basePath = git
         serverId = d9d7e2b0-e41a-4c9d-b1f1-40fee375363a
         canonicalWebUrl = http://localhost:8086/
     [database]
         type = h2
         database = /Users/lin/Documents/gerrit/install/db/ReviewDB
     [index]
         type = LUCENE
     [auth]
         type = HTTP
     [receive]
         enableSignedPush = false
     [sendemail]
     	enable = true
     	smtpServer = smtp.163.com
     	smtpServerPort = 25
     	smtpUser = username@163.com
     	smtpPass = password
     	sslVerify = false
     	from = username@163.com
     [container]
         user = lin
         javaHome = /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre
     [sshd]
         listenAddress = *:29418
     [httpd]
         listenUrl = http://*:8086/
     [cache]
         directory = cache
    

    然后就可以使用了

     ./install/bin/gerrit.sh stop
     ./install/bin/gerrit.sh start
     ./install/bin/gerrit.sh restart
    

    注意如下几个点:

    • 端口我使用的 8086,默认 8080
    • 邮箱一定要配置,否则发送确认邮件失败
    • 邮箱需要配置 from 属性,否则会被当做垃圾邮件
  4. 生成用户
    install/etc/ 文件夹下执行如下命令

     touch passwords
     htpasswd passwords admin
     New password:
     Re-type new password:
     Adding password for user admin
    

    按照提示输入两次密码,生成用户 admin 和密码备用

  5. 配置 Apache
    使用 Apache 做反向代理,Mac 下已经安装了 Apache,只需要配置一下即可
    在文件/private/etc/apache2/httpd.conf 最后部分,找到一行

     Include /private/etc/apache2/other/*.conf
    

    在这一行之前增加如下内容

     <VirtualHost *:80>
         ServerName localhost
         ProxyRequests Off
         ProxyVia Off
         ProxyPreserveHost On
         <Proxy *>
             Order deny,allow
             Allow from all
         </Proxy>
         <Location /login/>
             AuthType Basic
             AuthName "Gerrit Code Review"
             AuthUserFile /Users/lin/Documents/gerrit/install/etc/passwords
             Require valid-user
         </Location>
         AllowEncodedSlashes On
         ProxyPass / http://localhost:8086/
     </VirtualHost>
    

    其中 AuthUserFile 属性的路径即上一步生成的 passwords 文件的路径
    此时启动或者执行apachectl --help会报错

     AH00526: Syntax error on line 539 of /private/etc/apache2/httpd.conf:
     Invalid command 'ProxyRequests', perhaps misspelled or defined by a module not included in the server configuration
    

    这时候需要将 httpd.conf 文件中的两行解注释

     LoadModule proxy_module libexec/apache2/mod_proxy.so
     LoadModule proxy_http_module libexec/apache2/mod_proxy_http.so
    

    至此,整个 Apache 的配置才结束,可以使用了

     sudo apachectl start
     sudo apachectl restart
     sudo apachectl stop
    

使用

  1. 访问http://localhost/,按照提示输入上一章节第四步创建的用户名、密码
  2. 进入系统后会要求发送邮件进行验证
  3. 验证完毕之后此用户即为管理员账号
  4. 剩下的设置公钥、创建项目等就略过了
  5. 测试提交、推送、Tag等功能都是可以的

问题

比较我在公司的 Gerrit 权限和自己搭建的权限
发现公司授予的 Tag 相关的权限在 refs/heads/*
自己搭建的 Tag 相关的权限在 refs/tags/*
让运维人员修改授权后,问题解决