Ubuntu之Gitlab、Gerrit、Jenkins協調工做配置

前提

  • 建立郵箱 user1@vzjc.compython

  • 建立郵箱 jenkins@vzjc.comgit

Gitlab配置

Gitlab已經配置了root帳戶的郵箱爲admin@vzjc.comshell

爲Gitlab的root用戶添加ssh密鑰

系統上建立admin用戶

$ sudo adduser adminvim

能夠獲得系統的用戶名admin和用戶本身設置的密碼服務器

系統上登陸admin帳戶,生成SSH密鑰

$ su - admin
$ ssh-keygen -C admin@vzjc.com
$ cat .ssh/id_rsa.pub
$ exit1234

這裏寫圖片描述

在Gitlab系統上更新SSH公鑰

  1. 用root帳號登陸http://gitlab.zjc.comssh

  2. 點擊頁面右上角的Profile Settings - 點擊左側的SSH Keys小鑰匙圖標 - 點擊Add SSH Key。在Key對應的輸入框中輸入上段落$cat .ssh/id_rsa.pub顯示的公鑰全文,點擊Title,應該會自動填充爲admin@vzjc.com 
    這裏寫圖片描述
    用了圈點Skitch,感受萌萌噠ide

建立用戶user1,做爲通常用戶

Gitlab上建立用戶user1

  1. 打開http://gitlab.zjc.com, 若是當前頁面爲登陸狀態就退出登陸工具

  2. 進入註冊頁面,輸入註冊信息,點擊Sign up按鈕來註冊用戶user1。gitlab

    這裏寫圖片描述

  3. 登陸郵箱點擊驗證鏈接,在Gitlab上成功建立user1帳號post

Ubuntu系統上建立user1帳號,生成SSH祕鑰

$ sudo adduser user1
$ su - user1
$ ssh-keygen -C user1@vzjc.com
$ cat .ssh/id_rsa.pub
$ exit12345

這裏寫圖片描述

這裏寫圖片描述

用user1登陸Gitlab,更新SSH公鑰

這裏寫圖片描述

Gitlab建立組和項目,並配置user1身份

用root登陸Gitlab,建立新Group爲dev-group

這裏寫圖片描述

這裏寫圖片描述

在dev-group中建立新項目test-project1

這裏寫圖片描述

這裏寫圖片描述

點擊 test-project1 工程界面左上的 dev-group

這裏寫圖片描述

點擊 Members

這裏寫圖片描述

把 user1 做爲 Reporter 身份添加到組 dev-group (Reporter能夠下載代碼可是沒上傳權限)

這裏寫圖片描述

用user1登陸Gitlab,查看新項目test-project1的git地址

地址爲git@gitlab.zjc.com:dev-group/test-project1.git

這裏寫圖片描述

這裏寫圖片描述

系統上登陸user1,克隆工程,測試權限

$ 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

這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

Gerrit配置

已經有了第一個帳戶admin並配置信箱爲admin@vzjc.com

系統上登陸admin,並獲取上步建立的公鑰

$ su - admin
$ cat ~/.ssh/id_rsa.pub
$ exit123

這裏寫圖片描述

用admin帳戶登陸gerrit,更新SSH公鑰

這裏寫圖片描述

這裏寫圖片描述

爲Gerrit系統建立用戶user1並配置

  1. 使用htpasswd工具建立user1和密碼

    $ sudo -u gerrit -H htpasswd /home/gerrit/gerrit/etc/htpasswd.conf user1

  2. 使用user1登陸gerrit,更新信箱爲user1@vzjc.com

  3. 更新SSH公鑰 (user1@zjc.com那個)

Jenkins配置

Jenkins系統已經建立了管理員帳戶jenkins並安裝了Gerrit Trigger插件

配置jenkins用戶

  1. 爲Gerrit系統建立用戶jenkins 
    $ sudo -u gerrit -H htpasswd /home/gerrit/gerrit/etc/htpasswd.conf jenkins

  2. 爲系統用戶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

