• ubuntu搭建Gerrit代码审核服务器


    谷歌的 Android 开源项目在 Git 的使用上有两个重要的创新,一个是为多版本库协同而引入的 repo,另外一个重要的创新就是 Gerrit —— 代码审核服务器。Gerrit 为 git 引入的代码审核是强制性的,就是说除非特别的授权设置,向 Git 版本库的推送(Push)必须要经过 Gerrit 服务器,修订必须经过代码审核的一套工作流之后,才可能经批准并纳入正式代码库中。

    Gerrit工作原理和流程

    首先贡献者的代码通过 git 命令(或git review封装)推送到 Gerrit 管理下的 Git 版本库,推送的提交转化为一个一个的代码审核任务,审核任务可以通过 refs/changes/下的引用访问到。代码审核者可以通过 Web 界面查看审核任务、代码变更,通过 Web 界面做出通过代码审核或者打回等决定。测试者也可以通过 refs/changes/引用获取(fetch)修订对其进行测试,如果测试通过就可以将该评审任务设置为校验通过(verified)。最后经过了审核和校验的修订可以通过 Gerrit 界面中提交动作合并到版本库对应的分支中。更详细的流程描述见下图所示: 
    这里写图片描述

    创建gerrit用户

     sudo adduser gerrit
     #给用户添加sudo权限
     chmod u+w /etc/sudoers
     sudo vi /etc/sudoers
     #在root ALL=(ALL) ALL添加下面一行
     gerrit ALL=(ALL) ALL
     su gerrit

    Gerrit安装与配置

    安装Gerrit需要装有最低1.6版本的JDK:

    sudo apt-get install default-jre
    sudo apt-get install git

    https://code.google.com/p/gerrit/ 
    https://gerrit-releases.storage.googleapis.com/gerrit-2.12.war

    安装Gerrit

    java -jar gerrit-2.11.war init -d review_site
    *** Git Repositories
    *** 
    Location of Git repositories   [git]: /home/gerrit/repositories
    
    *** SQL Database
    *** 
    
    Database server type           [h2]: postgresql
    Server hostname                [localhost]: 
    Server port                    [(postgresql default)]: 
    Database name                  [reviewdb]: 
    Database username              [gerrit]: 
    gerrit's password              : 
                  confirm password :
    
    *** User Authentication
    *** 
    Authentication method          [OPENID/?]: http
    Get username from custom HTTP header [y/N]? 
    SSO logout URL 
    
    *** Review Labels
    *** 
    
    Install Verified label         [y/N]? y
    
    *** Email Delivery
    *** 
    
    SMTP server hostname           [localhost]: smtp.163.com
    SMTP server port               [(default)]: 25
    SMTP encryption                [NONE/?]: 
    SMTP username                  [gerrit]: your_name
    gerrit's password              : 
                  confirm password : 
    
    *** SSH Daemon
    *** 
    Listen on address              [*]: 
    Listen on port                 [29418]: 
    *** HTTP Daemon
    *** 
    
    Behind reverse proxy           [y/N]y
    Use SSL (https://)             [y/N]? 
    Listen on address              [*]: 
    Listen on port                 [8080]: 8081
    Canonical URL                  [http://learnLinux:8081/]: http://localhost:8080
    
    *** Plugins 
    *** 
    
    Installing plugins.
    Install plugin download-commands version v2.11 [y/N]? y
    Install plugin reviewnotes version v2.11 [y/N]? y
    Install plugin singleusergroup version v2.11 [y/N]? y
    Install plugin replication version v2.11 [y/N]? y
    Install plugin commit-message-length-validator version v2.11 [y/N]? y
    Initializing plugins.
    
    • 59

    Gerrit支持H2(内置) / MySQL / PostgreSQL数据库,简单使用默认数据库H2,mysql和postgreSQL数据库在认证人数比较多时选用. 
    Gerrit支持OpenID / HTTP / LDAP, 认证方式没有选择OpenId, 而是http, 因为这样会使得gerrit对外部系统有依赖, 目前gerrit支持google和yahoo提供的openid. 
    选择http需要反向代理支持, 这和http认证有关. 
    LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP 
    配置文件review_site/etc/gerrit.config,邮箱密码存在review_site/etc/secure.config文件中.

    vi ./review_site/etc/gerrit.config
    #将canonicalWebUrl修改为代理服务器地址
    [gerrit]
            basePath = /home/gerrit/repositories
            canonicalWebUrl = http://localhost:8090/
    [database]
            type = postgresql
            hostname = localhost
            database = reviewdb
            username = gerrit
    [index]
            type = LUCENE
    [auth]
            type = HTTP
    [sendemail]
            enable = true
            smtpServer = smtp.163.com
            smtpServerPort = 25
            smtpUser = your_name@163.com
            from = gerrit<your_name@163.com>
    [sshd]
            listenAddress = *:29418
    [httpd]
            listenUrl = proxy-http://*:8081/
    [cache]
            directory = cache
    vi etc/secure.config
    [database]
            password = your_password
    [auth]
            registerEmailPrivateKey = your_password
            restTokenPrivateKey = your_password
    [sendemail]
            smtpPass = your_password

    配置nginx代理服务器

    nginx作为代理服务器更加方便,在/etc/nginx/sites-enabled添加一个server模块

    server {
         listen *:8090;  
         server_name localhost;    
         location / {
                    auth_basic              "Welcomme to Gerrit Code Review Site";
                    #确保passwd路径正确
                    auth_basic_user_file    /home/gerrit/review_site/etc/passwd;
                    proxy_pass              http://localhost:8081;
                    proxy_set_header        X-Forwarded-For $remote_addr;
                    proxy_set_header        Host $host;
            }
            location /login/ {
                    proxy_pass              http://localhost:8081;
                    proxy_set_header        X-Forwarded-For $remote_addr;
                    proxy_set_header        Host $host;
            } 
       }  
    touch ./review_site/etc/passwd
    #添加gerrit账号
    htpasswd -b ./review_site/etc/passwd yourname yourpassword
    #重启gerrit,账号才会生效
    ./review_site/bin/gerrit.sh restart

    账户配置

    第一次成功登录的用户会被gerrit作为管理员用户。登录后点击右上角的”匿名懦夫”Anonymous Coward -> Settings来配置账户。 
    这里写图片描述 
    添加SSH公钥 
    要使用gerrit必须要提供用户的公钥。选择页面左侧的SSH Public Keys为当前用户添加公钥。直接将公钥粘贴到Add SSH Public Key框内,然后点击add即可。

    添加其他普通账户

    如果采用http认证,那么添加其他账户时,需要现添加http认证账户。用htpasswd创建的用户时,并没有往gerrit中添加账号,只有当该用户通过web登陆gerrit服务器时,该账号才会被添加进gerrit数据库中。

    为什么不能Sign Out

    也行你会发现用gerrit+HTTP认证,通过web登陆后,点击右上角的Sign Out无法登出。要么是依然保持登陆的状态,要么就是直接出错。 
    不要以为怎么了,其实这是正常现象,以下这段话是从网上看到的:You are using HTTP Basic authentication. There is no way to tell abrowser to quit sending basic authentication credentials, to logout with basicauthentication is to close the Webbrowser.

    SSH访问

    #默认使用.ssh/id_rsa.pub公钥
    ssh -p 29418 -i admin@localhost
    
      ****    Welcome to Gerrit Code Review    ****
    
      Hi admin, you have successfully connected over SSH.
    
      Unfortunately, interactive shells are disabled.
      To clone a hosted Git repository, use:
    
      git clone ssh://admin@learnLinux:29418/REPOSITORY_NAME.git
    
    Connection to localhost closed.

    git仓库

    新建一个gerritRepo仓库,git clone http://127.0.0.1:8080/gerritRepo 
    在推送时

    remote: Unauthorized
    fatal: Authentication failed for 'http://admin@127.0.0.1:8080/gerritRepo/'

    改用ssh方式push

    git remote remove origin
    git remote add origin ssh://admin@127.0.0.1:29418/gerritRepo
    git push origin master

    将commit提交到服务器接受代码审核。

    remote: Branch refs/heads/master:
    remote: You are not allowed to perform this operation.
    remote: To push into this reference you need 'Push' rights.
    remote: User: member
    remote: Please read the documentation and contact an administrator
    remote: if you feel the configuration is incorrect
    remote: Processing changes: refs: 1, done    
    To ssh://member@127.0.0.1:29418/hello1
     ! [remote rejected] master -> master (prohibited by Gerrit)
    error: 无法推送一些引用到 'ssh://member@127.0.0.1:29418/hello1'

    这就是gerrit的精髓所在了。原因是gerrit不允许直接将本地修改同步到远程仓库。客户机必须先push到远程仓库的refs/for/*分支上,等待审核。这也是为什么我们需要使用gerrit的原因。gerrit本身就是个代码审核工具。

    提交changes

    gerrit项目分支权限

    这里写图片描述

    #提交master分支
    git push origin HEAD:refs/for/master
    #提交所有分支
    git push origin refs/heads/*:refs/for/*
    #修改.git/config文件,添加push时的引用
    [remote "origin"]
            url = ssh://chenjianhua@127.0.0.1:29418/hello1
            fetch = +refs/heads/*:refs/remotes/origin/*
            push = HEAD:refs/for/*

    下载hook

    再次推送到服务器

    remote: Processing changes: refs: 1, done    
    remote: ERROR: missing Change-Id in commit message footer
    remote: 
    remote: Hint: To automatically insert Change-Id, install the hook:
    remote:   gitdir=$(git rev-parse --git-dir); scp -p -P 29418 root@ubuntu:hooks/commit-msg ${gitdir}/hooks/
    remote: And then amend the commit:
    remote:   git commit --amend
    remote: 
    To ssh://member@127.0.0.1:29418/hello1
     ! [remote rejected] master -> refs/for/master (missing Change-Id in commit message footer)
    error: 无法推送一些引用到 'ssh://member@127.0.0.1:29418/gerritRepo'

    push时提示需要Change-Id在提交信息中, 需要从gerrit server上下载一个脚本 
    钩子的目的是在提交信息中自动创建 ‘Change-Id:’ 标签

    scp -p -P 29418 admin@127.0.0.1:hooks/commit-msg gerritRepo/.git/hooks/
    #修改上次提交记录,或者再次提交修改
    git commit --amend
    remote: Processing changes: new: 1, refs: 1, done    
    remote: 
    remote: New Changes:
    remote:   http://localhost:8081/2 vi README
    remote: 
    To ssh://member@127.0.0.1:29418/gerritRepo
     * [new branch]      master -> refs/for/master

    审查代码

    配置项目权限

    这里写图片描述 
    这里写图片描述 
    给refs/head/*分支Label Verified权限添加用户分组,这里分配Administrators组. 
    项目评审过程中,需要几个条件,代码才能最终提交到分支

    • Review >=+2
    • Verify >=+1

    评审过程通常有三个人参与,代码提交,代码验证(Verify),代码审查(Review). 通常由自动测试工具jenkins完成代码验证(Verify).

    Needs Verified , Needs Code-Review

    这里写图片描述

    验证和审查通过后,显示Ready to Submit状态,现在就可以合并代码到head/*分支中

    这里写图片描述

    查看合并结果

    这里写图片描述

    jenkins自动验证

    这里写图片描述

    patch补丁集

    开发者的代码需要先提交到refs/for/master分支上,变动的代码称作补丁集,保存在 refs/changes/* 命名空间下.

    git ls-remote
    From ssh://admin@localhost:29418/gerrit_ci
    5f8ed98b0f88787c22e705595e2818db62874f56    HEAD
    eeaef9da4ea27d7c23bfb5f9a2ed1b5357ebbba8    refs/changes/01/1/1
    5f8ed98b0f88787c22e705595e2818db62874f56    refs/changes/02/2/1
    bfdb700f4aab3afc32ec79a29b0e25f8be758f8f    refs/changes/03/3/1
    5f8ed98b0f88787c22e705595e2818db62874f56    refs/heads/master
    887107fcb25c48d1a1eb116ec466fc4f9b298a5c    refs/meta/config
    21be8fce8a38d9437363128d214739c64bdd5710    refs/notes/review
    
    #下载补丁
    git fetch ssh://admin@localhost:29418/gerrit_ci refs/changes/03/3/1

    Draft草案

    Topic主题

    使用postgreSQL数据库

    安装postgreSQL

    sudo apt-get install postgresql
    #次安装后,会默认生成名为postgres的Linux系统用户、数据库和数据库用户(作为数据库管理员),首先修改postgres数据库用户的密码,然后增加Gerrit需要的数据库
    #切换到postgres用户
    sudo su postgres
    #登录postgres数据库
    psql postgres
    #修改postgres用户登录密码
    ALTER USER postgres with PASSWORD 'password'
    #输入密码
    postgres=# 
    #输入第二遍密码
    postgres=# q
    
    #创建gerrit用户
    CREATE USER gerrit WITH PASSWORD 'password';
    #创建数据库
    CREATE DATABASE reviewdb OWNER gerrit;
    #将reviewdb所有权限赋予gerrit
    GRANT ALL PRIVILEGES ON DATABASE reviewdb to gerrit;
    #vi etc/gerrit.config
    [database]
          type = postgresql
          hostname = localhost
          database = reviewdb
          username = gerrit
    #vi etc/secure.config
    [database]
          password = password

    使用mysql数据库

    #连接数据库
    mysql -u root -p
    #查看帮助
    help contents;
    help Administration;
    #创建gerrit用户和reviewdb数据库
    CREATE USER 'git'@'localhost' IDENTIFIED BY 'git';  
    CREATE DATABASE reviewdb;  
    ALTER DATABASE reviewdb charset=latin1;  
    GRANT ALL ON reviewdb.* TO 'git'@'localhost';  
    FLUSH PRIVILEGES;  
    #查看所有数据库
    SHOW DATABASES;
    #查看所有用户
    SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
    *** SQL Database
    *** 
    
    Database server type           [h2]: mysql
    
    Gerrit Code Review is not shipped with MySQL Connector/J 5.1.21
    **  This library is required for your configuration. **
    Download and install it now [Y/n]? y
    Downloading http://repo2.maven.org/maven2/mysql/mysql-connector-java/5.1.21/mysql-connector-java-5.1.21.jar ... OK
    Checksum mysql-connector-java-5.1.21.jar OK
    Server hostname                [localhost]: 
    Server port                    [(mysql default)]: 3306
    Database name                  [reviewdb]: reviewdb
    Database username              [gerrit]: gerrit
    gerrit's password              : 
                  confirm password : 

    也可以将mysql-connector-Java-5.1.21.jar放入lib目录下

  • 相关阅读:
    MODIS系列之NDVI(MOD13Q1)三:.jdk文件配置+MRT安装
    MODIS系列之NDVI(MOD13Q1)二:modis数据相关信息
    MODIS系列之NDVI(MOD13Q1)一:数据下载(二)基于FTP
    MODIS系列之NDVI(MOD13Q1)一:数据下载(一)基于插件
    Python 1基础语法四(数字类型、输入输出汇总和命令行参数)
    Python 1基础语法三(变量和标识符的区别)
    mysql Can't connet MySQL server to '@localhost'
    使用get传参的时候,参数在后头获取不到或者出现别的错误。
    搭建nexus后,进入首页的时候出现warning: Could not connect to Nexus.错误
    在配置dubbo框架的时候出现dubbo:application标签无法识别问题。
  • 原文地址:https://www.cnblogs.com/jiangzhaowei/p/7922600.html
Copyright © 2020-2023  润新知