轉載_CI 系統搭建:Git、Gerrit與Jenkins

轉載_CI 系統搭建:Git、Gerrit與Jenkins

去年寫的這五篇 CI 文章時候方便郵件測試,自己搞了一個 thstack.com 域名玩。當時也沒在意,所有的文章裏郵箱地址都是引用 @thstack.com 域名。讓我沒想到是,2014 年這個神奇的一年,thstack.com 會成爲我們的公司名字和域名。

  1. 我想說的是,我們內部的郵件系統也在用 @thstack.com,和這幾個文章裏的郵箱會衝突,導致一些朋友完全照着我的文檔測試。結果最近收到了很多垃圾郵件。
  2. 我還想說的是,不要讓別人懷疑你的智商。
  3. 我最想說的一句是,搞 CI 是一個高尚的工作。

隨着計算機的高速發展、各種時代變革的到來。百度、360、騰訊這些賺錢機器之間爲了利益掐個沒完沒了。開源項目、社區的活躍進而引來持續集成(CI)系統的誕生。也越發的聽到更多的人在說協同開發、敏捷開發、迭代開發、持續集成和單元測試這些拉風的術語。但是也僅僅只是聽到在說而已,也沒有見到國內有幾家公司能有完整的 CI 體系流程。反之一些開源項目都有完整的 CI 體系。我覺得就這一點國內的公司還是要去學習的。

由於對上面那些生澀的詞感冒,專門研究了下 CI 相關的系統,纔有了這幾篇文章

我用到的系統有:

  • Gitlab:代碼託管
  • Gerrit:Code Review
  • Jenkins:代碼測試

    一開始測試使用了 Gitorious 來做代碼的託管,發現界面的功能不全,比較蛋疼。雖然 Gerrit 本身有代碼託管功能,Gerrit 的界面不敢恭維、也沒有 Gitlab 的功能強大。so …

決定使用 Gitlab 還有一點重要原因就是它本身提供保護分支的功能,可以達到 Review 效果。這樣和 Gerrit 結合的話,可以針對不同的用戶羣來分配 Review 方式。

  1. 強制 Review:在 Gitlab 上創建的項目,指定相關用戶只有 Reporter 權限,這樣用戶沒有權限使用 Git push 功能,只能 git review 到 Gerrit 系統上,Jenkins 在監聽 Gerrit 上的項目事件會觸發構建任務來測試代碼,Jenkins 把測試結果通過 ssh gerrit 個這個項目打上 Verified 成功或失敗標記,成功通知其它人員 Review。

  2. Gitlab 保護 Master 分支:在 Gitlab 上創建的項目可以把 Master 分支保護起來,普通用戶可以自己創建分支並提交代碼到自己的分支上,沒有權限直接提交到 Master 分支,用戶最後提交申請把自己的分支 Merge 到 Master,管理員收到 Merge 請求後,Review 後選擇是否合併。

由於沒有閒置服務器、公網 IP、域名讓我去浪費。想到 qingcloud 上還有 2k 沒花,所以需要把三套系統整合塞到一個 vps 中。在整合三套系統中遇到一些需要規劃的小問題

  • 每個系統都有發送郵件的功能,最好弄三個郵箱賬號
  • 三個系統都默認監聽了 8080 端口,需要規劃端口
  • 規劃了端口的同時隨便規劃下三個域名,後端做個端口轉發方便用戶訪問

知道什麼需要規劃後,就來設置吧:

使用系統

ubuntu-12.04.2-server

修改 ssh 時候不需要輸入 yes,如果不設置後面 Jenkins 連接 Gerrit 時候會報錯

echo 'StrictHostKeyChecking  no' >> /etc/ssh/ssh_config
/etc/init.d/ssh restart

Gitlab、Gerrit、Jenkins 版本和下載地址

設置主機名

hostname www.thstack.com
sysctl -w kernel.hostname=www.thstack.com
echo www.thstack.com > /etc/hostname
echo '192.168.8.224 www.thstack.com www' >> /etc/hosts

註冊四個郵箱賬號,其中 [email protected] 用來管理三套系統

