Code Review
Base on Gerrit-3.1.0, Gitlab, Jenkins
Gerrit
Install
- download 安裝包 gerrit*.war;
- Strongly recommended 添加系統(tǒng) 用戶 gerrit;
# 添加用戶
user add gerrit
# 切換用戶
su gerrit
- 安裝
java -jar gerrit*.war init -d ~/gerrit_site
Attention
- 為方便進行多用戶管理 鑒權(quán)方式改為 http
[auth]
type = HTTP
- 應(yīng)用反向代理
Behind reverse proxy [y/N]? y
- canonicalWebUrl 對外訪問地址
- listenUrl = proxy-http://*:8081/ http 監(jiān)聽地址
配置文件
[gerrit]
basePath = git
canonicalWebUrl = http://190.168.1.27:8081/
serverId = ba92321d-bb2c-4c13-9d0c-7a4c2e43fce8
[container]
javaOptions = "-Dflogger.backend_factory=com.google.common.flogger.backend.log4j.Log4jBackendFactory#getInstance"
javaOptions = "-Dflogger.logging_context=com.google.gerrit.server.logging.LoggingContext#getInstance"
user = gerrit
javaHome = /usr/java/jdk1.8.0_201-amd64/jre
[index]
type = lucene
[auth]
type = HTTP
[receive]
enableSignedPush = true
[sendemail]
smtpServer = smtp.126.com
smtpServerPort = 465
smtpEncryption = SSL
smtpUser = xxx@126.com
from = xxx@126.com
[sshd]
listenAddress = *:29418
[httpd]
listenUrl = proxy-http://*:8081/
[cache]
directory = cache
[plugins]
# allow install plugin
allowRemoteAdmin = true
安裝官方參考 Install
設(shè)置
Nginx 反向代理配置
server {
listen 81;
server_name www.example.com;
auth_basic "Welcomme to Gerrit Code Review Site!";
auth_basic_user_file /home/gerrit/gerrit_site/etc/gerrit.password;
location / {
proxy_pass http://127.0.0.1:8081/;
#proxy_set_header X-Forwarded-For $remote_addr;
#proxy_set_header Host $host;
}
}
- proxy_pass 對應(yīng) gerrit 配置中 listenUrl屬性, like this, "proxy-http://*:8081/"
添加用戶
htpasswd -cb gerrit_site/etc/gerrit.password gerrit gerrit
Attention:
- 默認 添加的第一個為 管理員;
- 只有在登錄之后久橙,用戶信息才會被寫入數(shù)據(jù)庫。
注冊郵箱
后續(xù) Push, Review, Verify, Submit, 接收通知 等都需要郵箱的支持。
- 登錄 -> 進入 Settings -> Email Addresses;
- 輸入郵箱地址 xxx@xxx.com, Click “Send VERIFICATION” button 發(fā)送驗證郵件;
- 接收郵件 訪問 驗證鏈接;
- done.
配置ssh 免密登錄
避免 Pull, Push 時頻繁輸入密碼。
配置 gerrit 服務(wù)端到 gitlab 的 ssh 免密登錄
- 為 gitlab 添加同名管理員賬戶 gerrit;
- 為 gerrit 服務(wù)端 系統(tǒng)用戶 gerrit 生成 rsa 密鑰;
# 為 gerrit 用戶生成 ssh 密鑰
ssh-keygen -t rsa -C gerrit
- 將公鑰添加到 gitlab 系統(tǒng) gerrit 管理員賬戶 下;
- done.
登錄 gitlab -> 進入 Settings -> SSH keys;
配置 開發(fā)端到 gerrit 的 ssh 免密登錄
可選氨鹏。ssh 方式 用俏扩。
- 確保開發(fā)端具有與 gerrit 注冊賬戶同名的用戶, like dev1;
- 在開發(fā)端生成 該用戶 dev1 的 rsa 密鑰;
# 為 dev1 用戶生成 ssh 密鑰
ssh-keygen -t rsa -C dev1
- 將公鑰添加到 gerrit 系統(tǒng) dev1 賬戶 下;
登錄 gerrit -> 進入 Settings -> SSH keys;
從 gerrit 系統(tǒng)獲取 http credentials
可選卿嘲。http 方式 Pull, Push 時用
- 登錄 gerrit -> 進入 Settings -> HTTP Credentials;
- 點擊 "GENERATE NEW PASSWORD" Button 獲取 password;
-
done.
添加項目
BROWSE -> Repositories -> CREATE NEW;
若使用外部倉庫大刊,以 Gitlab 為例。
- 在 gerrit UI 創(chuàng)建倉庫 repo-demo(倉庫名稱須與 gitlab 中的項目名稱相同)
- 去 gerrit 倉庫目錄下刪除 repo-demo, 從 Gitlab 克隆同名項目替換之 加上 --bare 參數(shù);
git clone --bare ssh://git@xxx.xxx.xxx/gerrit/repo-demo.git
代碼審查流程
- Coder 提交代碼敌蜂,實際代碼到了 gerrit 對應(yīng)倉庫的暫存區(qū)(staging area), 供 Review 和 Verify;
- Review, Verify -> Passed -> Submit, 代碼到了 gerrit 對應(yīng)倉庫的 master 分支;
- done.
參考文檔 代碼審查流程
開發(fā)端 Pull 代碼
倉庫地址在倉庫詳情頁箩兽。
Attention: Pull 時選擇 "Clone with commit-msg hook" 方式以便 Commit 時通過 commit-msg (.git/hooks/commit-msg) 生成 Change-Id 。
- Http 方式;
- ssh 方式;
gerrit 內(nèi)建分支
推送之前有必要講一下 推送涉及到的分支
- refs/for/*
- refs/*
refs/for/*
這個Reference 是用于 Review 的章喉。
The refs/for/ prefix is used to map the Gerrit concept of "Pushing for Review" to the git protocol. For the git client it looks like every push goes to the same branch, e.g. refs/for/master but in fact for each commit that is pushed to this ref Gerrit creates a new branch under the refs/changes/ namespace. In addition Gerrit creates an open change.
refs/*
配置 Verified 權(quán)限的時候 要針對這個 Reference 進行配置汗贫。
開發(fā)端 Push 代碼
Attention: 確保 commit footer 有 Change-Id 信息。
Change-Id 可從通過 commit-msg (.git/hooks/commit-msg) 生成秸脱。
Strongly recommended:
只需確保項目目錄(.git/hooks/)下有 commit-msg 腳本落包,再配合對應(yīng) 的 gerrit 插件即可在 Commit 時完成 Change-Id 的自動生成。
Idea, Eclipse 都有相應(yīng)的 gerrit 插件摊唇。
Change-Id
用于標(biāo)識 commit咐蝇。
Parent: 65c50d48 (Update Dockerfile base image to 0.1.2)
Author: xxxx <xxxxx@qq.com>
AuthorDate: 2019-12-12 17:41:51 +0800
Commit: xxxx <xxxxx@qq.com>
CommitDate: 2019-12-12 17:41:51 +0800
add readme.md;
Change-Id: Iba58fbc6a1a440a9d3e2e48bbd72f898ba3cabc0
開啟權(quán)限
進入對應(yīng)的項目 -> 選擇菜單 "Access" 選項
-
開啟 Verify 權(quán)限
為 "refs/*" Reference 添加 Label Verified 相關(guān)權(quán)限,并賦權(quán)給相應(yīng)的 Group, 否則無權(quán)限進行 Verify巷查。
繞過審核直接提交
開啟相應(yīng)人員的 Submit 權(quán)限;
-
提交時在相應(yīng)分支后加上 %submit;
Idea gerrit 插件中 Push 時勾選 Submit Change 選項即可有序。
參考文檔 Gerrit-訪問控制
Review& Verified
YOUR -> Changes -> 選擇對應(yīng)的 Change -> REPLY;
添加接收者抹腿,抄送者辽聊,評論钮莲,并對 Code-Review, Verified(若有權(quán)限) 打分贮尉。
若 Code-Review Verified 對應(yīng)的分數(shù)均符合 Sumbmit 要求真慢,
則 當(dāng)前Change 狀態(tài)變?yōu)?Ready to submit, 放出 Sumbim 按鈕。點擊 Submit 按鈕 完成 變更往 gerrit 倉庫 master 分支的提交蜗顽。
Replication
與外部 git 倉庫同步
config path: gerrit_home/etc/replication.config
target 有兩種連接方式:
- ssh
- http
[remote "gitlab_domain"]
# http 方式
# url = https://username:password@gitlab_domain/xxxx/${name}.git
# ssh 方式
url = ssh://git@gitlab_domain/xxxx/${name}.git
push = +refs/heads/*:refs/heads/*
push = +refs/tags/*:refs/tags/*
push = +refs/changes/*:refs/changes/*
timtout = 30
threads = 3
關(guān)閉 Gitlab 的提交權(quán)限
后續(xù)開發(fā)端提交代碼走 gerrit, 不再走 gitlab坞嘀。
Jenkins Gerrit trigger TODO
- 為 Jenkins 添加 插件 Gerrit trigger
- 添加 Gerrit Server
- 為 Job 配置 Gerrit trigger
涉及的問題
- send email 失敗
安裝完之后砚哆,記得加入 from 屬性!
[sendemail]
smtpServer = smtp.126.com
smtpServerPort = 465
smtpEncryption = SSL
smtpUser = xxx@126.com
from = xxx@126.com
- 開發(fā)端 Pull Code 無權(quán)限
因 canonicalWebUrl 配置錯誤導(dǎo)致的 gerrit 倉庫地址生成錯誤,
配置文件檢查
- 開發(fā)端 Push Code 無權(quán)限
push 時的密碼是 http credentials, 不是賬戶密碼!
- 開發(fā)端 Push 被拒(缺少Change-Id)
去 gerrit 對應(yīng)的 repository 里(.git/hooks/)拿 commit-msg,
通過 commit-msg 生成 Change-Id, 可借助插件在 commit 時自動生成缩膝。
- You don't have permission to verify
為對應(yīng)項目的 "refs/*" Reference 添加 Label Verified 相關(guān)權(quán)限混狠,并賦權(quán)給相應(yīng)的 Group
- replication Error
配置文件有誤
- 格式不對;
- 目標(biāo)倉庫地址錯誤;
- 目標(biāo)倉庫地址鑒權(quán)信息(ssh/account)錯誤;
- ...