天天看點

2、Gerrit配置--使用者配置

前面一章已經搭建好了Gerrit伺服器,下面介紹如何建立Gerrit使用者,并使用使用者登入已經改配使用者設定

環境:

Gerrit Server:172.16.206.133 

Client:172.16.206.129

1、在Gerrit伺服器上建立使用者

Gerrit伺服器使用的是HTTP認證類型,并用httpd做反向代理,建立使用者使用以下指令

#cd  /home/gerrit2/gerrit/etc

[root@localhost etc]# htpasswd -m passwords user2

New password: 

Re-type new password: 

Adding password for user user2

根據提示輸入密碼即可(密碼設定為123456),這樣賬号就建立好了。建立完成,httpd和gerrit服務不需要重新開機。

2、登入gerrit使用者

用建立的user2賬号通路gerrit,在浏覽器位址欄輸入:

<a href="http://172.16.206.133:8080/" target="_blank">http://172.16.206.133:8080</a>

輸入登入的賬号密碼,賬号為user2,密碼為123456

3、修改gerrit使用者配置

登入後,在網頁右上角點選自己的使用者名,在點選“settings”。

<a href="http://s2.51cto.com/wyfs02/M00/7F/E4/wKioL1cxT6yh3itBAABV-Z3w2RA103.png" target="_blank"></a>

1、送出公鑰

注意:為什麼要送出公鑰檔案到gerrit伺服器?如果不送出公鑰,則不能使用git push指令将本地代碼送出到gerrit伺服器。公鑰是一個認證方式。

在用戶端172.16.206.129這台伺服器上建立公、私鑰對,具體指令如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<code>[user2@host2 root]$ </code><code>ssh</code><code>-keygen -t rsa </code>

<code>Generating public</code><code>/private</code> <code>rsa key pair.</code>

<code>Enter </code><code>file</code> <code>in</code> <code>which</code> <code>to save the key (</code><code>/home/user2/</code><code>.</code><code>ssh</code><code>/id_rsa</code><code>): </code>

<code>Created directory </code><code>'/home/user2/.ssh'</code><code>.</code>

<code>Enter passphrase (empty </code><code>for</code> <code>no passphrase): </code>

<code>Enter same passphrase again: </code>

<code>Your identification has been saved </code><code>in</code> <code>/home/user2/</code><code>.</code><code>ssh</code><code>/id_rsa</code><code>.</code>

<code>Your public key has been saved </code><code>in</code> <code>/home/user2/</code><code>.</code><code>ssh</code><code>/id_rsa</code><code>.pub.</code>

<code>The key fingerprint is:</code>

<code>fd:da:fa:c5:ec:6c:82:60:1f:39:15:</code><code>bc</code><code>:62:ec:51:15 user2@host2</code>

<code>The key's randomart image is:</code>

<code>+--[ RSA 2048]----+</code>

<code>|           . .E. |</code>

<code>|            +    |</code>

<code>|         . . o   |</code>

<code>|         .= o    |</code>

<code>|        So.=     |</code>

<code>|        o =. o   |</code>

<code>|       . o +. +  |</code>

<code>|          oo.+.  |</code>

<code>|          ooooo  |</code>

<code>+-----------------+</code>

生成的密鑰檔案如下:

[user2@host2 ~]$ ls .ssh/

id_rsa  id_rsa.pub

id_rsa.pub為公鑰檔案,将該檔案中的内容,複制并粘貼到settings==&gt;SSH Public Keys中,并點選Add

<a href="http://s5.51cto.com/wyfs02/M00/7F/E4/wKioL1cxUgfDFUVqAAGq3q-4bWk295.png" target="_blank"></a>

測試SSH key是否送出成功,在client 172.16.206.129伺服器上執行以下指令:

#ssh -p 29418  [email protected]

29418為gerrit上SSH伺服器監聽的端口,user2為登入的gerrit賬号,注意這裡我們使用哪個賬号登入gerrit就使用哪個賬号測試,并且該賬号已經登入了gerrit伺服器,而且公鑰已經上傳。172.16.206.133為gerrit伺服器的IP

<code>[user2@host2 .</code><code>ssh</code><code>]$ </code><code>ssh</code> <code>-p 29418 [email protected]</code>