[email protected]
[email protected]
[email protected]
[email protected]

規劃三個域名和端口

gitlab.thstack.com:8081
review.thstack.com:8082
jenkins.thstack.com:8083

設置解析

vim /etc/hosts
192.168.8.224 gitlab.thstack.com gitlab
192.168.8.224 review.thstack.com review
192.168.8.224 jenkins.thstack.com jenkins

簡單規劃完後就可以動手去搭建了,後面幾篇文章會記錄安裝的過程。

二. GitLab 的安裝配置

上一篇文章 CI 系統搭建:一. 基礎環境設置、規劃 大概規劃了下環境,本文主要用來記錄安裝 Gitlab 的過程,主要參考官方文檔 並沒有做太多的修改。

設置源

設置國內 163 apt 源

# vim /etc/apt/sources.list
deb http://mirrors.163.com/ubuntu/ precise main universe restricted multiverse
deb http://mirrors.163.com/ubuntu/ precise-security universe main multiverse restricted
deb http://mirrors.163.com/ubuntu/ precise-updates universe main multiverse restricted
deb http://mirrors.163.com/ubuntu/ precise-proposed universe main multiverse restricted
deb http://mirrors.163.com/ubuntu/ precise-backports universe main multiverse restricted

deb-src http://mirrors.163.com/ubuntu/ precise main universe restricted multiverse
deb-src http://mirrors.163.com/ubuntu/ precise-security universe main multiverse restricted
deb-src http://mirrors.163.com/ubuntu/ precise-proposed universe main multiverse restricted
deb-src http://mirrors.163.com/ubuntu/ precise-backports universe main multiverse restricted
deb-src http://mirrors.163.com/ubuntu/ precise-updates universe main multiverse restricted
# apt-get update

安裝依賴包

Gitlab 依賴包、庫

sudo apt-get install -y build-essential zlib1g-dev libyaml-dev libssl-dev libgdbm-dev libreadline-dev \
                        libncurses5-dev libffi-dev curl openssh-server redis-server checkinstall \
                        libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev logrotate

安裝 markdown 文檔風格依賴包

sudo apt-get install -y python-docutils 

安裝 git,Gerrit 依賴 gitweb,同時 GitLab 依賴 git 版本 >= 1.7.10,Ubuntu apt-get 默認安裝的是 1.7.9.5,當然不升級也是沒有問題的

sudo apt-get install -y git git-core gitweb git-review

升級 Git 版本(可選)

sudo apt-get install -y git gitweb
sudo apt-get remove git-core
sudo apt-get install -y libcurl4-openssl-dev libexpat1-dev gettext libz-dev libssl-dev build-essential
cd /tmp
curl --progress https://git-core.googlecode.com/files/git-1.8.4.1.tar.gz | tar xz
cd git-1.8.4.1/
make prefix=/usr/local all
sudo make prefix=/usr/local install
# * 如果升級了 git 的版本,相應修改 Gitlab.yml 中的 git 腳本位置,這一步在 clone gitlab 後在操作*
sudo -u git -H vim /home/git/gitlab/config/gitlab.yml
bin_path: /usr/local/bin/git

Gitlab 需要收發郵件,安裝郵件服務器

sudo apt-get install -y postfix

如果安裝了 ruby1.8,卸載掉,Gitlab 依賴 2.0 以上

sudo apt-get remove ruby1.8

下載編譯 ruby2.0

mkdir /tmp/ruby && cd /tmp/ruby
curl --progress ftp://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p353.tar.gz | tar xz
cd ruby-2.0.0-p353
./configure --disable-install-rdoc
make
sudo make install

修改 gem 源指向 taobao

gem source -r https://rubygems.org/
gem source -a http://ruby.taobao.org/

安裝 Bundel 命令

sudo gem install bundler --no-ri --no-rdoc

系統用戶

給 Gitlab 創建一個 git 用戶

sudo adduser --disabled-login --gecos 'GitLab' git

GitLab Shell

下載 Gitlab Shell,用來 ssh 訪問倉庫的管理軟件

