整合Gitolite、Jenkins、Gerrit 協同工做

  • Gitolite 代碼託管
  • Gerrit 代碼審覈
  • Jenkins 編譯測試
    各個環境的搭建能夠參考個人其餘幾篇文章

在基本環境的基礎上,須要用到:java

  • git hook
  • Jenkins 的 Generic Webhook Trigger插件
  • Gerrit 的 replication 插件

本例以PostgreSQL項目爲例git

1.Gerrit

1.1安裝 replication 插件
若是安裝Gerrit時安裝了replication插件,能夠忽略這一項。
插件安裝:web

[gerrit@localhost ~]$ java -jar gerrit-2.12.4.war init -d gerrit_site --batch --install-plugin replication

1.2 建立postgresql項目
該項目事先存在於Gitolite中sql

[gerrit@localhost git]$ pwd
/home/gerrit/gerrit_site/git
[gerrit@localhost git]$ ssh -p 29418 -i ~/.ssh/id_rsa 192.168.81.186 -l gerrit gerrit create-project postgresql
[gerrit@localhost git]$ ll
total 0
drwxrwxr-x. 7 gerrit gerrit 100 Jan 25 02:56 All-Projects.git
drwxrwxr-x. 7 gerrit gerrit 100 Jan 25 02:56 All-Users.git
drwxrwxr-x. 7 gerrit gerrit 100 Jan 29 04:35 postgresql.git
[gerrit@localhost git]$ rm -rf postgresql.git/
[gerrit@localhost git]$ git clone --bare git@127.0.0.1:postgresql.git
Cloning into bare repository 'postgresql.git'...
remote: Counting objects: 515391, done.
remote: Compressing objects: 100% (78522/78522), done.
remote: Total 515391 (delta 434772), reused 515391 (delta 434772)
Receiving objects: 100% (515391/515391), 151.21 MiB | 6.49 MiB/s, done.
Resolving deltas: 100% (434772/434772), done.

1.3 添加replication.config給gerritshell

[gerrit@localhost ~]$ cd gerrit_site/etc/
[gerrit@localhost etc]$ vi replication.config
[remote "postgresql"]
projects = postgresql
url = git@127.0.0.1:postgresql.git
push = +refs/heads/*:refs/heads/*
push = +refs/tags/*:refs/tags/*
push = +refs/changes/*:refs/changes/*
threads = 3

1.4 設置gerrit用戶的 ~/.ssh/configssh

[gerrit@localhost ~]$ sh -c "ssh-keyscan -t rsa 127.0.0.1 >> /home/gerrit/.ssh/known_hosts"
[gerrit@localhost ~]$ sh -c "ssh-keygen -H -f /home/gerrit/.ssh/known_hosts"

1.5.從新啓動 Gerrit 服務curl

[gerrit@localhost ~]$ /home/gerrit/gerrit_site/bin/gerrit.sh restart

2.Jenkins

2.1 安裝Generic Webhook Trigger Plugin 插件
系統管理 =》管理插件 =》可選插件
使用過濾功能找到"Generic Webhook Trigger Plugin"插件並安裝(因爲我已經安裝好了,所在不顯示出來,能夠在已安裝找到)
輸入圖片說明
輸入圖片說明
2.2 新建PostgreSQL項目
輸入圖片說明
構建觸發器配置項目觸發條件,勾選Generic Webhook Trigger,在此使用Request parameters,填寫自定義的參數和值
輸入圖片說明
構建 =》Execute shell
簡單編寫一個clone代碼而後編譯的動做post

git clone git@127.0.0.1:postgresql
cd postgresql
./configure --prefix=$WORKSPACE/pgsql
make 
make install

輸入圖片說明
2.3 獲取API Token
在「系統管理–管理用戶–用戶列表」 點擊用戶後面的設置圖標,獲取 API Token
輸入圖片說明測試

3.Gitolite

添加 hook
進入postgresql項目的hooks目錄,添加post-receive,使用curl命令發送post請求url

[git@localhost hooks]$ pwd
/home/git/repositories/postgresql.git/hooks
[git@localhost hooks]$ touch post-receive
[git@localhost hooks]$ vi post-receive
#!/bin/sh
curl "http://highgo:8703ec11eeea10bb0be850d76c5ef2f7@192.168.81.186:8080/generic-webhook-trigger/invoke?start=1"
# 命令參數說明:http://用戶名:API_Token@IP:端口//generic-webhook-trigger/invoke?自定義參數=參數值
[git@localhost hooks]$ chmod +x post-receive

修改項目權限,普通用戶不能寫入

[git@localhost conf]$ pwd
/home/git/gitolite-admin/conf
[git@localhost conf]$ vi gitolite.conf
repo gitolite-admin
    RW+     =   admin

repo postgresql
    R       =   @all
    RW+     =   admin gerrit

repo testing
    RW+     =   @all

提交修改

[git@localhost conf]$ git add gitolite.conf 
[git@localhost conf]$ git commit -am "change gitolite.conf"
[git@localhost conf]$ git push origin master
  1. 測試

用戶clone代碼,修改遠程倉庫到gerrit,修改提交

[highgo@localhost Desktop]$ git clone git@127.0.0.1:postgresql
[highgo@localhost Desktop]$ cd postgresql/
[highgo@localhost postgresql]$ touch testfile.txt
[highgo@localhost postgresql]$ git remote rm origin
[highgo@localhost postgresql]$ git remote add origin ssh://highgo@192.168.81.186:29418/postgresql.git
[highgo@localhost postgresql]$ git config user.name highgo
[highgo@localhost postgresql]$ git config user.email highgo@test.com
[highgo@localhost postgresql]$ git config remote.origin.push refs/heads/*:refs/for/*
[highgo@localhost postgresql]$ scp -p -P 29418 highgo@192.168.81.186:hooks/commit-msg .git/hooks/
[highgo@localhost postgresql]$ git add testfile.txt 
[highgo@localhost postgresql]$ git commit -am "add testfile.txt"
[highgo@localhost postgresql]$ git push

gerrit審覈提交併經過
輸入圖片說明 git通知Jenkins執行項目的編譯安裝 到此完成了簡單的整合工做 (在用戶提交到gerrit的時候,也會觸發hook,審覈經過再次觸發,也就是說Jenkins會執行兩次,有待解決)

相關文章
相關標籤/搜索