GitLab on Ubuntu 12.04 LTS,在 Ubuntu 12.04 上安裝 GitLab

講到 git 就會想到 GitHubGitHub 從 2008 年面世至今已經成為世界最大的 code hosting site。GitHub 的 free plan 對於一般開發者來說沒有什麼特殊限制(除了單檔不超過 100MB、單一 repository 不超過 1G),允許開發者隨意開 public repository。但是對於企業或是少數有 private repository 需求的開發者來說,如果需要 private repository 就要付費(想開免費 private repository 又沒有 collaborative coding 需求的話可以去用 Bitbucket)。

以我個人使用 GitHub 的經驗來說,github 的 UI 及功能(包含相關的 api)設計得蠻完善的,唯一讓我覺得不滿意的是速度。對敝社來說(順便工商服務一下),GitHub 用得還算順手,但是就是對速度上不是很滿意,同時隨著公司規模及產品線越來越大,對於 private repository 的需求也越來越大。在這情況下,開始嘗試導入 GitLab CE 這個 GitHub-like 的 open source solution(下面簡稱 GitLab,GitLab 也有付費的版本 GitLab EE 及付費的技術支援)。

事實上,同樣的考量及嘗試我們在兩年前評估過,但是當時的 GitLab 還不夠成熟(GitLab 是 2011/09 時誕生的專案,可以參考這裡的介紹),安裝過程太複雜而且非常不 maintainable,所以決定暫緩。前陣子去日本參加 AsiaBSDConf 時遇到跑去後院享受細節的 gslin,花了一夜龜在飯店重新弄了一輪 GitLab,順了一下流程還算可以接受,而且跑了一個多月後看起來效果還不錯,寫篇文章簡單記錄一下。

早期的 GitLab 需要個別裝先裝妥 Git、Ruby on Rails、Redis、MySQL/PostgreSQL、Nginx 等環境,接著需要把 GitLab 各個組成原件分別拉回來編譯 & 安裝;而在最近幾個版本開始用 Omnibus 並且把所有相依套件都直接封裝成一個 .deb 的 package,這對於維護成本來說瞬間降低不少(當然,如果想要把部份的服務改接系統上的版本或是抽出去獨立用其他機器來跑也是沒問題)。

這邊假設已經裝好一台 Ubuntu 12.04…

安裝 GitLab:

sudo wget https://downloads-packages.s3.amazonaws.com/ubuntu-12.04/gitlab_6.8.2-omnibus-1_amd64.deb # 連結在 https://www.gitlab.com/downloads/
sudo dpkg -i gitlab_6.8.2-omnibus-1_amd64.deb
sudo apt-get install python-docutils

接著是設定,用 dpkg 裝出來的 GitLab 主要檔案都會塞在 /opt/gitlab 及 /var/opt/gitlab 下,設定檔則是採 template 的概念來處理。所以可以簡單的在 /etc/gitlab/gitlab.rb 去指定一些關鍵的設定後,剩下的交由 GitLab 用 template 去產生完整的設定,下面是簡單的 /etc/gitlab/gitlab.rb 設定範例。

如果是走 http 的 gitlab site:

# 你的 GitLab 網址
external_url 'http://gitlab.hychen.org'

# GitLab 備份檔路徑
gitlab_rails['backup_path'] = '/net/gitlab_backup'

# gitlab 要把 repository 塞哪,不能是 symbolic link
gitlab_rails['gitlab_shell_repos_path'] = '/net/gitlab_data/repos'

# gitlab 要把 hooks 塞哪,不能是 symbolic link
gitlab_rails['gitlab_shell_hooks_path'] = '/net/gitlab_data/hooks/'

# gitlab 的 support mail
gitlab_rails['gitlab_support_email'] = 'admin@hychen.org'

# 單一檔案最大值
gitlab_rails['git_max_size'] = 10485760

 

如果是走 https 的 gitlab site:

# 你的 GitLab 網址
external_url 'http://gitlab.hychen.org'

# GitLab 備份檔路徑
gitlab_rails['backup_path'] = '/net/gitlab_backup'

# gitlab 要把 repository 塞哪,不能是 symbolic link
gitlab_rails['gitlab_shell_repos_path'] = '/net/gitlab_data/repos'