cd /home/git
sudo -u git -H git clone https://github.com/gitlabhq/gitlab-shell.git
cd gitlab-shell
sudo -u git -H cp config.yml.example config.yml

修改 gitlab-shell/config.yml

sudo -u git -H vim /home/git/gitlab-shell/config.yml
gitlab_url: "http://gitlab.thstack.com/"

安裝 GitLab Shell

cd /home/git/gitlab-shell
sudo -u git -H ./bin/install

Mysql

安裝 MySQL 包

sudo apt-get install -y mysql-server mysql-client libmysqlclient-dev

給 Gitlab 創建 mysql 數據庫並授權用戶訪問

sudo mysql -uroot -p
> create database gitlabdb;
> grant all on gitlabdb.* to 'gitlabuser'@'localhost' identified by 'gitlabpass';

GitLab

下載 GitLab 源代碼,並切換到最新的分支上

cd /home/git
sudo -u git -H git clone https://github.com/gitlabhq/gitlabhq.git gitlab
cd gitlab
sudo -u git -H git checkout 6-4-stable

配置 GitLab,修改 gitlab.yml,其中 host: 項和 gitlab-shell 中 gitlab_url 的主機一致

cd /home/git/gitlab
sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml
sudo -u git -H vim config/gitlab.yml
host: gitlab.thstack.com
email_from: [email protected]
support_mail: [email protected]
signup_enabled: true             #開啓用戶註冊

創建相關目錄

cd /home/git/gitlab
sudo -u git -H mkdir tmp/pids/
sudo -u git -H mkdir tmp/sockets/
sudo -u git -H mkdir public/uploads 
sudo -u git -H mkdir /home/git/repositories
sudo -u git -H mkdir /home/git/gitlab-satellites

修改相關目錄權限

sudo chown -R git:git log/ tmp/
sudo chmod -R u+rwX  log/ tmp/ public/uploads

修改 unicorn.rb 監聽端口爲:8081

cd /home/git/gitlab/
sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb
sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb
sudo -u git -H vim config/unicorn.rb
listen "gitlab.thstack.com:8081", :tcp_nopush => true

配置 GitLab 訪問 mysql 數據庫設置

cd /home/git/gitlab/
sudo -u git cp config/database.yml.mysql config/database.yml
sudo -u git -H vim config/database.yml  
*修改 Production 部分:*
production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: gitlabdb
  pool: 10
  username: gitlabuser
  password: "gitlabpass"
  host: localhost
  socket: /var/run/mysqld/mysqld.sock

設置 GitLab 使用指定郵箱發送郵件,注意 production.rb 的文件格式,開頭空兩格

cd /home/git/gitlab/
sudo -u git -H vim config/environments/production.rb
  #修改 :sendmail 爲 :smtp
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :address              => "smtp.googlemail.com",
    :port                 => 587,
    :domain               => 'thstack.com',
    :user_name            => '[email protected]',
    :password             => 'password',
    :authentication       =>  :plain,
    :enable_starttls_auto => true
  }
end          # 上面內容加入到 end 裏面

安裝 gem

修改 Gemfile 文件中源指向爲 taobao

cd /home/git/gitlab/
sudo -u git -H vim Gemfile
source "http://ruby.taobao.org/"

雖然在文件中指向了國內 taobao 源,但是依然會卡一會,耐心等待…

cd /home/git/gitlab/
sudo -u git -H bundle install --deployment --without development test postgres aws

初始化數據庫並激活高級功能

cd /home/git/gitlab/
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production

輸入 yes 來初始化數據庫、創建相關表,最後會輸出 GitLab Web 管理員用來登錄的賬號和密碼

Do you want to continue (yes/no)? yes
...
Administrator account created:
[email protected]
password......5iveL!fe

設置 GitLab 啓動服務

cd /home/git/gitlab/
sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab
sudo update-rc.d gitlab defaults 21

設置 GitLab 使用 Logrotate 備份 Log

cd /home/git/gitlab/
sudo cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab

檢查GitLab及其環境的配置是否正確:

