參考之前的文章:
目錄
Gerrit 和 Jenkins 整合
讓 Gerrit 支持 Jenkins,Gerrit 在 2.7 版本后去掉了 ‘lable Verified’,需要自己添加
# cd /tmp
# git init cfg; cd cfg
# git config user.name 'admin'
# git config user.email 'admin@thstack.com'
# git remote add origin ssh://admin@review.thstack.com:29418/All-Projects
# git pull origin refs/meta/config
# vim project.config # 在文件末尾添加下面 5 行
[label "Verified"]
function = MaxWithBlock
value = -1 Fails
value = 0 No score
value = +1 Verified
# git commit -a -m 'Updated permissions'
# git push origin HEAD:refs/meta/config
查看 Jenkins 的 log 發現會一直出現下面的信息,是因為 Jenkins 沒有權限監聽 Gerrit 的 ‘Stream Events’
# tail -f /var/log/jenkins/jenkins.log
Dec 26, 2013 1:16:49 AM com.sonyericsson.hudson.plugins.gerrit.gerritevents.GerritHandler run
INFO: Ready to receive data from Gerrit
Dec 26, 2013 1:16:49 AM com.sonyericsson.hudson.plugins.gerrit.gerritevents.GerritHandler run
INFO: Ready to receive data from Gerrit
在 Gerrit 全局設置中能看到 Gerrit 的 ‘Stream Events’ 動作權限默認對 ‘Non-Interactive Users’ 組開放
用 admin@thstack.com 用戶登錄 Gerrit 系統,添加 Jenkins@thstack.com 用戶到 ‘Non-Interactive Users’ 組
點擊 -> People -> List Groups -> Non-Interactive Users
現在提交的 Review 請求只有 Code Rivew 審核,我們要求的是需要 Jenkins 的 Verified 和 Code Review 雙重保障,在 Projects 的 Access 欄里,針對 Reference: refs/heads/ 項添加 Verified 功能*
點擊 Projects -> Access -> Edit -> 找到 Reference: refs/heads/* 項 -> Add Permission -> Label Verified -> Group Name 里輸入 Non-Interactive Users -> Add -> 在最下面點擊保存更改
GitLab 上為 openstack 項目的一些准備
.gitreview
之前在 CI 系統搭建:二. GitLab 的安裝配置 文章中在 GitLab 上創建了一個叫 openstack 項目,並設置了權限,普通用戶是沒有辦法去 push 的,只能使用 git review 命令提交. 而 git review 命令需要 .gitreview 文件存在於項目目錄里,用 admin@thstack.com 用戶添加 .gitreview 文件
git clone git@gitlab.thstack.com:devgroup/openstack.git
cd openstack
git config user.name 'admin'
git config user.email 'admin@thstack.com'
編輯 .gitreview 文件
vim .gitreview
[gerrit]
host=review.thstack.com
port=29418
project=openstack.git
添加到版本庫
git add .gitreview
git commit .gitreview -m 'Admin add .gitreview file.'
git push origin master
.testr.conf
Python 代碼我使用了 testr,需要先安裝 testr 命令
apt-get install python-pip
pip install testrepository
在 openstack 這個項目中添加 .testr.conf 文件
cd openstack
vim .testr.conf
[DEFAULT]
test_command=OS_STDOUT_CAPTURE=1 OS_STDERR_CAPTURE=1 OS_TEST_TIMEOUT=60 ${PYTHON:-python} -m subunit.run discover -t ./ ./ $LISTOPT $IDOPTION
test_id_option=--load-list $IDFILE
test_list_option=—list
提交到版本庫中
git add .testr.conf
git commit .testr.conf -m 'Admin add .testr.conf file'
git push origin master
Gerrit 上為 openstack 項目的一些准備
在 Gerrit 上創建 openstack 項目
要知道 review 是在 gerrit 上,而 gerrit 上現在是沒有項目的,想讓 gitlab 上的項目能在 gerrit 上 review 的話,必須在 gerrit 上創建相同的項目,並有相同的倉庫文件.
用 admin 用戶在 Gerrit 上創建 openstack 項目
ssh -p 29418 admin@review.thstack.com gerrit create-project openstack
clone –bare Gitlab 上的倉庫到 Gerrit
在 Gerrit 上 clone Gitlab 的 openstack 項目
cd /etc/gerrit/git
rm -fr openstack.git
git clone --bare git@gitlab.thstack.com:devgroup/openstack.git
同步 Gerrit 的 openstack 項目到 Gitlab 上的 openstack 項目目錄中
當用戶 git review 后,代碼通過 jenkins 測試、人工 review 后,代碼只是 merge 到了 Gerrit 的 openstack 項目中,並沒有 merge 到 Gitlab 的 openstack 項目中,所以需要當 Gerrit openstack 項目倉庫有變化時自動同步到 Gitlab 的 openstack 項目倉庫中。Gerrit 自帶一個 Replication 功能,同時我們在安裝 Gerrit 時候默認安裝了這個 Plugin。現在只需要添加一個 replication.config 給 Gerrit。
添加 replication.config 文件
vim /etc/gerrit/etc/replication.config
[remote "openstack"]
# Gerrit 上要同步項目的名字
projects = openstack
url = root@gitlab.thstack.com:/home/git/repositories/devgroup/openstack.git
push = +refs/heads/*:refs/heads/*
push = +refs/tags/*:refs/tags/*
push = +refs/changes/*:refs/changes/*
threads = 3
上面的 url 是用 root 用戶來做 Gerrit 的 openstack 項目復制到 Gitlab 的 openstack 項目中,需要免密碼登錄,生成密鑰
ssh-copy-id -i gitlab.thstack.com # 輸入 root 用戶密碼
設置 ~/.ssh/config
vim ~/.ssh/config
Host gitlab.thstack.com:
IdentityFile ~/.ssh/id_rsa
PreferredAuthentications publickey
在 ~/.ssh/known_hosts 中,給 gitlab.thstack.com 添加 rsa 密鑰
> ~/.ssh/known_hosts
ssh-keyscan -t rsa gitlab.thstack.com > ~/.ssh/known_hosts
ssh-keygen -H -f ~/.ssh/known_hosts
重新啟動 Gerrit 服務
/etc/init.d/gerrit restart
Gerrit 的復制功能配置完畢,在 gerrit 文檔中有一個 ${name} 變量用來復制 Gerrit 的所有項目,這里並不需要。如果有多個項目需要復制,則在 replication.config 中添加多個 [remote ....] 字段即可。務必按照上面步驟配置復制功能。
在 Jenkins 上對 openstack 項目創建構建任務
用 admin 用戶登錄 jenkins,針對 openstack 項目創建構建任務 點擊 新建 -> 輸入任務名 -> 選中構建一個自由風格的軟件項目
選中 ‘git’ -> 在 ‘Repository URL’ 里輸入 openstack 項目在 Gerrit 上的地址(在 gerrit 的 project list 中 openstack 后面的 gitweb 里有具體 url,) -> 在 ‘Branches to build’ 里輸入 ‘origin/$GERRIT_BRANCH’
選中 Gerrit event -> 點擊 Add 分別添加 ‘Patchset Created’ 和 ‘Draft Published’ -> 在第一個 plain 輸入項目名字 openstack,第二個 plain 輸入 master -> 點擊 增加構建步驟 -> 選擇 ‘Execute shell’ -> 寫入要測試的腳本 -> 保存 同時也可以選擇 ‘添加構建后操作步驟’ 來郵件通知一個群組等功能。
提交 Review 任務
切換到 longgeek 用戶,之前創建了一個 longgeek 用戶,方便測試
su - longgeek
cd openstack # 之前添加過一個 testfile 文件,push 沒有權限,所以用 git review 來代替 git push
git push
git review
Creating a git remote called gerrit that maps to:
ssh://longgeek@review.thstack.com:29418/openstack.git
Your change was committed before the commit hook was installed
Amending the commit to add a gerrit change id
remote: Processing changes: new: 1, refs: 1, done
remote:
remote: New Changes:
remote: http://review.thstack.com/1
remote:
To ssh://longgeek@review.thstack.com:29418/openstack.git
* [new branch] HEAD -> refs/for/master/master
用 admin 用戶登錄 http://review.thstack.com 就可以看到 longgeek 用戶提交的 reivew 請求
可以看到 jenkins 已經通過,並打上了 ‘綠勾’ 標記,接下來 admin 用戶 +2 並提交就可以 Merge 代碼
在 Merge 代碼后,Gerrit 會自動同步到 Gitlab 上,如下圖
同步后,使用 git pull 命令就可以從 Gitlab 上的 openstack 倉庫下來代碼
cd openstack
git pull
From gitlab.thstack.com:devgroup/openstack
70c59ff..7948aae master -> origin/master
Already up-to-date.
如此便是一個完整的 CI 體系流程了,剩下的最重要的是如何用、真正的用起來。