建立郵箱 user1@vzjc.compython
建立郵箱 jenkins@vzjc.comgit
Gitlab已經配置了root帳戶的郵箱爲admin@vzjc.comshell
$ sudo adduser admin
vim
能夠獲得系統的用戶名admin和用戶本身設置的密碼服務器
$ su - admin $ ssh-keygen -C admin@vzjc.com $ cat .ssh/id_rsa.pub $ exit1234
用root帳號登陸http://gitlab.zjc.com
ssh
點擊頁面右上角的Profile Settings - 點擊左側的SSH Keys小鑰匙圖標 - 點擊Add SSH Key
。在Key對應的輸入框中輸入上段落$cat .ssh/id_rsa.pub
顯示的公鑰全文,點擊Title,應該會自動填充爲admin@vzjc.com
用了圈點Skitch,感受萌萌噠ide
打開http://gitlab.zjc.com, 若是當前頁面爲登陸狀態就退出登陸工具
進入註冊頁面,輸入註冊信息,點擊Sign up按鈕來註冊用戶user1。gitlab
登陸郵箱點擊驗證鏈接,在Gitlab上成功建立user1帳號post
$ sudo adduser user1 $ su - user1 $ ssh-keygen -C user1@vzjc.com $ cat .ssh/id_rsa.pub $ exit12345
點擊 test-project1 工程界面左上的 dev-group
點擊 Members
把 user1 做爲 Reporter 身份添加到組 dev-group (Reporter能夠下載代碼可是沒上傳權限)
地址爲
git@gitlab.zjc.com:dev-group/test-project1.git
$ su - user1 $ cd ~ $ git clone git@gitlab.zjc.com:dev-group/test-project1.git $ exit1234
$ su - user1 $ cd ~/test-project1/ $ git config --global user.name 'user1'$ git config --global user.email 'user1@vzjc.com'$ touch testfile $ git add . $ git commit -m 'user1 add testfile'$ git push $ exit123456789
已經有了第一個帳戶admin並配置信箱爲admin@vzjc.com
$ su - admin $ cat ~/.ssh/id_rsa.pub $ exit123
使用htpasswd工具建立user1和密碼
$ sudo -u gerrit -H htpasswd /home/gerrit/gerrit/etc/htpasswd.conf user1
使用user1登陸gerrit,更新信箱爲user1@vzjc.com
更新SSH公鑰 (user1@zjc.com那個)
Jenkins系統已經建立了管理員帳戶jenkins並安裝了Gerrit Trigger插件
爲Gerrit系統建立用戶jenkins $ sudo -u gerrit -H htpasswd /home/gerrit/gerrit/etc/htpasswd.conf jenkins
爲系統用戶jenkins建立ssh祕鑰,並查看公鑰
也能夠用前面的su - jenkins
登陸後再操做。這樣就不用sudo -u jenkins -H
前綴了
$ sudo -u jenkins -H ssh-keygen -C jenkins@vzjc.com $ sudo -u jenkins -H cat /home/jenkins/.ssh/id_rsa.pub
1
2
1
2
3
此處有個問題,若是在安裝Jenkins系統前沒有建立jenkins用戶(安裝時候系統會自動建立)而且使用的Dovecot來接收郵件而且用默認配置。那麼要先查一下是否系統用戶jenkins的uid是否大於500,若是不是,須要修改配置文件
主頁面->Manage Jenkins->Configure System
Jenkins Location ->System Admin e-mail address : jenkins@vzjc.com
E-mail Notification
勾選Test configuration by sending test e-mail,而後在出現的框中填admin@vzjc.com
,測試若是出錯(好吧,若是用vzjc.com的郵件服務器是確定會出錯)Unrecognized SSL message, plaintext connection
,則參考這個設置
SMTP server : mail.vzjc.com
Default user e-mail suffix : @vzjc.com
勾選Use SMTP Authentication
User Name : jenkins@vzjc.com
Password : 輸入您設置的密碼
勾選Use SSL
SMTP Port : 587
Reply-To Address : jenkins@vzjc.com
Charset : UTF-8
主頁面->Manage Jenkins->Gerrit Trigger->Add New Server
Add New Server : Check4Gerrit
勾選 Gerrit Server With Default Configurations
Hostname : review.zjc.com
Frontend URL : http://review.zjc.com
Username : jenkins
E-mail : jenkins@vzjc.com
若是點擊Test Connection後出現如圖所示的錯誤User jenkins has no capability to connect to Gerrit event stream.
,這是由於因Jenkins 沒有權限監聽 Gerrit 的 ‘Stream Events’,可如此配置
點擊Test Connection,出現的字符串爲 success 則表明配置成功
點擊下方的保存按鈕來保存設置
若是安裝Gerrit時沒有或者沒有選擇添加Verified標籤功能[‘lable Verified’],須要本身添加
$ su - admin $ git init cfg; cd cfg $ git config --global user.name 'admin'$ git config --global user.email 'admin@vzjc.com'$ git remote add origin ssh://admin@review.zjc.com:29418/All-Projects $ git pull origin refs/meta/config $ vim project.config1234567
在文件末添加
[label "Verified"] function = MaxWithBlock value = -1 Fails value = 0 No score value = +1 Verified12345
$ git commit -a -m 'Updated permissions'$ git push origin HEAD:refs/meta/config12
如今提交的 Review 請求只有 Code Rivew 審覈,咱們要求的是須要 Jenkins 的 Verified 和 Code Review 雙重保障,在 Projects 的 Access 欄裏,針對 Reference: refs/heads/ 項添加 Verified 功能
Projects -> List -> All-Projects
Projects -> Access -> Edit -> 找到 Reference: refs/heads/* 項 -> Add Permission -> Label Verified-> Group Name 裏輸入 Non-Interactive Users -> 回車 或者 點擊Add 按鈕 -> 在最下面點擊 Save Changes 保存更改
test-project1
工程前面咱們在Gitlab上搭建了一個 test-project1 的工程,普通用戶是沒有辦法去 push 的,只能使用 git review 命令提交. 而 git review 命令須要 .gitreview 文件存在於項目目錄裏。
.gitreview
文件$ su - admin $ git clone git@gitlab.zjc.com:dev-group/test-project1.git $ cd test-project1 $ vim .gitreview1234
文件內容爲:
[gerrit]host=review.zjc.comport=29418project=test-project1.git1234
.gitreview
到版本庫$ git add .gitreview $ git config --global user.name 'admin'$ git config --global user.email 'admin@vzjc.com'$ git commit .gitreview -m 'add .gitreview file by admin.'$ git push origin master $ exit123456
.testr.conf
文件$ sudo apt-get install python-pip $ sudo pip install testrepository12
test-project1
這個項目中添加 .testr.conf
文件$ su - admin $ cd test-project1 $ vim .testr.conf123
文件內容爲:
[DEFAULT] test_command=OS_STDOUT_CAPTURE=1 OS_STDERR_CAPTURE=1 OS_TEST_TIMEOUT=60 ${PYTHON:-python} -m subunit.run discover -t ./ ./ $LISTOPT $IDOPTIONtest_id_option=--load-list $IDFILEtest_list_option=—list1234
$ git add .testr.conf $ git commit .testr.conf -m 'add .testr.conf file by admin'$ git push origin master $ exit1234
要知道 review 是在 gerrit 上,而 gerrit 上如今是沒有項目的,想讓 gitlab 上的項目能在 gerrit 上 review 的話,必須在 gerrit 上建立相同的項目,並有相同的倉庫文件.
$ su - admin $ ssh -p 29418 admin@vzjc.com gerrit create-project test-project1 $ exit123
由於gerrit用戶無訪問gitlab的權限。因此要先看是否gerrit用戶下已經存在了id_rsa密鑰,若是沒有則建立,而後把公鑰加入到gitlab的admin帳戶上(這裏加到 admin 帳戶上,是由於後面Gerrit系統還會有個複製 git 庫到 Gitlab的功能,須要管理員權限)
$ sudo -u gerrit -H ls /home/gerrit/.ssh $ sudo -u gerrit -H ssh-keygen -C gerrit@vzjc.com $ sudo -u gerrit -H cat /home/gerrit/.ssh/id_rsa.pub123
用 root 登陸Gitlab,把公鑰添加到 admin 的SSH Key上 - 額,這是由於,Gerrit使用gerrit用戶身份運行的,而Gerrit得git庫複製功能須要權限複製到Gitlab上
gerrit
身份克隆 test-project1$ cd /home/gerrit/gerrit/git $ sudo -u gerrit -H rm -fr test-project1.git $ sudo -u gerrit -H git clone --bare git@gitlab.zjc.com:dev-group/test-project1.git123
當用戶 git review 後,代碼經過 jenkins 測試、人工 review 後,代碼只是 merge 到了 Gerrit 的 test-project1 項目中,並無 merge 到 Gitlab 的 test-project1 項目中,因此須要當 Gerrit test-project1 項目倉庫有變化時自動同步到 Gitlab 的 test-project1 項目倉庫中。Gerrit 自帶一個 Replication 功能,同時咱們在安裝 Gerrit 時候默認安裝了這個 Plugin。如今只須要添加一個 replication.config 給 Gerrit
$ sudo -u gerrit -H vim /home/gerrit/gerrit/etc/replication.config1
文件內容爲:
[remote "test-project1"] # Gerrit 上要同步項目的名字 projects = test-project1 url = git@gitlab.zjc.com:dev-group/test-project1.git push = +refs/heads/*:refs/heads/* push = +refs/tags/*:refs/tags/* push = +refs/changes/*:refs/changes/* threads = 312345678
$ sudo -u gerrit -H vim /home/gerrit/.ssh/config1
文件內容爲:
Host gitlab.zjc.com: IdentityFile ~/.ssh/id_rsa PreferredAuthentications publickey123
$ sudo -u gerrit -H sh -c "ssh-keyscan -t rsa gitlab.zjc.com >> /home/gerrit/.ssh/known_hosts"$ sudo -u gerrit -H sh -c "ssh-keygen -H -f /home/gerrit/.ssh/known_hosts"12
$ sudo /etc/init.d/gerrit restart1
在 gerrit 文檔中有一個 ${name} 變量用來複制 Gerrit 的全部項目,這裏並不須要。若是有多個項目須要複製,則在 replication.config 中添加多個 [remote ….] 字段便可。務必按照上面步驟配置複製功能。
用 jenkins 登陸 http://jenkins.zjc.com
Manage Jenkins -> Manage Plugins -> Avalabile -> 安裝 Git Plugin
用 jenkins 登陸 Jenkins http://jenkins.zjc.com
New Item頁面設置
Item name : test-project1
Freestyle project
Git頁面設置
To get the Git Plugin to download your change; set Refspec to $GERRIT_REFSPEC and the Choosing strategy to Gerrit Trigger. You may also need to set 'Branches to build' to $GERRIT_BRANCH. If this does not work for you set Refspec to refs/changes/*:refs/changes/* and 'Branches to build' to $GERRIT_REFSPEC Note: Be aware that $GERRIT_BRANCH and $GERRIT_REFSPEC are not set in the Ref Updated case. If you want to trigger a build, you can set Refspec and 'Branches to build' to $GERRIT_REFNAME.
Source Code Management : Git
Respostories : http://review.zjc.com:8082/p/test-project1.git
Branches to build : origin/$GERRIT_BRANCH
Build Triggers : Gerrit event
按下文來配置,上面的無效 - 來自 這裏
這裏 Choose a Server 選的是 Any Server 由於只有一個服務器 Check4Gerrit, 因此要確保這個服務器是Enable 狀態。能夠去服務器那裏去檢查一下看看
Gerrit Event頁面設置
Pattern : test-project1
Branches : master
Trigger on : 點擊 Add 分別添加 Patchset Created 和 Draft Published
Dynamic Trigger Configuration
Build頁面設置
Add build step -> Execute shell cd $WORKSPACE
[ ! -e .testrepository ] && testr init
testr run
用 user1 登陸
刪除目錄 test-project1
克隆 test-project1 工程
進入 test-project1 目錄
添加文件、提交
git review
增長 review 到Gerrit
$ su - user1 $ rm -rf test-project1/ $ git clone git@gitlab.zjc.com:dev-group/test-project1.git $ cd test-project1/ $ touch testfile $ git add . $ git commit -m 'add by user1'$ git review $ exit123456789
個人環境還有一個問題 第一次 git review
沒有問題,可是若是用 git commit --amend
修改了提交信息,再次git reivew
機會出現error: unpack failed: error Missing tree
的錯誤,折中解決方案看這裏
查看 test-project1 工程,能夠看到新增長文件操做已經同步過來了