cd /home/git/gitlab/
sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production
System information
System:         Ubuntu 12.04
Current User:   git
Using RVM:      no
Ruby Version:   2.0.0p353
Gem Version:    2.0.14
Bundler Version:1.3.5
Rake Version:   10.1.0

GitLab information
Version:        6.4.2
Revision:       214a013
Directory:      /home/git/gitlab
DB Adapter:     mysql2
URL:            http://gitlab.thstack.com
HTTP Clone URL: http://gitlab.thstack.com/some-project.git
SSH Clone URL:  [email protected]:some-project.git
Using LDAP:     no
Using Omniauth: no

GitLab Shell
Version:        1.8.0
Repositories:   /home/git/repositories/
Hooks:          /home/git/gitlab-shell/hooks/
Git:            /usr/bin/git

啓動 GitLab 服務

/etc/init.d/gitlab restart
Shutting down both Unicorn and Sidekiq.
GitLab is not running.
Starting both the GitLab Unicorn and Sidekiq..
The GitLab Unicorn web server with pid 17771 is running.
The GitLab Sidekiq job dispatcher with pid 17778 is running.
GitLab and all its components are up and running

最後編譯一下

sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production

Nginx

安裝 Nginx 包

sudo apt-get install -y nginx

配置 Nginx

cd /home/git/gitlab
sudo cp lib/support/nginx/gitlab /etc/nginx/sites-available/gitlab
sudo ln -s /etc/nginx/sites-available/gitlab /etc/nginx/sites-enabled/gitlab
sudo vim /etc/nginx/sites-available/gitlab
listen *:80 default_server;
server_name gitlab.thstack.com;
proxy_pass http://gitlab.thstack.com:8081;

啓動 Nginx

/etc/init.d/apache2 stop
/etc/init.d/nginx restart

訪問

用瀏覽器訪問: http://gitlab.thstack.com
用戶名:[email protected]
密碼:5iveL!fe

2: https://github.com/gitlabhq/gitlabhq/blob/master/doc/install/insta llation.md

界面簡單使用

使用 [email protected] 賬號登錄: gitlab-login

登錄後系統讓修改密碼,先修改密碼: gitlab-set_pass

修改完密碼後,用新密碼登錄一下,然後修改 Admin 用戶得郵箱地址爲:[email protected] gitlab-set-email

點擊保存更改後,系統會自動給剛纔輸入的郵箱地址發送一封確認修改信息,點擊郵件內容中的連接後會自動用新賬號郵箱登錄。

創建一個 GROUP:

gitlab-click-group

輸入 Group 名字爲:DevGroup gitlab-create-gourp

點擊創建項目: gitlab-click-project

創建一個項目,名爲:OpenStack,這個項目屬於 DevGroup: gitlab-create-project

創建完項目後,點擊添加 ssh 密鑰: gitlab-click-sshkey

生成 [email protected] 郵箱密鑰: gitlab-gen-sshkey

在界面輸入剛纔生成得密鑰: gitlab-add-sshkey

再註冊一個賬號,登錄頁面點擊註冊按鈕: gitlab-click-user

註冊一個 Longgeek 用戶,輸入郵箱、密碼,然後去輸入得郵箱驗證: gitlab-create-user

創建一個 Longgeek 用戶,並生成密鑰: gitlab-longgeek-gen-sshkey

[email protected] 用戶登錄 GitLab,添加剛纔生成得密鑰到 sshkey 裏: gitlab-longgeek-add-sshkey

[email protected] 用戶登錄 GitLab,把 [email protected] 添加到 DevGroup 組中,權限是 Reporter,這樣 longgeek 用戶也可以訪問 openstack 這個項目,不過沒有權限直接 Push: gitlab-add-longgeek-gourp

[email protected] 用戶登錄就可以看到 OpenStack 項目: gitlab-longgeek-view-project

[email protected] clone 項目,嘗試 push: gitlab-longgeek-try-push

很顯然 longgeek 用戶是沒有 push 到 master 分支得權限。接下來會安裝 Gerrit、Jenkins。以及它們三個如何整合成流程。請參考後面得文章。

