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