2018-9-26 14:00:39 星期三
場景:
由於某種情況, 不能使用Jenkins, so......只有自己實現了
看圖: webUI
設計方案,
- 文件夾A, 用來存放git分支
- 文件夾B, 運行網站的代碼目錄
- 文件夾C, 部署用的腳本目錄, C里邊的腳本用來把A里邊的代碼更新同步到B里邊
- 入口1 gitlab, 添加push hook, hook中指定的url可以執行C中的代碼
- 入口2 Linux命令行, 執行C中的腳本進行部署
- 入口3 網頁, 在頁面中點擊按鈕進行部署
注意點:
1. 腳本由一個入口腳本和多個附屬腳本組成, 附屬腳本用來部署不同的項目
2. 附屬腳本由很多共同點, 比如拉取最新代碼, 找到差異, 覆蓋到目標目錄, 刪除緩存等, 不同的是各種路徑信息. 因此將這些共同的功能抽出來做一個類, 附屬腳本傳遞不同的參數去實現部署
3. 腳本入口有三種: 一種是命令行, 一種是接收gitlab push hook, 還有就是網頁交互
4. gitlab發送的是一串json, PHP接收的時候用 file_get_contents('php://input', 'r'); 內容如下(注意 object_kind, ref, repository->url, 他三個可以確認是哪個項目的哪個分支被push了代碼)

1 { 2 "object_kind":"push", 3 "before":"615.....ab2", 4 "after":"c2c.....c8f", 5 "ref":"refs/heads/develop", 6 "checkout_sha":"c2c.....c8f", 7 "message":null, 8 "user_id":44, 9 "user_name":"zhangzhibin", 10 "user_email":"zhangzhibin@......com", 11 "project_id":120, 12 "repository":{ 13 "name":"test", 14 "url":"git@...../test.git", 15 "description":"test", 16 "homepage":"http://...../test", 17 "git_http_url":"http://...../test.git", 18 "git_ssh_url":"git@...../test.git", 19 "visibility_level":0 20 }, 21 "commits":[ 22 { 23 "id":"c2c6e32d76b7539b8f981ff9830eb2e611c2dc8f", 24 "message":"測試hook", 25 "timestamp":"2018-10-08T16:49:03+08:00", 26 "url":"http://........./test/commit/c2c.....c8f", 27 "author":{ 28 "name":"zhangzhibin", 29 "email":"zhangzhibin@......com" 30 } 31 } 32 ], 33 "total_commits_count":1 34 }
5. git diff 的時候會把中文路徑重新編碼, 可以設置一下 sudo git config --global core.quotepath false
項目地址: 碼雲 Summer-PHP-Deploy