[ DevOps ] 安裝 GitLab

GitLab 是一個版本管理工具並利用 Ruby on Rails 開發的開源專案,可透過 GitLab 的 Web 介面來瀏覽原始碼、Issue 追蹤公開或私有專案。


GitLab 安裝過程中使用到下方元件

  1. Packages / Dependencies
  2. Ruby
  3. Go
  4. System Users
  5. Database
  6. Redis
  7. GitLab
  8. Nginx

前置準備作業

  1. Ubuntu 14.04 以上的版本

安裝 GitLab

Packages / Dependencies
  • Step 1. 確認系統為最新狀態

請使用 root 來執行

apt-get update -y

apt-get install sudo -y  
  • Step 2. 安裝所需套件
sudo apt-get install -y build-essential zlib1g-dev libyaml-dev libssl-dev libgdbm-dev libreadline-dev libncurses5-dev libffi-dev curl openssh-server  checkinstall libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev logrotate python-docutils pkg-config cmake nodejs  
  • Step 3. 移除舊版的 Git
sudo apt-get remove git-core  
  • Step 4. 安裝 Git 相依套件
sudo apt-get install -y libcurl4-openssl-dev libexpat1-dev gettext libz-dev libssl-dev build-essential
  • Step 5. 下載與編譯 Git
cd /tmp

curl --silent --remote-name https://www.kernel.org/pub/software/scm/git/git-2.8.4.tar.gz

tar -xzf git-2.8.4.tar.gz

cd git-2.8.4

./configure

make prefix=/usr/local all  
  • Step 6. 安裝 Git 到 /usr/local/bin
sudo make prefix=/usr/local install  
  • Step 7. 清除 Git 壓縮檔
cd /tmp

sudo rm -rf git-2.8.4*  

Ruby
  • Step 1. 移除舊版 Ruby
sudo apt-get remove ruby1.8  
  • Step 2. 下載與編譯 Ruby
mkdir /tmp/ruby  &&  cd /tmp/ruby

curl --silent --remote-name https://cache.ruby-lang.org/pub/ruby/ruby-2.3.3.tar.gz

tar zxvf ruby-2.3.3.tar.gz

cd ruby-2.3.3

./configure --disable-install-rdoc

make

sudo make install  
  • Step 3. 安裝 Bundler Gem
sudo gem install bundler --no-ri --no-rdoc  

Go
  • Step 1. 移除 Go 的安裝目錄
sudo rm -rf /usr/local/go

curl --silent --remote-name https://storage.googleapis.com/golang/go1.5.3.linux-amd64.tar.gz

sudo tar -C /usr/local -xzf go1.5.3.linux-amd64.tar.gz

sudo ln -sf /usr/local/go/bin/{go,godoc,gofmt} /usr/local/bin/

rm go1.5.3.linux-amd64.tar.gz  

System Users
  • Step 1. 建立 GitLab 使用者
sudo adduser --disabled-login --gecos 'GitLab' git  

Database
  • Step 1. 安裝資料庫套件
sudo apt-get install -y postgresql postgresql-client libpq-dev postgresql-contrib  
  • Step 2. 建立 GitLab 使用者
sudo -u postgres psql -d template1 -c "CREATE USER git CREATEDB;"  
  • Step 3. 建立 pg_trgm (required for GitLab 8.6+)
sudo -u postgres psql -d template1 -c "CREATE EXTENSION IF NOT EXISTS pg_trgm;"  
  • Step 4. 設定權限
sudo -u postgres psql -d template1 -c "CREATE DATABASE gitlabhq_production OWNER git;"  

Redis
  • Step 1. 安裝 Redis
sudo apt-get install -y redis-server  

GitLab
  • Step 1. 安裝 GitLab 到 git 目錄
cd /home/git  
  • Step 2. Clone GitLab repository
sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 8-14-stable gitlab  
  • Step 3. 進入到 GitLab 安裝目錄
cd /home/git/gitlab  
  • Step 4. 複製 GitLab 設定檔
sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml  
  • Step 5. 更新 GitLab config
sudo -u git -H sed -i -e 's/bin_path:.*/bin_path: \/usr\/local\/bin\/git/g'  config/gitlab.yml  
  • Step 6. 複製 secrets 檔案
sudo -u git -H cp config/secrets.yml.example config/secrets.yml

sudo -u git -H chmod 0600 config/secrets.yml  
  • Step 7. 更改 log 與 tmp 目錄權限
sudo chown -R git log/