# gitlab 要把 hooks 塞哪,不能是 symbolic link
gitlab_rails['gitlab_shell_hooks_path'] = '/net/gitlab_data/hooks/'

# gitlab 的 support mail
gitlab_rails['gitlab_support_email'] = 'admin@hychen.org'

# 單一檔案的最大值
gitlab_rails['git_max_size'] = 10485760

# nginx 要做 redirect
nginx['redirect'] = true

# 你的 ssl certificate
nginx['ssl_certificate'] = "/etc/gitlab/ssl/x509-gitlab.hychen.org/server.crt"

# 你的 ssl certificate key
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/x509-gitlab.hychen.org/server.key"

最後透過 gitlab-ctl 這指令去 regenerate 出新的 GitLab 設定檔:

sudo gitlab-ctl reconfigure

接著,就能透過瀏覽器去存取你的 GitLab 啦。

如果需要調整其他細節,可以參考 /opt/gitlab/embedded/cookbooks/gitlab/templates/default 下的檔案及 /opt/gitlab/embedded/cookbooks/gitlab/attributes/default.rb 的內容,把相對應的設定寫到 /etc/gitlab/gitlab.rb 後,再重新呼叫 gitlab-ctl reconfigure 來更新設定檔。(但是因為 GitLab 可以設的東西不少,如果你發現想調的設定在這兩個位置找不到,那就代表還沒實做完成…只能用下面的土砲法來調整設定)

或者也可以用土砲法,直接去改 /var/opt/gitlab/*/etc/* 下的東西後,呼叫 gitlab-ctl restart 來重跑服務。但是土砲法的缺點是,當有人去叫 gitlab-ctl reconfigure 時,所有沒有寫在 /etc/gitlab/gitlab.rb 得非預設設定都會被 rollback 回預設值(這在更新 GitLab 版本的時候就很苦了:~)。

 

如何重跑 GitLab 服務
假設要重跑 GitLab 相關的所有服務

sudo gitlab-ctl restart

當然,也可以重跑特定服務就好,例如只想單純重跑 nginx

sudo gitlab-ctl restart nginx

 

如何備份 GitLab 資料

sudo gitlab-rake gitlab:backup:create RAILS_ENV=production

會生出一個 ${timestamp}_gitlab_backup.tar,放在 /etc/gitlab/gitlab.rb 指定的 gitlab_rails[‘backup_path’] 路徑下,這邊是假設設成 /net/gitlab_backup

 

如何從備份還原 GitLab 資料
這邊是假設備份檔放在 /net/gitlab_backup,檔名是 ${timestamp}_gitlab_backup.tar

gitlab-rake gitlab:backup:restore RAILS_ENV=production gitlab:backup:restore BACKUP=/net/gitlab_backup/${timestamp}_gitlab_backup.tar

 

如何更新 GitLab 版本
假設要從舊版 GitLab Omnibus 封裝升到 GitLab 6.8.2(請注意,未來可能會有些版本需要額外的手動介入,更新前最好還是看一下官方文件並且先備份)

sudo wget https://downloads-packages.s3.amazonaws.com/ubuntu-12.04/gitlab_6.8.2-omnibus-1_amd64.deb
sudo gitlab-ctl stop unicorn
sudo gitlab-ctl stop sidekiq
sudo dpkg -i gitlab_6.8.2-omnibus-1_amd64.deb
sudo gitlab-ctl reconfigure
sudo gitlab-ctl start

 

結論

GitLab 後面可以搭配不少應用,例如用 web hook 去串 CI,或者是去接 HipChat…等等,GitLab 本身也同 GitHub 一樣有提供 API 可以串接。跑了近兩個月,中間歷經了 GitLab 好幾次版本更新及修正,可以看出 GitLab 近期越來越活躍,也看到在社群出價希望有志之士能協助開發 API 的 case(這價格…XD)。雖然現在 UI 還是醜了點,中間有幾個版本也遇到一些小問題需要手動去修,不過整體來說感覺還不錯,有興趣自己架 git hosting 的可以參考看看:p

分類: Cloud, Computer, FreeBSD, Linux, Software,標籤: , , , , 。這篇內容的永久連結

發表迴響