php与Git下基于webhook的自动化部署
前言
2018年第一篇文章,沒啥技術(shù)含量,權(quán)當(dāng)筆記
我們一般都會(huì)用git或者svn來管理我們的代碼
每次代碼更新后還要手動(dòng)的去把服務(wù)器上的代碼也更新一遍
項(xiàng)目小了還好 項(xiàng)目大了著實(shí)浪費(fèi)時(shí)間
要是服務(wù)器上的代碼也能像git那樣增量更新就好了
今天就說說如何通過webhook的形式來讓服務(wù)器自動(dòng)拉取我們push的代碼
原理
現(xiàn)在的Git服務(wù)器一般都會(huì)有個(gè)webhook服務(wù)
什么意思呢?
就是我們?cè)趫?zhí)行了push、merge等一系列的操作的時(shí)候
Git服務(wù)器會(huì)發(fā)送一個(gè)請(qǐng)求到我們指定的URL
并且會(huì)把此次動(dòng)作的相關(guān)數(shù)據(jù)也發(fā)送過去
例
這里我們使用開源中國的碼云演示
在幫助文檔中可以看到
當(dāng)我們發(fā)生push之類的操作的時(shí)候
Git服務(wù)器會(huì)像我們指定的url發(fā)送以下數(shù)據(jù)
{"before": "fb32ef5812dc132ece716a05c50c7531c6dc1b4d", "after": "ac63b9ba95191a1bf79d60bc262851a66c12cda1", "ref": "refs/heads/master", "user_id": 13,"user_name": "123", "user": {"name": "123","username": "test123","url": "https://gitee.com/oschina"}, "repository": {"name": "webhook", "url": "http://git.oschina.net/oschina/webhook", "description": "", "homepage": "https://gitee.com/oschina/webhook"}, "commits": [{"id": "ac63b9ba95191a1bf79d60bc262851a66c12cda1", "message": "1234 bug fix", "timestamp": "2016-12-09T17:28:02 08:00", "url": "https://gitee.com/oschina/webhook/commit/ac63b9ba95191a1bf79d60bc262851a66c12cda1", "author": {"name": "123", "email": "123@123.com", "time": "2016-12-09T17:28:02 08:00"}}], "total_commits_count": 1, "commits_more_than_ten": false, "project": {"name": "webhook", "path": "webhook", "url": "https://gitee.com/oschina/webhook", "git_ssh_url": "git@gitee.com:oschina/webhook.git", "git_http_url": "https://gitee.com/oschina/webhook.git", "git_svn_url": "svn://gitee.com/oschina/webhook", "namespace": "oschina", "name_with_namespace": "oschina/webhook", "path_with_namespace": "oschina/webhook", "default_branch": "master"}, "hook_name": "push_hooks", "password": "pwd" }于是乎,我們就可以拿這些數(shù)據(jù)來做做文章了
準(zhǔn)備
- 一個(gè)git倉庫
- 安裝了web服務(wù)器與git支持的服務(wù)器
步驟
服務(wù)器篇
1.首先我們需要為www用戶創(chuàng)建一個(gè)ssh密鑰
切換到www用戶下并生成一個(gè)rsa密鑰
su - www ssh-keygen -t rsa密鑰一般生成在 ==/var/www/.ssh/== 這個(gè)路徑下
文件名為 id_rsa
網(wǎng)站的建立這里不再敷述
文件內(nèi)容如下
<?php //git webhook 自動(dòng)部署腳本 //項(xiàng)目存放物理路徑 $path = "你的項(xiàng)目部署路徑"; $requestBody = file_get_contents("php://input"); if (empty($requestBody)) {die('send fail'); } $content = json_decode($requestBody, true); //若是主分支且提交數(shù)大于0 if ($content['ref']=='refs/heads/master' && $content['total_commits_count']>0) {$res = shell_exec("cd {$path} && git pull 2>&1");//以www用戶運(yùn)行$res_log = '-------------------------'.PHP_EOL;$res_log .= $content['user_name'] . ' 在' . date('Y-m-d H:i:s') . '向' . $content['repository']['name'] . '項(xiàng)目的' . $content['ref'] . '分支push了' . $content['total_commits_count'] . '個(gè)commit:' . PHP_EOL;$res_log .= $res.PHP_EOL;file_put_contents("git-webhook.txt", $res_log, FILE_APPEND);//追加寫入 } echo '很棒:'.date('y-m-d H:i:s');以上代碼相信都可以看懂
Git發(fā)送過來的數(shù)據(jù)相當(dāng)豐富
我們可以用這些數(shù)據(jù)來做些過濾來決定是否需要更新服務(wù)器上的本地代碼
代碼庫篇
創(chuàng)建方法這里不在敷述
在項(xiàng)目管理中把上面步驟第一步中得到的ssh密鑰添加到倉庫的部署密鑰中
這樣我們的web服務(wù)器就有了拉取代碼的權(quán)限
3.添加hook路徑
同樣在項(xiàng)目管理中添加webhook鏈接
這里可以添加一個(gè)密碼,我偷懶這里沒加
需要加的話可以在hook文件中添加一個(gè)驗(yàn)證
可以防止被惡意訪問
聯(lián)合
最后我們需要在我們的部署目錄先把git初始化一次
su - www git clone git倉庫地址 項(xiàng)目部署地址然后我們往git倉庫中提交一次代碼更新
稍等一下
如果一切正常的話我們的項(xiàng)目目錄就會(huì)自動(dòng)拉取你剛才提交的代碼了
在hook路徑中也有l(wèi)og記錄
不需要的話可以把代碼注釋掉
轉(zhuǎn)載于:https://www.cnblogs.com/ixysy/p/8529589.html
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的php与Git下基于webhook的自动化部署的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 乌克兰会拖欧盟下水吗?
- 下一篇: php中的parse_ini_file函