用jenkins登陸Gerrit更新郵箱並驗證zjc@zjc:~$ id jenkins uid=122(jenkins) gid=131(jenkins) groups=131(jenkins) zjc@zjc:~$ sudo vim /etc/dovecot/conf.d/10-mail.conf而後修改first_valid_uid這個值
  • 1

  • 2

  • 3

  • 此處有個問題,若是在安裝Jenkins系統前沒有建立jenkins用戶(安裝時候系統會自動建立)而且使用的Dovecot來接收郵件而且用默認配置。那麼要先查一下是否系統用戶jenkins的uid是否大於500,若是不是,須要修改配置文件

用jenkins登陸Gerrit,更新SSH公鑰爲系統用戶jenkins的公鑰

Jenkins系統的SMTP設置 (根據具體狀況配置)

主頁面->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 
      這裏寫圖片描述
      這裏寫圖片描述

設置Gerrit Trigger

主頁面->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 和 Jenkins 整合

讓 Gerrit 支持 Jenkins

若是安裝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

這裏寫圖片描述
這裏寫圖片描述

用admin登陸Gerrit

如今提交的 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 保存更改

Gitlab上設置test-project1工程

前面咱們在Gitlab上搭建了一個 test-project1 的工程,普通用戶是沒有辦法去 push 的,只能使用 git review 命令提交. 而 git review 命令須要 .gitreview 文件存在於項目目錄裏。

用 admin用戶添加.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

這裏寫圖片描述

用 admin用戶添加.testr.conf 文件

Python 代碼我使用了 testr,須要先安裝 testr 命令

$ 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

這裏寫圖片描述

Gerrit上設置 test-project1工程

在 Gerrit 上建立 test-project1 項目

要知道 review 是在 gerrit 上,而 gerrit 上如今是沒有項目的,想讓 gitlab 上的項目能在 gerrit 上 review 的話,必須在 gerrit 上建立相同的項目,並有相同的倉庫文件.

用 admin 用戶在 Gerrit 上建立 test-project1 項目

$ su - admin
$ ssh -p 29418 admin@vzjc.com gerrit create-project test-project1
$ exit123

這裏寫圖片描述

clone –bare Gitlab 上的倉庫到 Gerrit

由於gerrit用戶無訪問gitlab的權限。因此要先看是否gerrit用戶下已經存在了id_rsa密鑰,若是沒有則建立,而後把公鑰加入到gitlab的admin帳戶上(這裏加到 admin 帳戶上,是由於後面Gerrit系統還會有個複製 git 庫到 Gitlab的功能,須要管理員權限)

賦予gerrit用戶訪問 test-project1 的權限
$ 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

這裏寫圖片描述

同步 Gerrit 的 test-project1 項目到 Gitlab 上的 test-project1 項目目錄中

當用戶 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
設置gerrit用戶的 ~/.ssh/config
$ sudo -u gerrit -H vim /home/gerrit/.ssh/config1

文件內容爲:

Host gitlab.zjc.com:
    IdentityFile ~/.ssh/id_rsa
    PreferredAuthentications publickey123
在gerrit用戶的~/.ssh/known_hosts 中,給 gitlab.zjc.com 添加 rsa 密鑰
$ 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
從新啓動 Gerrit 服務
$ sudo /etc/init.d/gerrit restart1
Gerrit 的複製功能配置完畢

在 gerrit 文檔中有一個 ${name} 變量用來複制 Gerrit 的全部項目,這裏並不須要。若是有多個項目須要複製,則在 replication.config 中添加多個 [remote ….] 字段便可。務必按照上面步驟配置複製功能。

在 Jenkins 上對 test-project1 項目建立構建任務

Jenkins上安裝git插件

安裝 Git Plugin

  • 用 jenkins 登陸 http://jenkins.zjc.com

  • Manage Jenkins -> Manage Plugins -> Avalabile -> 安裝 Git Plugin

Jenkins上建立項目

添加 test-project1工程

  • 用 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.


  • 這裏 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 提交一個更改

  1. 用 user1 登陸

  2. 刪除目錄 test-project1

  3. 克隆 test-project1 工程

  4. 進入 test-project1 目錄

  5. 添加文件、提交

  6. 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的錯誤,折中解決方案看這裏

這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述

用 admin 登陸 Gerrit 系統,經過驗證,提交

這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

登陸 Gitlab

查看 test-project1 工程,能夠看到新增長文件操做已經同步過來了

這裏寫圖片描述

參考

http://longgeek.com/

相關文章
相關標籤/搜索