三. Gerrit 的安裝配置

重新安裝 gerrit 需要特別注意:

  • 如果有項目在使用記得備份 /etc/gerrit/git 目錄
  • rm -fr /etc/gerrit
  • mysql -uroot -p
  • >>>drop database gerritdb;
  • >>>create database gerritdb;
  • 重新安裝吧。
  • 如果 /etc/gerrit/logs/error_log 裏出現 java.lang.IllegalStateException: Missing project All-Projects 這個錯誤,那就重新安裝下,記得清除一下 db。

之前寫過兩篇文章,CI 系統搭建:一. 基礎環境設置、規劃CI 系統搭建:二. GitLab 的安裝配置,本文主要記錄 Gerrit 的安裝的過程。

下載 Gerrit 包

目前最新版爲 2.8

wget http://gerrit-releases.storage.googleapis.com/gerrit-2.8.war 

安裝 Gerrit 依賴

Gerrit 的包是 Java 格式,需要安裝 jre

apt-get install default-jre daemon

創建 Gerrit 數據庫

上一篇再安裝 GitLab 時已經安裝了 Mysql,直接創建庫

sudo mysql -uroot -p
> create database gerritdb;
> grant all on gerritdb.* to 'gerrituser'@'localhost' identified by 'gerritpass';

開始安裝

把 gerrit 安裝再 /etc/gerrit/ 下

# java -jar gerrit-2.8.war init -d /etc/gerrit/

*** Gerrit Code Review 2.8
***

Create '/etc/gerrit'           [Y/n]? y

*** Git Repositories
***

Location of Git repositories   [git]:

*** SQL Database
***

Database server type           [h2]: mysql

Gerrit Code Review is not shipped with MySQL Connector/J 5.1.21
**  This library is required for your configuration. **
Download and install it now [Y/n]? y
Downloading http://repo2.maven.org/maven2/mysql/mysql-connector-java/5.1.21/mysql-connector-java-5.1.21.jar ... OK
Checksum mysql-connector-java-5.1.21.jar OK
Server hostname                [localhost]:
Server port                    [(mysql default)]:
Database name                  [reviewdb]: gerritdb
Database username              [root]: gerrituser
gerrituser's password          :
              confirm password :

*** User Authentication
***

Authentication method          [OPENID/?]: http
Get username from custom HTTP header [y/N]? n
SSO logout URL                 :

*** Email Delivery
***

SMTP server hostname           [localhost]: smtp.googlemail.com
SMTP server port               [(default)]: 587
SMTP encryption                [NONE/?]: tls
SMTP username                  [root]: [email protected]
[email protected]'s password  :
              confirm password :

*** Container Process
***

Run as                         [root]:
Java runtime                   [/usr/lib/jvm/java-6-openjdk-amd64/jre]:
Copy gerrit-2.8.war to /etc/gerrit/bin/gerrit.war [Y/n]? y
Copying gerrit-2.8.war to /etc/gerrit/bin/gerrit.war

*** SSH Daemon
***

Listen on address              [*]:
Listen on port                 [29418]:

Gerrit Code Review is not shipped with Bouncy Castle Crypto v144
  If available, Gerrit can take advantage of features
  in the library, but will also function without it.
Download and install it now [Y/n]? y
Downloading http://www.bouncycastle.org/download/bcprov-jdk16-144.jar ... OK
Checksum bcprov-jdk16-144.jar OK
Generating SSH host key ... rsa... dsa... done

*** HTTP Daemon
***

