2016年5月17日 星期二

gitlab 自動custom hook佈署到server上

gitlab和web server現在放在同一台機器上,想要本地測試完git push到機器上後自動佈署到web服務上
因為懶得搞另一套 jenkins,所以打算用hook做

方案
git本身的hook
gitlab的 custom hook   http://docs.gitlab.com/ee/hooks/custom_hooks.html
gitlab的 web hook   http://docs.gitlab.com/ee/web_hooks/web_hooks.html

這裡選用gitlab的 custom hook

步驟:
0. 切換到git使用者,因為gitlab文件的使用者權限都是git
# sudo su git
1. 假設root使用者的test專案需要hook
2. 找server上test.git庫的位置,我的放在 /var/opt/gitlab/git-data/repositories/root/test.git ,但也有可能放在 /home/git/repositories/<group>/<project>.git ,依你安裝的方式不同而不同
3. 進入該資料夾後新建 custom_hooks 資料夾
4. 進入 custom_hooks 資料夾,在這邊新增原git hook類型的檔案,ex. pre-receive。這邊新增 post-receive這個鉤子文件,當push完成後這個文件就會被調用
post-receive
#!/bin/sh
git --work-tree=/var/www/test.localhost/html --git-dir=/var/opt/gitlab/git-data/repositories/root/test.git checkout -f
--git-dir git庫所在位置
--work-tree 實際文件被存放的位置
5. 別忘了賦予 post-receive 可執行權限
$ chmod +x post-receive
6. /var/www/test.localhost/html 路徑也必須是git權限
# chown git:git -R /var/www/test.localhost/html

最後在本地端測試push後,機器上web服務也馬上更新了,
如果 /var/www/test.localhost/html 上面原本有內容的檔案不會清空,只會被覆蓋

參考資料:
http://sumyblog.me/2015/11/02/use-git-hooks-for-hexo-automatic-deployment/  使用git hooks进行hexo博客自动化部署


沒有留言:

張貼留言