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目錄下


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM