<xmp id="63nn9"><video id="63nn9"></video></xmp>

<xmp id="63nn9"></xmp>

<wbr id="63nn9"><ins id="63nn9"></ins></wbr>

<wbr id="63nn9"></wbr><video id="63nn9"><ins id="63nn9"><table id="63nn9"></table></ins></video>

gerrit部署使用

前言

gerrit是基于git的工具,code review功能是開發中很方便,此外它可以配合Jenkins進行自動化的編譯工作。今天給大家分享一下gerrit在ubuntu的安裝使用

作者:良知猶存

轉載授權以及圍觀:歡迎關注微信公眾號:羽林君

或者添加作者個人微信:become_me


Gerrit簡介

Gerrit 是建立在Git版本控制系統之上并且基于Web的一個免費開源的輕量級代碼審查工具。 作為開發者和Git之間的一層屏障,不允許直接將本地修改內容同步到遠程倉庫中。 與Jenkins集成后,可以在每次提交代碼后,人工審核代碼前,通過Jenkins任務自動運行單元測試、構建以及自動化測試,如果Jenkins任務失敗,會自動打回本次提交。

一般Git、Gerrit和Jenkins集成后的使用流程

  • 1.開發者提交代碼到Gerrit
  • 2.觸發對應的Jenkins任務,通過以后Verified加1
  • 3.人工審核,審核通過后code review加2,觸發對應的Jenkins任務
  • 4.通過以后確認本次提交,Gerrit執行與Git倉庫的代碼同步操作
  • 5.代碼進入Git倉庫

Gerrit 安裝

安裝需要內容,gerrit文件,環境有git、jdk環境、反向代理工具nginx(幫助登錄網頁)

本文在nginx安裝部分是最后解決問題時候,才發現反向代理工具的是必須的,最后才安裝。

先下載 gerrit文件

下載鏈接:https://www.gerritcodereview.com/#download,官網可達 也可以使用wget工具命令行下載:wget https://gerrit-releases.storage.googleapis.com/gerrit-3.1.3.war

官網安裝指導文檔:

  • https://gerrit-review.googlesource.com/Documentation/index.html

  • https://gerrit-documentation.storage.googleapis.com/Documentation/3.6.2/install.html

下載下來等待其他環境安裝好之后進行使用。

jdk環境

直接apt install進行安裝sudo apt install openjdk-11-jdk 安裝之后使用java -version命令查看是否安裝成功

git環境

gerrit需要git環境,直接安裝git sudo apt-get install git

gerrit啟動

進入到gerrit*.war文件所在目錄,執行以下命令進行交互式安裝 java -jar ~/Downloads/gerrit*.war init -d ~/Gerrit/

很多人是把gerrit放入/home/gerrit/目錄,但是無法創建,需要創建一個gerrit賬號進行訪問。

sudo adduser gerrit
sudo su gerrit

類似于如此操作就可以創建一個gerrit目錄

最后我選擇了本地目錄建立 Gerrit ,操作執行 java -jar gerrit*.war init -d ~/Gerrit/

注意: 1、gerrit可以將數據保存到mysql或其他的數據中,如果你需要使用mysql,則需要安裝gerrit之前安裝mysql,我這里直接使用默認的數據庫類型 H2。如果你需要使用其他類型數據庫,可以參考下面這個鏈接:

  • https://gerrit-documentation.storage.googleapis.com/Documentation/2.12.2/install.html

2、如果gerrit需要結合jenkins做CI持續構建的話,在交互式安裝過程中需要安裝Verified label,默認是不安裝的:Install Verified label [y/N]? y 這里選擇y,安裝Verified label。

啟動執行具體細節如下:(比較復雜,后期使用需要反復觀看里面配置以及解釋)

lyn@lyn:~/Downloads$ java -jar gerrit*.war init -d ~/Gerrit/
Using secure store: com.google.gerrit.server.securestore.DefaultSecureStore
[2023-03-11 21:25:46,286] [main] INFO  com.google.gerrit.server.config.GerritServerConfigProvider : No /home/lyn/Gerrit/etc/gerrit.config; assuming defaults

*** Gerrit Code Review 3.7.0
***

Create '/home/lyn/Gerrit'      [Y/n]? y

*** Git Repositories
***

Location of Git repositories   [git]:  git       #指定Git存儲庫,最好指定一個不存在的目錄,他會自動創建

*** JGit Configuration
***

Auto-configured "receive.autogc = false" to disable auto-gc after git-receive-pack.

*** Index
***

Type                           [lucene]:       #默認即可。

*** User Authentication
***

Authentication method          [openid/?]: HTTP       #認證方法輸入HTTP,我們要使用反向代理
Get username from custom HTTP header [y/N]?
SSO logout URL                 :
Enable signed push support     [y/N]?
Use case insensitive usernames [Y/n]?

*** Review Labels
***

Install Verified label         [y/N]?  y

*** Email Delivery
***

SMTP server hostname           [localhost]:       #輸入自動發送郵件的smtp服務器
SMTP server port               [(default)]:       #465/994時SSL協議端口后,25是非SSL協議端口號
SMTP encryption                [none/?]:       #如果上一步輸入的是465/994,此處輸入SSL,否則直接回車即可
SMTP username                  :       #此處是你定義自動發送郵件的郵箱地址
                                       #接著我們需要輸入2次咱們郵箱的授權碼,而非郵箱密碼!

*** Container Process
***

Run as                         [lyn]: gerrit
Java runtime                   [/usr/lib/jvm/java-11-openjdk-amd64]:
Copy gerrit-3.7.0.war to /home/lyn/Gerrit/bin/gerrit.war [Y/n]?
Copying gerrit-3.7.0.war to /home/lyn/Gerrit/bin/gerrit.war

*** SSH Daemon
***

Listen on address              [*]:       #指定SSH后臺服務的監聽地址
Listen on port                 [29418]:       #指定SSH后臺服務的端口號
Generating SSH host key ... rsa... ed25519... ecdsa 256... ecdsa 384... ecdsa 521... done

