[TOC] #### 1. 將項目拉取到云服務(wù)器 --- **拉取指定分支到云服務(wù)器** ``` git clone -b 分支名 倉庫地址 ``` ``` git clone -b liang https://gitee.com/holyking/test.git ``` **配置永久記住密碼** ``` git config --global credential.helper store ``` #### 2. 配置 SSH 公共密鑰 --- **生成 SSH 密鑰, 一路回車** ``` ssh-keygen -t rsa ``` **查看 `SSH 公鑰`** ``` cat ~/.ssh/id_rsa.pub ``` ![](https://img.itqaq.com/art/content/bbac76659538edf5aab33ec7ec8f3986.png) **配置 gitee 倉庫的 `部署公鑰`** ![](https://img.itqaq.com/art/content/296f983c5f10cf6c849eae324c963904.png) #### 3. 寶塔安裝webhook --- ![](https://img.itqaq.com/art/content/e04dc0fc5338d9f5dba1bebec55ecab6.png) **腳本文件** ```shell #!/bin/bash echo "" #輸出當(dāng)前時間 date --date='0 days ago' "+%Y-%m-%d %H:%M:%S" echo "Start" #git分支名稱 branch="liang" #git項目路徑 gitPath="/www/wwwroot/ft.itqaq.com/test" #git 倉庫地址 gitHttp="https://gitee.com/holyking/test.git" #gitHttp="http://192.168.2.20/llh/$1.git" //多倉庫的時候 echo "Web站點路徑:$gitPath" #判斷項目路徑是否存在 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 "拉取最新的項目文件" #sudo git reset --hard origin/$branch git remote add origin $gitHttp git branch --set-upstream-to=origin/$branch $branch sudo git reset --hard origin/$branch sudo git pull $gitHttp 2>&1 echo "設(shè)置目錄權(quán)限" sudo chown -R www:www $gitPath echo "End" exit else echo "該項目路徑不存在" echo "新建項目目錄" 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 "拉取最新的項目文件" #sudo git reset --hard origin/$branch sudo git pull gitHttp 2>&1 echo "設(shè)置目錄權(quán)限" sudo chown -R www:www $gitPath echo "End" exit fi ``` **腳本文件中的項目目錄、git地址、git分支名替換成自己的** ``` # 遠程分支 branch="master" # 換成自己寶塔項目根目錄 gitPath="/www/wwwroot/test" # gitee 倉庫地址 gitHttp="https://gitee.com/holyking/test.git" ``` ![](https://img.itqaq.com/art/content/dbf1980378397d5c5ae6881ce1f22311.png) #### 4. 配置 gitee WebHook --- **復(fù)制寶塔的 WebHook 提供的URL和密鑰** ![](https://img.itqaq.com/art/content/b3f14785d1636050a586e190b2311a53.png) **在 gitee 倉庫的 WebHook 中添加 WebHook** ![](https://img.itqaq.com/art/content/fc09696bfe50d3f6a60afe8bfa18ffeb.png) #### 5. 測試同步 --- 碼云倉庫隨意改個文件保存一下, 生成新的提交記錄, 看云服務(wù)器上有沒有同步更新