團(tuán)隊(duì)開發(fā)過程中,團(tuán)隊(duì)成員 Push 到 git 遠(yuǎn)程倉庫時(shí),遠(yuǎn)程倉庫會(huì)自動(dòng)監(jiān)聽 Push 命令并觸發(fā)服務(wù)器上的 WebHooks 腳本,實(shí)現(xiàn)線上服務(wù)器代碼自動(dòng) Pull 更新,官方參考文檔。
登錄開源中國碼云,https://gitee.com

碼云
進(jìn)入某個(gè)項(xiàng)目,點(diǎn)擊管理

進(jìn)入項(xiàng)目管理
新增部署公鑰
[root@iZbp17xenom6dgmv6v3sphZ ~]# ssh-keygen -t rsa -C "auto_pull"
[root@iZbp17xenom6dgmv6v3sphZ ~]# cd ~/.ssh/
[root@iZbp17xenom6dgmv6v3sphZ .ssh]# ll
total 12
-rw------- 1 root root 1675 Sep 8 14:32 id_rsa
-rw-r--r-- 1 root root 386 Sep 8 14:32 id_rsa.pub
-rw-r--r-- 1 root root 367 Sep 27 08:38 known_hosts
[root@iZbp17xenom6dgmv6v3sphZ .ssh]#
- 將id_rsa.pub公鑰里面的內(nèi)容拷貝到部署公鑰里面保存,官網(wǎng)參考文檔
添加部署公鑰 - 測試是否添加成功,看到
Welcome to Gitee.com, Anonymous!證明公鑰添加成功
[root@iZbp17xenom6dgmv6v3sphZ .ssh]# ssh -T git@gitee.com
The authenticity of host 'gitee.com (120.55.226.24)' can't be established.
ECDSA key fingerprint is 27:e5:d3:f7:2a:9e:eb:6c:93:cd:1f:c1:47:a3:54:b1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com' (ECDSA) to the list of known hosts.
Welcome to Gitee.com, Anonymous!
[root@iZbp17xenom6dgmv6v3sphZ .ssh]#
新增WebHooks,URL為線上服務(wù)器地址對(duì)應(yīng)的某個(gè)方法(如果使用MVC模式開發(fā)),密碼設(shè)置是為了防止惡意更新操作,可在線上服務(wù)器 Pull 時(shí)校驗(yàn)

WebHooks
線上服務(wù)器更新邏輯, /root/www/project是你的項(xiàng)目目錄,根據(jù)實(shí)際情況設(shè)定,返回上述步驟點(diǎn)擊【測試】,如果能返回更新信息說明配置成功
public function git()
{
/* 數(shù)據(jù)校驗(yàn)省略,
post過來的是json數(shù)據(jù),
一般只是驗(yàn)證密碼是否與之前后臺(tái)的一樣
*/
header('Content-type:text/html;charset=utf-8');
$output = shell_exec("cd /root/www/project; sudo -u root git pull 2<&1");
echo "<pre>$output</pre>";
}
注意:
執(zhí)行上述步驟前請(qǐng)確保線上服務(wù)器已經(jīng)安裝好了 git 環(huán)境,并且 git 已經(jīng)加入了環(huán)境變量,用作更新的用戶請(qǐng)確保此用戶的公鑰已經(jīng)添加到了Gitee部署公鑰 里面了,上述步驟沒有校驗(yàn)密碼,為了安全建議大家完善密碼校驗(yàn)部分。
