Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

第 167 章 Gitlab 項目管理

目錄

167.1. GitLab
167.1.1. Yum 安裝 GitLab
167.1.2. Docker 方式安裝 Gitlab
167.1.3. GitLab Runner
167.1.4. 用戶管理
167.1.5. 組管理
167.1.6. 項目管理
167.1.7. 綁定SSL證書
167.1.8. FAQ
167.1.8.1. gitolite 向 gitlab 遷移
167.1.8.2. 修改主機名
167.2. 創建用戶
167.3. 創建組與項目
167.4. 分支管理
167.5. Issue
167.5.1. Milestones 里程碑
167.5.2. Labels 標籤
167.6. 代碼審查
167.7. 合併
167.8. WebHook
167.9. CI / CD
167.9.1. GitLab Runner
167.9.1.1. Install GitLab Runner
167.9.1.2. Registering Runners
167.9.1.3. /etc/gitlab-runner/config.toml
167.9.2. 配置 CI / CD
167.9.3. Pipeline
167.9.3.1. cache
167.9.3.2. before_script
167.9.3.3. stages
167.9.3.4. services
167.9.4. Java
167.9.5. vue.js android

實施DEVOPS首先我們要有一個項目管理工具。

我建議使用 Gitlab,早年我傾向使用Trac,但Trac項目一直處于半死不活狀態,目前來看Trac 對於 Ticket管理強於Gitlab,但Gitlab發展的很快,我們可以看到最近的一次升級中Issue 加入了 Due date 選項。Gitlab已經有風投介入,企業化運作,良性發展,未來會超越Redmine等項目管理軟件,成為主流。所以我在工具篇採用Gitlab,BTW 我沒有使用 Redmine,我認為 Redmine 的發展方向更接近傳統項目管理思維。

軟件項目管管理,我需要那些功能,Ticket/Issue管理、里程碑管理、內容管理Wiki、版本管理、合併分支、代碼審查等等

關於Gitlib的安裝配置請參考 http://www.netkiller.cn/project/project/gitlab/index.html

167.1. GitLab

https://github.com/gitlabhq

GitLab是一個利用 Ruby on Rails 開發的開源應用程序,實現一個自託管的Git項目倉庫,可通過Web界面進行訪問公開的或者私人項目。

它擁有與Github類似的功能,能夠瀏覽原始碼,管理缺陷和註釋。可以管理團隊對倉庫的訪問,它非常易於瀏覽提交過的版本並提供一個檔案歷史庫。團隊成員可以利用內置的簡單聊天程序(Wall)進行交流。它還提供一個代碼片段收集功能可以輕鬆實現代碼復用,便于日後有需要的時候進行查找。

GitLab 5.0以前版本要求伺服器端採用 Gitolite 搭建,5.0版本以後不再使用 Gitolite ,採用自己開發的 gitlab-shell 來實現。如果你覺得安裝麻煩可以使用 GitLab Installers 一鍵安裝程序。

167.1.1. Yum 安裝 GitLab

yum localinstall -y https://downloads-packages.s3.amazonaws.com/centos-6.6/gitlab-ce-7.10.0~omnibus.2-1.x86_64.rpm

gitlab-ctl reconfigure

cp /etc/gitlab/gitlab.rb{,.original}
		

停止 GitLab 服務

# gitlab-ctl stop
ok: down: logrotate: 1s, normally up
ok: down: nginx: 0s, normally up
ok: down: postgresql: 0s, normally up
ok: down: redis: 0s, normally up
ok: down: sidekiq: 1s, normally up
ok: down: unicorn: 0s, normally up
		

啟動 GitLab 服務

# gitlab-ctl start
ok: run: logrotate: (pid 3908) 0s
ok: run: nginx: (pid 3911) 1s
ok: run: postgresql: (pid 3921) 0s
ok: run: redis: (pid 3929) 1s
ok: run: sidekiq: (pid 3933) 0s
ok: run: unicorn: (pid 3936) 1s
		

配置gitlab

# vim /etc/gitlab/gitlab.rb
external_url 'http://gitlab.example.com'		
		

SMTP配置

gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['gitlab_email_from'] = 'openunix@163.com'
gitlab_rails['gitlab_email_display_name'] = 'Neo'
gitlab_rails['gitlab_email_reply_to'] = 'noreply@example.com'

gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.163.com"
gitlab_rails['smtp_user_name'] = "openunix@163.com"
gitlab_rails['smtp_password'] = "password"
gitlab_rails['smtp_domain'] = "163.com"
gitlab_rails['smtp_authentication'] = "login"		
		

任何配置檔案變化都需要運行 # gitlab-ctl reconfigure

WEB管理員

# Username: root 
# Password: 5iveL!fe		
		

167.1.2. Docker 方式安裝 Gitlab

		
docker pull gitlab/gitlab-ce:latest		
		
		
		
