[TOC] #### 1. 将项目拉取到云服务器 --- **拉取指定分支到云服务器** ``` 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 "" #输出当前时间 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 "设置目录权限" 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 "设置目录权限" 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 --- **复制宝塔的 WebHook 提供的URL和密钥** ![](https://img.itqaq.com/art/content/b3f14785d1636050a586e190b2311a53.png) **在 gitee 仓库的 WebHook 中添加 WebHook** ![](https://img.itqaq.com/art/content/fc09696bfe50d3f6a60afe8bfa18ffeb.png) #### 5. 测试同步 --- 码云仓库随意改个文件保存一下, 生成新的提交记录, 看云服务器上有没有同步更新