Wednesday, March 31, 2021

安裝 Gerrit Missed Event Playback

引言
不少公司都會採用 Gerrit 管理代碼,並且結合 Jenkins 作持續交付之用。例如,在 Jenkins 與 Gerrit 之間,其實可以透過 Gerrit Trigger Plugin 互連:當每次有新的 Code Review Event,Jenkins 就可以對新的 Code Patch 進行編譯、測試、然後 Tag Verified。這樣,Jenkins 就可以用作檢查代碼的穩定性,以及協助開發者快速找出有問題的代碼。

不過,因為不同原因(例如關機維護),有時候 Jenkins 與 Gerrit 會需要斷開。當兩者斷開之後,若果 Gerrit 出現的新 events,Jenkins 就會出現 event miss。即使 Jenkins 重啟之後,所有 missed events 在預設的狀態下,並不會自動同步。是次實作,我們會透過在 Gerrit 上安裝 Events-Log Plugin,使 Jenkins Gerrit Trigger 能自動同步(並執行)所有 missed events。

---

Step-by-Step 步驟

A. 手動編譯 events-log 插件

1. 從 Google Source 下載 events-log 的 git:
git clone https://gerrit.googlesource.com/plugins/events-log

2. 從 git 中 checkout 最新的穩定版本 (for example 3.1):

cd events-log/
git checkout stable-3.1

3. 安裝編譯需要的 dependencies:

sudo apt install openjdk-11-jdk
sudo apt install npm
npm install -g @bazel/bazelisk

4. 編譯 events-log:

bazel build events-log

5. 查看 build 輸出:

ls bazel-bin/events-log.jar

B. 打開 Gerrit 的 Remote Plugin Admin

1. 開啟  <gerrit-install-path>/etc/gerrit.conf 並加上以下兩行:

[plugins]
        allowRemoteAdmin = true

2. 重新啟動 Gerrit:

sudo /etc/init.d/gerrit restart

3. 登入 Gerrit 的 Web UI 並查看 Plugin Manager 是否存在:

http://<your gerrit url>/plugins/plugin-manager/static/index.html
C. 用 SSH 安裝插件

1. 安裝 events-log 插件到 Gerrit:

cd bazel-bin/
ssh -p 29418 localhost gerrit plugin install -n events-log.jar events-log.jar

2. 重新啟動 Gerrit Server:

sudo /etc/init.d/gerrit restart

3. 開啟 Gerrit Web UI,查看 enabled plugins 中是否含有 events-log:

http://<your gerrit server url>/admin/plugins
D. 設置 Jenkins Gerrit Trigger

1. 開啟 Jenkins 上的 Gerrit Trigger Server Configuration:

Manage Jenkins -> Gerrit Trigger -> Add new server / Server Settings 

2. 按 "Advanced...",找到 "REST API" 一項,並填上以下資料:

[v] 剔選 "Use REST API"
Gerrit HTTP Username:  <your jenkins username in Gerrit>
Gerrit HTTP Password:  <your jenkins HTTP Credentials>
[v] 剔選 "Enable Code-Review"
[v] 剔選 "Enable Verified"

3. 儲存,並重開 Gerrit 與 Jenkins 的連線。

---

小結
由於 event-log 並沒有已經預先編譯好的 jar 執行檔,所以是次實作,我們自行編譯它,並開啟 Gerrit 的插件安裝工具,最後把它順利安裝到 Gerrit 中。安裝完成後,Jenkins 內的 Gerrit Trigger Plugin 將能自動同步 event history,以減少不必要的手動 trigger 或 event miss。

---

參考:

[1] - https://plugins.jenkins.io/gerrit-trigger/ 

[2] - https://docs.bazel.build/versions/master/install-bazelisk.html

[3] - https://gerrit-review.googlesource.com/Documentation/dev-build-plugins.html

[4] - https://gerrit-review.googlesource.com/Documentation/cmd-plugin-install.html