<code>The authenticity of host </code><code>'[172.16.206.133]:29418 ([172.16.206.133]:29418)'</code> <code>can't be established.</code>

<code>RSA key fingerprint is 93:d3:47:c2:15:32:f0:41:d9:e6:7f:88:66:01:f3:9b.</code>

<code>Are you sure you want to </code><code>continue</code> <code>connecting (</code><code>yes</code><code>/no</code><code>)? </code><code>yes</code>

<code>Warning: Permanently added </code><code>'[172.16.206.133]:29418'</code> <code>(RSA) to the list of known hosts.</code>

<code>  </code><code>****    Welcome to Gerrit Code Review    ****</code>

<code>  </code><code>Hi user2, you have successfully connected over SSH.</code>

<code>  </code><code>Unfortunately, interactive shells are disabled.</code>

<code>  </code><code>To clone a hosted Git repository, use:</code>

<code>  </code><code>git clone </code><code>ssh</code><code>:</code><code>//user2</code><code>@172.16.206.133:29418</code><code>/REPOSITORY_NAME</code><code>.git</code>

<code>Connection to 172.16.206.133 closed.</code>

可以看到連接配接成功,說明SSH Public Key上傳成功。

為user2注冊郵箱

<a href="http://s1.51cto.com/wyfs02/M00/7F/E7/wKiom1cxU7GAHRFDAAGEtbEIY_0461.png" target="_blank"></a>

點選Register後,登入你剛才注冊的郵箱,激活即可。

4、建立使用者組并建立一個項目

注意,隻有管理者組的使用者才能建立group和Project

登入Gerrit管理者賬号admin,建立一個新的組為group2。并将user2賬号加入該組

<a href="http://s5.51cto.com/wyfs02/M02/7F/E4/wKioL1cxVyeQLKvEAAC6XxXWCvc918.png" target="_blank"></a>

<a href="http://s4.51cto.com/wyfs02/M02/7F/E4/wKioL1cxVyiieK8VAAGaaMumo9Q911.png" target="_blank"></a>

建立一個Project,名為Project2

<a href="http://s4.51cto.com/wyfs02/M02/7F/E4/wKioL1cxWsKynVNDAAICK7gAolU605.png" target="_blank"></a>

<a href="http://s2.51cto.com/wyfs02/M00/7F/E4/wKioL1cxWsWjwjEwAAMaT5CjD8I516.png" target="_blank"></a>

5、gerrit代碼稽核流程

前面都是準備工作,接下來示範gerrit如何做代碼稽核的

a、client server( 172.16.206.129)的git初始配置

配置使用者名和郵箱

#git config --global user.name "user2"

#git config --global user.email "YOUR EMAIL-ADDRESS"

b、用賬戶user2登入gerrit 

在Projects==&gt; List中找到Projects2,然後點選Clone with commit-msg hook, 在點選ssh按鈕,

複制ssh下面項目位址:

<code>git clone </code><code>ssh</code><code>:</code><code>//user2</code><code>@172.16.206.133:29418</code><code>/Project2</code> <code>&amp;&amp; </code><code>scp</code> <code>-p -P 29418 [email protected]:hooks</code><code>/commit-msg</code> <code>Project2/.git</code><code>/hooks/</code>

直接将剛才複制的指令,在client server的指令行運作

注意:步驟b一定選Clone with commit-msg hook下面的位址來clone,否則在用戶端使用git push

指令送出代碼時會報錯。如果不需要向gerrit伺服器送出代碼,那麼你隻需使用

c、建立一個檔案,并送出到gerrit伺服器端

<code>[user2@host2 Project2]$ vim </code><code>test</code><code>.sh </code>

<code>aaaaaaaaaaaaa</code>

<code>bbbbbbbbbbbbb</code>

<code>ccccccccccccc</code>

用git add 和git commit指令将檔案儲存到git倉庫中

<code>[user2@host2 Project2]$ git add </code><code>test</code><code>.sh </code>

<code>[user2@host2 Project2]$ git commit -m </code><code>"建立了test.sh檔案"</code>

<code>[master 16462e9] 建立了</code><code>test</code><code>.sh檔案</code>

<code> </code><code>1 files changed, 3 insertions(+), 0 deletions(-)</code>

<code> </code><code>create mode 100644 </code><code>test</code><code>.sh</code>

d、用git push指令将代碼從本地傳到gerrit伺服器對象項目下的master分支