docker run \
    -d \
    -p 443:443 \
    -p 80:80 \
    --name gitlab-ce \
    --restart unless-stopped \
    -v /opt/gitlab/etc:/etc/gitlab \
    -v /opt/gitlab/log:/var/log/gitlab \
    -v /opt/gitlab/data:/var/opt/gitlab \
    gitlab/gitlab-ce		
		
		

配置對外url,域名或者ip,公網能訪問即可

		
vim /mnt/gitlab/etc/gitlab.rb
添加一下配置:
external_url	'http://127.0.0.1' (你的域名或者ip地址)	
		
		

配置郵箱

		
vim /mnt/gitlab/etc/gitlab.rb
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "13721218@qq.com"    (替換成自己的QQ郵箱)
gitlab_rails['smtp_password'] = "xxxxx"
gitlab_rails['smtp_domain'] = "smtp.qq.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = '13721218@qq.com'  (替換成自己的QQ郵箱,且與smtp_user_name一致)		
		
		

重新啟動gitlab

		
docker restart gitlab-ce	
		
		

167.1.3. GitLab Runner

curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash
sudo yum install gitlab-ci-multi-runner
		

進入 CI 配置頁面 http://git.netkiller.cn/netkiller.cn/www.netkiller.cn/settings/ci_cd

Specific Runners 你將看到 CI 的URL和他的Token

Specify the following URL during the Runner setup: http://git.netkiller.cn/ci

Use the following registration token during setup: wRoz1Y_6CXpNh2JbxN_s

現在回到 GitLab Runner

# gitlab-ci-multi-runner register
Running in system-mode.                            
                                                   
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://git.netkiller.cn/ci
Please enter the gitlab-ci token for this runner:
wRoz1Y_6CXpNh2JbxN_s
Please enter the gitlab-ci description for this runner:
[iZ62yln3rjjZ]: gitlab-ci-1
Please enter the gitlab-ci tags for this runner (comma separated):
test
Whether to run untagged builds [true/false]:
[false]: 
Registering runner... succeeded                     runner=wRoz1Y_6
Please enter the executor: docker, docker-ssh, shell, ssh, virtualbox, docker+machine, docker-ssh+machine, kubernetes, parallels:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 
		

回到 Gitlab 頁你將看到 Pending 狀態變成 Running 狀態

升級 GitLab Runner

yum install gitlab-ci-multi-runner		
		

167.1.4. 用戶管理

初始化GitLab,進入Admin area,單擊左側菜單Users,在這裡為gitlab添加用戶

167.1.5. 組管理

初始化GitLab組,我比較喜歡使用“域名”作為組名,例如example.com

167.1.6. 項目管理

創建項目,我通常會在組下面創建項目,每個域名對應一個項目,例如www.example.com,images.example.com

版本庫URL如下

http: http://192.168.0.1/example.com/www.example.com.git
ssh: git@192.168.0.1:example.com/www.example.com.git
		

167.1.7. 綁定SSL證書

編輯 /etc/gitlab/gitlab.rb 檔案

		
external_url 'https://git.netkiller.cn'

nginx['enable'] = true
nginx['redirect_http_to_https'] = true
nginx['ssl_certificate'] = "/etc/gitlab/ssl/git.netkiller.cn.crt"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/git.netkiller.cn.key"
nginx['listen_https'] = true
nginx['http2_enabled'] = true
		
		

167.1.8. FAQ

167.1.8.1. gitolite 向 gitlab 遷移

早期gitlab使用gitolite為用戶提供SSH服務,新版gitlab有了更好的解決方案gitlab-shell。安裝新版本是必會涉及gitolite 向 gitlab 遷移,下面是我總結的一些遷移經驗。

第一步,將gitolite複製到gitlab倉庫目錄下

# cp -r /gitroot/gitolite/repositories/* /var/opt/gitlab/git-data/repositories/
			

執行導入處理程序

# gitlab-rake gitlab:import:repos
			

上面程序會處理一下目錄結構,例如

進入gitlab web界面,創建倉庫與導入的倉庫同名,這樣就完成了導入工作。

[提示]提示

轉換最好在git用戶下面操作,否則你需要運行

# chown git:git -R /var/opt/gitlab/git-data/repositories				
				
167.1.8.2. 修改主機名

預設Gitlab採用主機名,給我使用代理一定麻煩

git@hostname:example.com/www.example.com.git
http://hostname/example.com/www.example.com.git
			

我們希望使用IP地址替代主機名

git@172.16.0.1:example.com/www.example.com.git
http://172.16.0.1/example.com/www.example.com.git
			

編輯 /etc/gitlab/gitlab.rb 配置檔案

external_url 'http://172.16.0.1'
			

重新啟動Gitlab

# gitlab-ctl reconfigure
# gitlab-ctl restart