sudo chown -R git tmp/

sudo chmod -R u+rwX,go-w log/

sudo chmod -R u+rwX tmp/  
  • Step 8. 更改 tmp/pids/ 與 tmp/sockets 權限
sudo chmod -R u+rwX tmp/pids/

sudo chmod -R u+rwX tmp/sockets/  
  • Step 9. 建立 public/uploads/ 目錄
sudo -u git -H mkdir public/uploads/  
  • Step 10. 更改 public/uploads/ 目錄權限
sudo chmod 0700 public/uploads  
  • Step 11. 改變 builds 目錄權限
sudo chmod -R u+rwX builds/  
  • Step 12. 改變 shared/artifacts/ 目錄權限
sudo chmod -R u+rwX shared/artifacts/  
  • Step 13. 複製 Unicorn 設定檔
sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb  
nproc  
  • Step 14. 複製 Rack attack 設定檔
sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb  
  • Step 15. 設定 Git 使用者
sudo -u git -H git config --global core.autocrlf input  
  • Step 16. Disable 'git gc --auto'
sudo -u git -H git config --global gc.auto 0  
  • Step 17. Enable packfile bitmaps
sudo -u git -H git config --global repack.writeBitmaps true  
  • Step 18. Redis 連接設定
sudo -u git -H cp config/resque.yml.example config/resque.yml  
  • Step 19. 改變 Redis socket
sudo -u git -H sed -i -e 's/localhost:6379/0.0.0.0:6379/g'  config/resque.yml

sudo -u git -H sed -i -e 's/  url:.*/  url: redis:\/\/0.0.0.0:6379/g'  config/resque.yml  
  • Step 20. GitLab DB 設定
sudo -u git cp config/database.yml.postgresql config/database.yml  
  • Step 21. 更改 config/database.yml 權限
sudo -u git -H chmod o-rwx config/database.yml  
  • Step 22. 安裝 Gems
sudo -u git -H bundle install --deployment --without development test mysql aws kerberos  
  • Step 23. 安裝 GitLab Shell
sudo -u git -H bundle exec rake gitlab:shell:install REDIS_URL=redis://127.0.0.1:6379 RAILS_ENV=production SKIP_STORAGE_VALIDATION=true  
  • Step 24. GitLab API
sudo -u git -H sed -i -e 's/^gitlab_url:.*/gitlab_url: http:\/\/localhost:8080\//g'  /home/git/gitlab-shell/config.yml  
  • Step 25. 安裝 gitlab-workhorse
cd /home/git

sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-workhorse.git gitlab-workhorse

cd /home/git/gitlab-workhorse

sudo make install  
  • Step 26. 設定 Administrator/root 密碼
cd /home/git/gitlab

echo "yes" | sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production GITLAB_ROOT_PASSWORD=password  
  • Step 27. 安裝初始化 Script
sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab  
LC_ALL=en_US.UTF-8  sudo update-rc.d gitlab defaults 21  
  • Step 28. 設定 Logrotate
sudo cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab  
  • Step 29. 確認應用程式狀態
sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production  
  • Step 30. 編譯 Assets
sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production  
  • Step 31. 變更 repo 權限
sudo chmod -R ug+rwX,o-rwx  /home/git/repositories/

sudo chmod -R ug-s          /home/git/repositories/

sudo find /home/git/repositories/ -type d -print0 | sudo xargs -0 chmod g+s  
  • Step 32. 啟動 GitLab
sudo service gitlab start  
Nginx
  • Step 1. 安裝 Nginx
sudo apt-get install -y nginx  
  • Step 2. 設定 Configuration
sudo cp lib/support/nginx/gitlab /etc/nginx/sites-available/gitlab

sudo ln -s /etc/nginx/sites-available/gitlab /etc/nginx/sites-enabled/gitlab

sudo rm -f /etc/nginx/sites-available/default  
  • Step 3. 重啟 Nginx
sudo service nginx restart  
  • Step 4. 確認應用程式狀態
cd /home/git/gitlab  
./bin/check

GitLab 安裝完成畫面

開啟瀏覽器並輸入 GitLab URL

http://localhost:8080  


Reference
List of blogs

Archer

Having being a full stack engineer. Experience with C, Python, Objective-C, Swift, Node.js, Lua, Linkit Smart 7688, Raspberry Pi, ARM mbed, Arduino, IoT solutions. Contact us : [email protected]

ALL RIGHTS RESERVED. COPYRIGHT © 2016. Designed and Coded by Makee.io