<code>[user2@host2 Project2]$ git push origin master:refs</code><code>/for/master</code>

<code>Counting objects: 4, </code><code>done</code><code>.</code>

<code>Compressing objects: 100% (2</code><code>/2</code><code>), </code><code>done</code><code>.</code>

<code>Writing objects: 100% (3</code><code>/3</code><code>), 341 bytes, </code><code>done</code><code>.</code>

<code>Total 3 (delta 0), reused 0 (delta 0)</code>

<code>remote: Processing changes: new: 1, refs: 1, </code><code>done</code>    

<code>remote: </code>

<code>remote: New Changes:</code>

<code>remote:   http:</code><code>//172</code><code>.16.206.133:8081</code><code>/5</code> <code>建立了</code><code>test</code><code>.sh檔案</code>

<code>To </code><code>ssh</code><code>:</code><code>//admin</code><code>@172.16.206.133:29418</code><code>/Project2</code>

<code> </code><code>* [new branch]      master -&gt; refs</code><code>/for/master</code>

origin:遠端倉庫的名稱,用git remote 可以檢視所有的遠端倉庫名稱

<code>[user2@host2 Project2]$ git remote </code>

<code>origin</code>

<code>master:refs</code><code>/for/master</code> <code>表示将代碼從本地的master分支傳到遠端的master分支,固定格式就是這樣</code>

e、用user2賬号登入Gerrit Web UI,邀請管理者admin來做code review。

注意:之前忘記截圖了,是以下圖不是原圖,但是基本上都一樣,隻有Owner和Project不對而已。

Add:添加指定使用者來進行Code Review

Cherry Pick:切換分支,當執行push操作之後如果發現分支不對,需要切換,可以在這裡點選Cherry Pick按鈕進行分支切換操作。

Abandon:撤銷push操作,當push之後,如果發現代碼有問題,需要修改,可以點選這個按鈕,然後修改代碼,修改完成後,再次push,然後登陸Gerrit Web UI,會有一個Restore按鈕,這時候點選Restore,就可以重新申請Code Review。

e、看看Gerrit伺服器上是否有code review的資訊

登入gerrit管理者賬号admin,在ALL下面點選“Open”,看到“建立了test.sh”這條記錄。

這裡“建立了test.sh檔案”就是git commit -m "建立了test.sh檔案" 這條指令産生的

<a href="http://s3.51cto.com/wyfs02/M01/7F/E6/wKioL1cxd0KC9IBEAAJt4--8EoI491.png" target="_blank"></a>

點開這條記錄,看到以下資訊

<a href="http://s1.51cto.com/wyfs02/M02/7F/E6/wKioL1cxd0biNku7AARvdB2TlY4241.png" target="_blank"></a>

我們可以點開test.sh檔案看到裡面的内容

<a href="http://s3.51cto.com/wyfs02/M02/7F/E6/wKioL1cxeGuDJUuNAAEkJvYYuEs066.png" target="_blank"></a>

還可以看到送出者的資訊:

<a href="http://s5.51cto.com/wyfs02/M02/7F/E6/wKioL1cxeRrASYpkAAEtZeZW_es670.png" target="_blank"></a>

這裡顯示的使用者名和郵箱位址其實是在client server上初始化git時,用以下指令配置的

點選上上圖中的code review和replay進行代碼稽核

稽核後可以看到稽核的日志

<a href="http://s1.51cto.com/wyfs02/M02/7F/E7/wKioL1cxe1iyH_XTAAEcFP-mH0E976.png" target="_blank"></a>

以上就是gerrit代碼稽核的流程,gerrit倉庫是純粹的代碼倉庫,不存在work tree這個說法。是以在gerrit倉庫裡面執行git指令都會報錯:

<code>[root@localhost Project2.git]</code><code># git status</code>

<code>fatal: This operation must be run </code><code>in</code> <code>a work tree</code>

但是,如果用git clone将gerrit倉庫裡面的項目clone下來後,就自動變成了一個git倉庫。

PS:本人是運維新手,很多東西還沒有來得及深入學習了解,如果文章有錯誤的地方請見諒。

本文轉自 曾哥最愛 51CTO部落格,原文連結:http://blog.51cto.com/zengestudy/1771762,如需轉載請自行聯系原作者