*** HTTP Daemon
***
Behind reverse proxy           [Y/n]? y        #使用發向代理
Proxy uses SSL (https://)      [Y/n]?       #不適用SSL
Subdirectory on proxy server   [/]:       #指定代理服務器的子目錄,默認為"/"路徑,默認即可
Listen on address              [*]:       #只當gerrit服務的監聽地址
Listen on port                 [8080]: 8088       #指定gerrit的服務端口 
Canonical URL                  [http://lyn:8080/]: http://lyn:8088       #指定標準連接


*** Cache
***


*** Plugins
***

Installing plugins.       #接下來就是詢問我們是否安裝插件,我們一路”y“ 即可~
Install plugin codemirror-editor version v3.7.0 [y/N]? y
Installed codemirror-editor v3.7.0
Install plugin commit-message-length-validator version v3.7.0 [y/N]? y
Installed commit-message-length-validator v3.7.0
Install plugin delete-project version v3.7.0 [y/N]? y
Installed delete-project v3.7.0
Install plugin download-commands version v3.7.0 [y/N]? y
Installed download-commands v3.7.0
Install plugin gitiles version v3.7.0 [y/N]? y
Installed gitiles v3.7.0
Install plugin hooks version v3.7.0 [y/N]? y
Installed hooks v3.7.0
Install plugin plugin-manager version v3.7.0 [y/N]? y
Installed plugin-manager v3.7.0
Install plugin replication version v3.7.0 [y/N]? y
Installed replication v3.7.0
Install plugin reviewnotes version v3.7.0 [y/N]? y
Installed reviewnotes v3.7.0
Install plugin singleusergroup version v3.7.0 [y/N]? y
Installed singleusergroup v3.7.0
Install plugin webhooks version v3.7.0 [y/N]? y
Installed webhooks v3.7.0
Initializing plugins.

============================================================================
Welcome to the Gerrit community

Find more information on the homepage: https://www.gerritcodereview.com
Discuss Gerrit on the mailing list: https://groups.google.com/g/repo-discuss
============================================================================
Initialized /home/lyn/Gerrit
Init complete, reindexing accounts,changes,groups,projects with: reindex --site-path /home/lyn/Gerrit --threads 1 --index accounts --index changes --index groups --index projects --disable-cache-statsReindexed 0 documents in accounts index in 0.0s (0.0/s)
Index accounts in version 12 is ready
Reindexing groups:      100% (2/2)
Reindexed 2 documents in groups index in 0.8s (2.5/s)
Index groups in version 9 is ready
Reindexing changes: Slicing projects: 100% (2/2), done
Reindexed 0 documents in changes index in 0.0s (0.0/s)
Index changes in version 79 is ready
Reindexing projects:    100% (2/2)
Reindexed 2 documents in projects index in 0.1s (14.6/s)
Index projects in version 5 is ready
Executing /home/lyn/Gerrit/bin/gerrit.sh start
Starting Gerrit Code Review: WARNING: Could not adjust Gerrit's process for the kernel's out-of-memory killer.
         This may be caused by /home/lyn/Gerrit/bin/gerrit.sh not being run as root.
         Consider changing the OOM score adjustment manually for Gerrit's PID=193375 with e.g.:
         echo '-1000' | sudo tee /proc/193375/oom_score_adj
OK

創建gerrit.password文件,并創建用戶

htpasswd -c /home/lyn/Gerrit/etc/gerrit.password admin      # 創建第一個用戶admin,同時會生成一個gerrit.password文件
 
htpasswd -m /home/lyn/Gerrit/etc/gerrit.password lyn     # 在gerrit.password增加用戶用 -m

執行上面的命令,會要求多次輸入密碼,改密碼就是對應用戶的登錄密碼。 htpasswd 是apache密碼生成工具,如果你的電腦沒有這個命令,可以直接安裝apache2sudo apt-get install apache2

運行gerrit

在之前安裝gerrit的目錄下執行腳本,啟動gerrit

# 啟動gerrit
$ ~/Gerrit/bin/gerrit.sh start
# 停止gerrit
$ ~/Gerrit/bin/gerrit.sh stop
# 重啟gerrit
$ ~/Gerrit/bin/gerrit.sh restart

成功之后使用8088端口進行訪問:

這個時候我沒有開啟反向代理,會提示如下問題:

Configuration Error
Check the HTTP server’s authentication settings.
The HTTP server did not provide the username in the Authorization header when it forwarded the request to Gerrit Code Review.

If the HTTP server is Apache HTTPd, check the proxy configuration includes an authorization directive with the proper location, ensuring it ends with ‘/’:

后面還遇到了反向代理設置后,502,403問題。 但都可以歸結為一個原因。

錯誤原因:gerrit.conf 配置錯誤

gerrit相關配置文件修改,gerrit目錄的gerrit.config和nginx的gerrit.conf修改解決。

原因是Gerrit有兩種工作方式,我們要采用 http 的工作方式,也就是代碼審核人員,可以直接通過Web頁面對提交的代碼進行評審以及后續的操作。

然后Gerrit要求不能直接請求其端口,必須要使用反向代理才能正確登錄。而我們知道Nginx的成功正在于其高效、輕量級以及 反向代理 ,雖然Apache也有反向代理的功能,但是如果你在安裝Apache時沒有開啟,后續的開啟過程要相對復雜一點,而Nginx就簡單的多了。

https://blog.csdn.net/zhoulr000/article/details/114173876

方法一:安裝ngnix sudo apt-get install nginx

設置nginx的gerrit反向代理文件 sudo vi /etc/nginx/conf.d/gerrit.conf

  • 配置nginx反向代理,文件內容如下
server {
    listen 81;
    server_name localhost;
    allow all;
    deny all;

    auth_basic "Welcome to Gerrit Code Review Site!";
    auth_basic_user_file /home/lyn/Gerrit/etc/gerrit.password;


    location / {
        proxy_pass http://127.0.0.1:8088;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
    }
}

需要注意的部分:

  • listen 81: nginx server 監聽的端口,盡量不要使用知名端口80,我這里面換成了81端口
  • /home/lyn/Gerrit/etc/gerrit.password: 這個是http認證使用的文件,后面需要配置 對應
  • gerrit 端口配置為8088

其中還修改了gerrit中etc目錄的gerrit.config文件,內容如下:

[gerrit]
        basePath = git
        canonicalWebUrl = http://lyn:8088/
        serverId = f0858671-052c-4ea0-9883-032a598c9aae
[container]
        javaOptions = "-Dflogger.backend_factory=com.google.common.flogger.backend.log4j.Log4jBackendFactory#getInstance"
        javaOptions = "-Dflogger.logging_context=com.google.gerrit.server.logging.LoggingContext#getInstance"
        user = lyn
        javaHome = /usr/lib/jvm/java-11-openjdk-amd64
[index]
        type = lucene
[auth]
        type = HTTP
[receive]
        enableSignedPush = false
[sendemail]
        smtpServer = localhost
[sshd]
        listenAddress = *:29418
[httpd]
        listenUrl = proxy-https://*:8088/
[cache]
        directory = cache

配置完成后重啟gerrit~/Gerrit/bin/gerrit.sh restart

配置完成后重啟ngnix服務

sudo /etc/init.d/nginx restart       # 重啟nginx
sudo /etc/init.d/nginx status        # 查看狀態
或者執行
systemctl restart nginx.service
systemctl status nginx.service

啟動之后可以使用工具查看一下自己配置的端口有沒有正常工作: sudo netstat -ltpn ps $(fuser 8088/tcp)這個工具可以直接查看有沒有占據這個端口的進程

方法二:安裝apache2

我們也可以讓Apache2作為反向代理服務器訪問Gerrit, 這個沒有驗證,所以沒有過多描述,大家也可以參考此文:https://www.jianshu.com/p/0808559e6e2b 進行apache2配置

登錄gerrit

輸入下面鏈接,就可以登錄到gerrit頁面。

http://192.168.33.128:81/
  • 192.168.33.128:是主機地址
  • 81: 是nginx監聽的地址,不是gerrit配置的8088(這就是反向代理的作用)。

輸入剛才htpasswd設置好的用戶名和密碼登錄: gerrit就可以看到了:

gerrit插件小知識

gerrit 使用過程中,當進行 commit時,必須要生成一個 Change-Id,否則,push到gerrit服務器時,會收到一個錯誤提醒,而生成Change-Id可以用hooks工具。hooks鉤子工具中commit-msg hook可以幫我們生成Change-Id,就是運行gerrit使用安裝的插件 插件安裝情況可以在gerrit管理員的界面看到

但是當我們想看插件里面的指導文檔時候會遇到打不開情況: 這個時候需要我們修改一下訪問方式:如下修改 http://192.168.33.128:81/plugins/hooks/Documentation/index.html

這樣修改就可以看到插件的指導網頁了 此外還有將Gerrit服務器設置開機自啟動,Jenkins配置等等,后面我們再描述

結語

這就是我在gerrit部署基本操作的使用分享。如果大家有更好的想法,也歡迎大家加我好友交流分享哈。


作者:良知猶存,白天努力工作,晚上原創公號號主。公眾號內容除了技術還有些人生感悟,一個認真輸出內容的職場老司機,也是一個技術之外豐富生活的人,攝影、音樂 and 籃球。關注我,與我一起同行。

                              ????????????????  END  ????????????????

推薦閱讀

【1】jetson nano開發使用的基礎詳細分享

【2】Linux開發coredump文件分析實戰分享

【3】CPU中的程序是怎么運行起來的 必讀

【4】cartographer環境建立以及建圖測試

【5】設計模式之簡單工廠模式、工廠模式、抽象工廠模式的對比

本公眾號全部原創干貨已整理成一個目錄,回復[ 資源 ]即可獲得。

posted @ 2023-03-12 22:30  良知猶存  閱讀(104)  評論(0編輯  收藏  舉報
人碰人摸人爱免费视频播放

<xmp id="63nn9"><video id="63nn9"></video></xmp>

<xmp id="63nn9"></xmp>

<wbr id="63nn9"><ins id="63nn9"></ins></wbr>

<wbr id="63nn9"></wbr><video id="63nn9"><ins id="63nn9"><table id="63nn9"></table></ins></video>