如何用Gerrit管理你的Android代碼?
作者|戶銳,單位:中國移動智慧家庭運營中心
Labs 導(dǎo)讀
Android源碼是基于Linux的開源操作系統(tǒng),目前Android ROM開發(fā)的代碼管理工具基本上是采用Git。由于Android代碼非常復(fù)雜,Google將其劃分為多個git repo,這樣不僅可以避免一個repo的代碼太多,還可以根據(jù)repo的功能將其分配給不同團隊進行管控。為了對Android代碼質(zhì)量進行管控,Google采用了Gerrit進行coderview,并利用jenkins做代碼靜態(tài)檢測和自動化驗證,當然還有集成CI工具。目前中國移動智慧家庭運營中心AOS-RM項目已經(jīng)部署自己的Gerrit服務(wù)器,Gerrit對AOS-RM項目代碼質(zhì)量管理起到了非常重要的作用。
作為Android ROM相關(guān)的開發(fā)者,有必要了解什么是Gerrit,Gerrit的工作流程是怎么樣的,Gerrit服務(wù)器怎么搭建,以及如何將Android codebase導(dǎo)入自己的Gerrit服務(wù)器。接下來本文將為大家詳細解答以上問題。
Part 01 Gerrit簡介
Gerrit是Google為Android系統(tǒng)研發(fā)量身定制的一套免費開源的代碼審核系統(tǒng),使用網(wǎng)頁界面。利用網(wǎng)頁瀏覽器,同一個團隊的軟件開發(fā)者可以相互審閱彼此修改后的代碼,決定是否提交,回退或是繼續(xù)修改。它使用版本控制系統(tǒng)Git作為底層。目前Gerrit被廣泛使用,與Android開發(fā)相關(guān),尤其是做Android ROM開發(fā)的公司基本都使用Gerrit進行code review。根據(jù)統(tǒng)計使用Gerrit的top3領(lǐng)域是IOT,Software Development以及Big Data。
數(shù)據(jù)來源:https://www.slintel.com/tech/source-code-management/gerrit-market-share
Part 02 Gerrit的工作流程
- 開發(fā)人員首先從git repo服務(wù)器上下載代碼,首次下載Android codebase代碼用git clone或者repo sync;
- 代碼修改完畢并通過自我驗證后以git push origin HEAD:refs/for/branch_name的方式提交到Gerrit上進行code review,其中branch_name需要根據(jù)實際情況修改為對應(yīng)的分支名;
- Jenkins自動觸發(fā)靜態(tài)代碼檢查和編譯,如果通過則verify+1,否則verify-1;
- Jenkins verify -1 后,根據(jù)提示修改代碼,進入步驟2;
- Jenkins verify+1后,要求其他開發(fā)人員和MDE(模塊owner,具有+2的權(quán)限)進行code review;
- 其他開發(fā)人員和MDE進行code review如果發(fā)現(xiàn)問題,則提出修改意見并-1,如果沒有問題則+1和+2;
- 如果codereview有-1,則根據(jù)進行澄清或者根據(jù)意見修改,然后進入步驟2;
- 如果沒有codereview -1,MDE點submit按鈕,代碼入庫,流程完畢。
Part 03 Gerrit服務(wù)器搭建
3.1 安裝git
sudo apt-get install git
3.2 安裝jdk11
sudo apt install openjdk-11-jdk
3.3 安裝gitweb
sudo apt-get install gitweb
3.4 Gerrit安裝包下載
wget https://gerrit-releases.storage.googleapis.com/gerrit-3.5.1.wa
3.5 安裝Gerrit(gerrit-3.5.0.1.war存放在~/gerrit目錄下面)
cd ~/gerrit
mkdir review_site
java -jar gerrit-3.5.0.1.war init -d ./review_site/ (可以都選擇默認,然后修改config文件)
sudo vim /home/gerrit/review_site/etc/gerrit.config
發(fā)送郵件配置的密碼保存在secure.config,發(fā)送郵件的密碼是授權(quán)碼
3.6 安裝Apache
sudo apt-get install apache2
3.7 配置Apache
sudo vim /etc/apache2/httpd.conf 內(nèi)容如下圖
sudo vim /etc/apache2/ports.conf 加入 Listen 8081(如果80端口沒有被占用,不用修改)
sudo vi /etc/apache2/apache2.conf 加入 Include httpd.conf
3.8 使能proxy module
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests
3.9 添加賬戶
htpasswd -b /xxx/passwords account xxxx
如果passwords文件不存在,第一次需要加-c,
htpasswd -c -b /xxxx/passwords account xxxx
3.10 啟動apache
sudo systemctl start apache2
sudo systemctl restart apache2
?3.11 啟動gerrit
/xxxx/bin/gerrit.sh start
/xxxx/bin/gerrit.sh restart
3.12 訪問gerrit
http://IP地址:端口號/
Part 04 Android Codebase導(dǎo)入Gerrit
4.1 創(chuàng)建AOSP倉庫
利用瀏覽器在Gerrit服務(wù)器上創(chuàng)建AOSP倉庫,將Only server as parent for other repositories設(shè)置為True
4.2 下載Android Codebase(-u的參數(shù)根據(jù)實際情況做修改)
repo init -u https://android.googlesource.com/platform/manifest --mirror
repo sync
4.3 創(chuàng)建repo(將IP修改為自己的服務(wù)器IP地址)
4.3.1 循環(huán)創(chuàng)建repo
repo forall -c 'echo $REPO_PROJECT; ssh -p 29418 account@IP gerrit create-project --owner AOSP_account $REPO_PROJECT;
4.3.2 設(shè)置repo的parent
repo forall -c 'echo $REPO_PROJECT; ssh -p 29418 account@IP gerrit set-project-parent --parent AOSP $REPO_PROJECT;
4.3.3 將Codebase的代碼push到Gerrit服務(wù)器?
repo forall -c 'echo $REPO_PROJECT; git push
ssh://account@IP:29418/$REPO_PROJECT +refs/heads/* +refs/tags/*
Part 05 生成和定制manifest
5.1clone一份platform/manifest倉庫的代碼
git clone "ssh://account@IP:29418/platform/manifest" && scp -p -P 29418 account@IP:hooks/commit-msg "manifest/.git/hooks/
5.2 將倉庫中的manifest復(fù)制一份,并重命名
一定要設(shè)置review字段,否則repo sync后的代碼不會產(chǎn)生change id
如果要增加自己特有的倉庫,則增加一個project節(jié)點,并配置name,path和revision即可,最后將新的xml文件提交到gerrit服務(wù)器,完成review后merge到遠程倉庫。
Part 06 下載和上傳代碼
repo init -u ssh://account@IP:29418/platform/manifest.git -b branch_name --repo-url=ssh://account@IP:29418/repo.git -m mymanifest.xml
repo sync -j4
Part 07 倉庫權(quán)限配置
詳細說明可以參考:
https://gerrit-documentation.storage.googleapis.com/Documentation/3.5.0/access-control.htm
配置中,需要根據(jù)實際情況進行修改,不同組對不同倉庫具有不同的訪問權(quán)限,切記不要開放對refs/*的push權(quán)限,否則開發(fā)人員可以跳過Gerrit,直接push到git服務(wù)器。?