寶塔利用git+ webhooks 實現git更新遠端同步阿里雲Linux伺服器
阿新 • • 發佈:2018-12-05
目的:希望web程式push到gitee上後,自動pull到阿里雲伺服器,實現本地與伺服器的資料同步更新
借鑑寶塔論壇的方法:https://www.bt.cn/bbs/thread-5348-1-1.html
伺服器環境:centos7.3,Nginx
專案目錄: /www/wwwroot/www (www變數,自定義)
遠端git版本庫: 碼雲
步驟:
1. 準備工作
寶塔自帶 git,沒有的話可以自行安裝
yum install git
已安裝的可以檢視git版本
git --version
git version 1.8.3.1
新增金鑰到碼雲
ssh-keygen -t rsa //檢視公鑰
把ssh-rsa全部字串新增到碼雲部署專案公鑰設定裡邊
為了避免git pull 輸入使用者名稱密碼,先把使用者名稱密碼加上
cd /www/wwwroot/ 下 //使用git克隆
git clone https://gitee.com/isu5cn/自己的專案名稱.git //碼雲裡有克隆連結,避免出現錯誤,使用https方式
git config --global user.name "使用者名稱"
git config --global user.email "郵箱"
git config --global credential.helper store
//會生成.gitconfig 的檔案,檢視
cat .gitconfig //報錯cat: .gitconfig : No such file or directory
cat ~/.gitconfig //顯示內容
[user]
name = 輸入的使用者名稱
email = 輸入的郵箱
[credential]
helper = store
//第一次pull會提示輸入使用者名稱密碼
[ [email protected] test]# git pull
Username for 'https://gitee.com': [email protected]
Password for 'https://[email protected]@gitee.com':輸入正確密碼(看不到輸入內容)
//生成.git-credentials 隱藏檔案
cat ~/.git-credentials
https://Username:[email protected]
vi ~/.git-credentials
可加多個使用者名稱密碼
2.寶塔面板 安裝 寶塔 webhook
執行指令碼程式碼
#!/bin/bash
echo ""
#輸出當前時間
date --date='0 days ago' "+%Y-%m-%d %H:%M:%S"
echo "Start"
#判斷寶塔WebHook引數是否存在
if [ ! -n "$1" ];
then
echo "param引數錯誤"
echo "End"
exit
fi
#git專案路徑
gitPath="/www/wwwroot/$1"
#git 網址
gitHttp="https://gitee.com/isu5cn/$1.git"
echo "Web站點路徑:$gitPath"
#判斷專案路徑是否存在
if [ -d "$gitPath" ]; then
cd $gitPath
#判斷是否存在git目錄
if [ ! -d ".git" ]; then
echo "在該目錄下克隆 git"
git clone $gitHttp gittemp
mv gittemp/.git .
rm -rf gittemp
fi
#拉取最新的專案檔案
git reset --hard origin/master
git pull
#設定目錄許可權
chown -R www:www $gitPath
echo "End"
exit
else
echo "該專案路徑不存在"
echo "End"
exit
fi
編輯框輸入的命令會被過濾,點選編輯重新新增shell命令進去儲存,需要使用命令重啟寶塔
/etc/init.d/bt restart
http://ip:埠/hooks?access_key=金鑰¶m=git專案名
網站專案名需跟碼雲的專案名保持一致
上述地址新增到碼雲
可同時推送多個伺服器,已方便使用負載均衡