Behind reverse proxy           [y/N]? y
Proxy uses SSL (https://)      [y/N]? n
Subdirectory on proxy server   [/]:
Listen on address              [*]:
Listen on port                 [8081]: 8082
Canonical URL                  [http://www.thstack.com/]: http://review.thstack.com/

*** Plugins
***

Install plugin reviewnotes version v2.8 [y/N]? y
Install plugin download-commands version v2.8 [y/N]? y
Install plugin replication version v2.8 [y/N]? y
Install plugin commit-message-length-validator version v2.8 [y/N]? y

Initialized /etc/gerrit
Executing /etc/gerrit/bin/gerrit.sh start
Starting Gerrit Code Review: OK
Waiting for server on review.thstack.com:80 ... OK
Opening http://review.thstack.com/#/admin/projects/ ...FAILED
Open Gerrit with a JavaScript capable browser:

http://review.thstack.com/#/admin/projects/

在安裝完後 Gerrit 默認會打開瀏覽器,由於我的系統是 Server 版沒有桌面和瀏覽器,所以纔會出現上面倒數第三行的 …FAILED 同時從上面的信息看出我使用了 http 方式驗證、啓用代理服務器,指定了 Gerrit 端口爲 8082,URL 爲 review.thstack.com。需要在 nginx 裏設置下端口轉發。

Gerrit 啓動腳本

cp /etc/gerrit/bin/gerrit.sh /etc/init.d/gerrit
vim /etc/init.d/gerrit
GERRIT_SITE=/etc/gerrit/       # 在代碼 47 行增加
sudo update-rc.d gerrit defaults 21
service gerrit restart

Nginx

修改 Nginx 配置文件,給 Gerrit 做端口轉發和訪問控制,把下面內容寫入到文件最後

# vim /etc/nginx/sites-available/gitlab
server {
  listen *:80;
  server_name review.thstack.com;
  allow   all;
  deny    all;
  auth_basic "THSTACK INC. Review System Login";
  auth_basic_user_file /etc/gerrit/etc/htpasswd.conf;

  location / { 
    proxy_pass  http://review.thstack.com:8082;
  }   
}

創建 htpasswd.conf 文件,並添加 admin 用戶、密碼到文件中

# touch /etc/gerrit/etc/htpasswd.conf
# htpasswd /etc/gerrit/etc/htpasswd.conf admin
  New password: 
  Re-type new password: 
  Adding password for user admin

默認第一個登錄 Gerrit 的用戶是 Admin。

訪問

在瀏覽器 url 輸入:http://review.thstack.com/,記得添加 hosts 解析。由於做了訪問控制,會出現如下驗證框,輸入剛纔創建的 admin 和密碼 gerrit-admin-access

註冊 admin 的郵箱,並添加 [email protected] 密鑰 gerrit-admin-sshkey

使用 htpasswd 創建 longgeek 用戶和密碼

htpasswd /etc/gerrit/etc/htpasswd.conf longgeek

換個瀏覽器或清除下瀏覽器保存的 admin 的密碼 訪問 http://review.thstack.com/ 輸入 longgeek 和密碼: gerrit-longgeek-login

註冊郵箱和添加 [email protected] 的密鑰 gerrit-longgeek-create-user

最後

如果想 Gitlab 上創建的項目使用 Gerrit 的 Code Review 功能,兩個系統的用戶必須統一,也就是說不管哪個用戶使用 Gerrit,前提是這個用戶在 Gitlab 和 Gerrit 上都已註冊,郵箱一致、sshkey 一致。當然 Nginx 訪問控制用戶的密碼那就隨意了。至於 Gitlab 上創建的項目如何同步到 Gerrit 上、Gitlab 如何使用 Gerrit 的 Code Review 功能等等都在 Jenkins 安裝完之後會一起整合在一起。請關注後面的文章。

四. Jenkins 的安裝配置


本文作爲之前幾篇文章的延續,在動手安裝 Jenkins 之前,確保參考瞭如下文章:

Jenkins

官網上簡單介紹 Jenkins 是一個可擴展的開源持續集成服務器,可擴展 Jenkins 系統集羣方式、大量的插件方式擴展,顯然 Jenkins 自己已經形成了一個小生態圈。還有一點不得不說的是 Jenkins 本身的迭代開發非常猛,看了下版本發佈週期平均一週一個版本。具體的 Jenkins 介紹可以去看 IBM developerWorks 的文章。

下載包

在寫這篇文章時候,Jenkins 最新版本爲 1.544

wget http://pkg.jenkins-ci.org/debian/binary/jenkins_1.544_all.deb

Jenkins

安裝依賴包

apt-get install daemon

安裝 Jenkins

dpkg -i jenkins_1.544_all.deb

jenkins 默認監聽了 8080 端口,修改爲 8083

vim /etc/default/jenkins
HTTP_PORT=8083

重新 jenkins 服務

/etc/init.d/jenkins restart

Nginx

配置 Nginx 端口轉發,在文件末尾加入下面配置

# vim /etc/nginx/sites-available/gitlab
server {
  listen *:80;
  server_name jenkins.thstack.com;

  location / {
    proxy_pass  http://jenkins.thstack.com:8083;
  }
}

重啓 Nginx,就可以用 jenkins.thstack.com 訪問 Jenkins 了

/etc/init.d/nginx restart

訪問

http://jenkins.thstack.com/ jenkins-dashboard

開啓用戶註冊功能,點擊 -> 系統管理 -> Configure Global Security -> 勾上啓用安全,就可以看到下圖 jenkins-user-set

保存後,會自動跳轉到登錄頁面,點擊右上角註冊按鈕 jenkins-user-restryges

輸入管理員信息 jenkins-create-admin

爲了安全,設置 Jenkins 不對普通用戶開放登錄權限,只有管理員可以設置、構建任務,普通用戶可以查看任務狀態 點擊 系統管理 -> Configure Global Security -> 去掉開放用戶註冊勾 jenkins-disable-user-siup

接下來要安裝 Jenkins 的插件,來支持 Gerrit 點擊 系統管理 -> 管理插件,會看到下圖顯示 jenkins-plugin

如果上圖種 可更新、可選插件、已安裝 三個菜單點開爲空白的話,需要獲取更新下 Jenkins 的信息,之後就可以看到插件信息了 點擊 系統管理 -> 管理插件 -> 高級 jenkins-update-info

安裝 Gerrit Trigger 插件 點擊 系統管理 -> 管理插件 -> 可選插件 -> “Gerrit Trigger” jenkins-select-gerrit

安裝完 Gerrit Trigger 插件重啓 Jenkins,進度掉如果卡在 Gerrit Trigger 最後時,嘗試刷新下頁面 jenkins-install-gerrit

在系統中給 Jenkins 用戶生成 ssh 密鑰,Jenkins 用戶在安裝包的時候自動創建了,家目錄在 /var/lib/jenkins

su - jenkins
ssh-keygen -C [email protected]
cat ~/.ssh/id_rsa.pub         # 把公鑰內容複製一下,後面需要添加到 Gerrit 中

用 htpasswd 創建 jenkins 用戶訪問控制密碼

htpasswd /etc/gerrit/etc/htpasswd.conf jenkins

繼續換個瀏覽器或者清除瀏覽器記錄,用 jenkins 用戶訪問 Gerrit
http://review.thstack.com jenkins-review-login

設置 jenkins 用戶郵箱,順便去郵箱裏確認 jenkins-jenkins-mail

添加剛纔對 [email protected] 郵箱生成的 ssh 密鑰 jenkins-sshkey

設置 Jenkins 系統 SMTP 點擊 -> 系統管理 -> 系統設置 -> 找到郵件通知欄 -> 高級 jenkins-email

回到 Jenkins,設置 Gerrit Trigger 插件,填寫剛纔在 Gerrit 上註冊的 jenkins 用戶信息 點擊 -> 系統管理 -> Gerrit Trigger -> 填寫 Gerrit Server 中的信息,最後點擊 Test Connection,會在左側出現 Success,如果失敗檢查輸入的信息是否有誤*

2014.04.05 update:

點擊 ‘Test Connection’ 如果出現下面內容,不要着急,繼續跟着我的博客往下做。這個問題會在第五篇文章裏解決掉。

User jenkins has no capability to connect to Gerrit event stream.

jenkins-gerrit-trigger-ssh

如果上面一步出現下圖錯誤,是因爲 jenkins ssh Gerrit 時候需要輸入 yes,卡住了 可以手工 ssh 輸入一下 也是。在返回界面 多刷新幾次就沒有錯誤信息了。 su – jenkins ssh -p 29418 [email protected] gerrit

jenkins-gerrit-conn-err

由於 Gerrit 的版本爲 2.8,Gerrit 在 2.7 後做了些更改,去掉了 gerrit approve 的 approve 命令,而改爲 review,所以需要修改 點擊 -> 系統管理 -> Gerrit Trigger -> Gerrit Reporting Values -> Code Review -> 點擊 “高級” jenkins-gerrit-approve-to-review

修改 gerrit approve 爲 gerrit review jenkins-gerrit-reviews

OK,Jenkins 設置完畢,接下來就需要針對項目在 jenkins 上創建構建任務,放在下一篇文章裏。

五. GitLab、Gerrit、Jenkins 三者整合

參考之前的文章:

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 '[email protected]'
# git remote add origin ssh://[email protected]: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’ 組開放

gerrit-Non-Interactive-Users

[email protected] 用戶登錄 Gerrit 系統,添加 [email protected] 用戶到 ‘Non-Interactive Users’ 組

點擊 -> People -> List Groups -> Non-Interactive Users gerrit-add-jenkins-user-in-non-interactive-group

現在提交的 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 -> 在最下面點擊保存更改 gerrit-add-verified-lable

GitLab 上爲 openstack 項目的一些準備

.gitreview

之前在 CI 系統搭建:二. GitLab 的安裝配置 文章中在 GitLab 上創建了一個叫 openstack 項目,並設置了權限,普通用戶是沒有辦法去 push 的,只能使用 git review 命令提交. 而 git review 命令需要 .gitreview 文件存在於項目目錄裏,用 [email protected] 用戶添加 .gitreview 文件

git clone [email protected]:devgroup/openstack.git
cd openstack
git config user.name 'admin'
git config user.email '[email protected]'

編輯 .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 [email protected] gerrit create-project openstack

clone –bare Gitlab 上的倉庫到 Gerrit

在 Gerrit 上 clone Gitlab 的 openstack 項目

cd /etc/gerrit/git
rm -fr openstack.git
git clone --bare [email protected]: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 = [email protected]:/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 項目創建構建任務 點擊 新建 -> 輸入任務名 -> 選中構建一個自由風格的軟件項目 jenkins-create-task

選中 ‘git’ -> 在 ‘Repository URL’ 裏輸入 openstack 項目在 Gerrit 上的地址(在 gerrit 的 project list 中 openstack 後面的 gitweb 裏有具體 url,) -> 在 ‘Branches to build’ 裏輸入 ‘origin/$GERRIT_BRANCH’ jenkins-create-task1

選中 Gerrit event -> 點擊 Add 分別添加 ‘Patchset Created’ 和 ‘Draft Published’ -> 在第一個 plain 輸入項目名字 openstack,第二個 plain 輸入 master -> 點擊 增加構建步驟 -> 選擇 ‘Execute shell’ -> 寫入要測試的腳本 -> 保存 同時也可以選擇 ‘添加構建後操作步驟’ 來郵件通知一個羣組等功能。 jenkins-create-task2

提交 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://[email protected]: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://[email protected]:29418/openstack.git
* [new branch]      HEAD -> refs/for/master/master

用 admin 用戶登錄 http://review.thstack.com 就可以看到 longgeek 用戶提交的 reivew 請求 gerrit-review

可以看到 jenkins 已經通過,並打上了 ‘綠勾’ 標記,接下來 admin 用戶 +2 並提交就可以 Merge 代碼 gerrit-review-submit

在 Merge 代碼後,Gerrit 會自動同步到 Gitlab 上,如下圖 gitlab-replication-ok

同步後,使用 git pull 命令就可以從 Gitlab 上的 openstack 倉庫下來代碼

cd openstack
git pull
From gitlab.thstack.com:devgroup/openstack
   70c59ff..7948aae  master     -> origin/master
Already up-to-date.

如此便是一個完整的 CI 體系流程了,剩下的最重要的是如何用、真正的用起來。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章