寶塔私有化部署git
準(zhǔn)備工作:在寶塔應(yīng)用商店中安裝webhook,
在命令行中輸入git --version看是否有安裝git,如果沒有,運(yùn)行下面命令安裝
yum install git
一、創(chuàng)建公鑰
cd ~/.ssh
ls
看是否有 id_rsa.pub 文件 如果沒有,用下面命令生成證書
ssh-keygen -t rsa -C "XXX@XXX.com"(XXX@XXX.com為你自己的git賬號)
(如果出現(xiàn)報(bào)錯:unknown key type rsa\302\240,換命令
ssh-keygen -t ed25519 -C "xx@xx.com"
)
然后一路回車
生成完之后查看證書
cd ~/.ssh
ls
cat id_rsa.pub
或
cat id_ed25519.pub
二、添加公鑰
在git上添加公鑰,我是企業(yè)版,在倉庫設(shè)置-部署公鑰管理

三、寶塔安裝webhook插件
進(jìn)入寶塔軟件商店,搜webhook 然后點(diǎn)安裝
安裝完之后進(jìn)入設(shè)置頁面


下面是腳本文件
#!/bin/bash
echo ""
#輸出當(dāng)前時間
date --date='0 days ago' "+%Y-%m-%d %H:%M:%S"
echo "Start"
#判斷寶塔WebHook參數(shù)是否存在
if [ ! -n "$1" ];
then
echo "param參數(shù)錯誤"
echo "End"
exit
fi
#git項(xiàng)目路徑
gitPath="/www/wwwroot/git/$1"
#git 網(wǎng)址
gitHttp="git@gitee.com:xxx/$1.git" //自己倉庫的鏈接
echo "Web站點(diǎn)路徑:$gitPath"
#判斷項(xiàng)目路徑是否存在
if [ -d "$gitPath" ]; then
cd $gitPath
#判斷是否存在git目錄
if [ ! -d ".git" ]; then
echo "在該目錄下克隆 git"
sudo git clone $gitHttp gittemp
sudo mv gittemp/.git .
sudo rm -rf gittemp
fi
echo "拉取最新的項(xiàng)目文件"
#sudo git reset --hard origin/master
sudo git pull
echo "設(shè)置目錄權(quán)限"
sudo chown -R www:www $gitPath
echo "End"
exit
else
echo "該項(xiàng)目路徑不存在"
echo "新建項(xiàng)目目錄"
mkdir $gitPath
cd $gitPath
#判斷是否存在git目錄
if [ ! -d ".git" ]; then
echo "在該目錄下克隆 git"
sudo git clone $gitHttp gittemp
sudo mv gittemp/.git .
sudo rm -rf gittemp
fi
echo "拉取最新的項(xiàng)目文件"
#sudo git reset --hard origin/master
sudo git pull
echo "設(shè)置目錄權(quán)限"
sudo chown -R www:www $gitPath
echo "End"
exit
fi
或
#!/bin/bash
echo ""
#輸出當(dāng)前時間
date --date='0 days ago' "+%Y-%m-%d %H:%M:%S"
echo "Start"
#判斷寶塔WebHook參數(shù)是否存在
if [ ! -n "$1" ];
then
echo "param參數(shù)錯誤"
echo "End"
exit
fi
#git項(xiàng)目路徑
gitPathList=("/www/wwwroot/admin/facemall-admin")
for gitPath in ${gitPathList[*]}
do
echo "----------Web站點(diǎn)路徑:$gitPath ----------"
#判斷項(xiàng)目路徑是否存在
if [ -d "$gitPath" ]; then
cd $gitPath
#判斷是否存在git目錄
if [ ! -d ".git" ]; then
echo "在該目錄下克隆 git"
sudo git clone $gitHttp gittemp
sudo mv gittemp/.git .
sudo rm -rf gittemp
fi
echo "拉取最新的項(xiàng)目文件"
#sudo git reset --hard origin/master
sudo git pull
echo "設(shè)置目錄權(quán)限"
sudo chown -R www:www $gitPath
# echo "End"
php artisan migrate
continue
else
echo "該項(xiàng)目路徑不存在"
echo "新建項(xiàng)目目錄"
mkdir $gitPath
cd $gitPath
#判斷是否存在git目錄
if [ ! -d ".git" ]; then
echo "在該目錄下克隆 git"
sudo git clone $gitHttp gittemp
sudo mv gittemp/.git .
sudo rm -rf gittemp
fi
echo "拉取最新的項(xiàng)目文件"
#sudo git reset --hard origin/master
sudo git pull
echo "設(shè)置目錄權(quán)限"
sudo chown -R www:www $gitPath
# echo "End"
php artisan migrate
continue
fi
echo "----------end:$gitPath ----------"
done
echo "End"
#git 網(wǎng)址
#gitHttp="git@gitee.com:zswq_xm/$1.git" //自己倉庫的鏈接
注意 你需要確認(rèn)你的項(xiàng)目路徑與倉庫目錄
$1是變量,通過GET獲取的,后面?zhèn)鲄鲄⒃僦v,如果你的站點(diǎn)名稱與倉庫名稱不一致,建議把站點(diǎn)改成固定的目錄
我的測試服務(wù)器站點(diǎn)目錄在/www/wwwroot/下面,你的不是就要改成你自己的
git網(wǎng)址xxx是你自己的git倉庫目錄,當(dāng)然,你也可以直接復(fù)制代碼倉庫的地址粘貼替換

(PS:部分用戶反饋,寶塔的添加那里的輸入框可能會過濾代碼,因此添加保存后務(wù)必點(diǎn)擊編輯按鈕查看代碼的完整度,你也可以在編輯狀態(tài)重新粘貼修改保存~)
四、配置gitee的webhook

點(diǎn)擊寶塔webhook的查看秘鑰按鈕,將密鑰填入gitee的密碼框中,將post路徑填入gitee的POST路徑框中



五、將Gitee倉庫clone到服務(wù)器的本地目錄中
進(jìn)入到你要放置的目錄中 然后git clone git@gitee.com:xxx/xxx.git(注意,這里要復(fù)制SSH地址clone不要用HTTPS)
然后試改個東西推送下
如果出現(xiàn)如下信息,則說明更新成功了

如果發(fā)現(xiàn)日志有增加,但是代碼沒有更新
去項(xiàng)目根目錄使用 git pull,使用 2 次手動拉代碼,如果2次都需要輸入密碼,就使用一下命令后重新拉取
git config --global credential.helper store
git pull
Enter passphrase for key ‘/root/.ssh/id_rsa’ : git push 重復(fù)輸入密碼的問題
這是因?yàn)槭褂胹sh協(xié)議時的公私鑰設(shè)置了密碼
刪除:
ssh-keygen -p
重新設(shè)置:
Enter file in which the key is (/Users/haoyangruanjian/.ssh/id_rsa):直接點(diǎn)回車, Enter new passphrase (empty for no passphrase):也是直接回車不設(shè)置密碼
————————————————
版權(quán)聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接和本聲明。
原文鏈接:https://blog.csdn.net/weixin_44052462/article/details/129727777

浙公網(wǎng)安備 33010602011771號