学无先后,达者为师

网站首页 PHP其他 正文

宝塔WebHook插件实现本地Git提交到码云后远程自动更新同步到宝塔服务器中

更新时间: 2018-12-11 PHP其他

步骤:

1. 准备工作

宝塔自带 git,没有的话可以自行安装

yum install git 
已安装的可以查看git版本
git --version
git version 1.8.3.1

2.生成秘钥配置到码云

ssh-keygen -t rsa

查看秘钥

# cat ~/.ssh/id_rsa.pub

3.把秘钥复制到码云中


为了避免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会提示输入用户名密码
[root@iZ25mi9h7ayZ test]# git pull
Username for 'https://gitee.com': xxxx@xxxx.com
Password for 'https://xxxx@xxxx.com@gitee.com':输入正确密码(看不到输入内容)
//生成.git-credentials 隐藏文件
cat ~/.git-credentials
https://Username:Password@gitee.com

vi ~/.git-credentials
可加多个用户名密码

4.宝塔面板 安装  宝塔  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项目路径
#web那层是我后加的正常没web
gitPath="/www/wwwroot/web/$1"
#git 网址 你的码云网址 $1是系统接收变量 就是在webhook填你的项目名时传来的
gitHttp="https://gitee.com/zhaoxingyu/$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/master
sudo git pull
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/master
sudo git pull
echo "设置目录权限"
sudo chown -R www:www $gitPath
echo "End"
exit
fi

添加的时候先随便写点内容就可以,添加完去编辑把上面内容复制进去然后把路径改成你自己的 ,因为第一次添加的内容会被过滤,



http://ip:端口/hooks?access_key=密钥¶m=git项目名 

网站项目名需跟码云的项目名保持一致

上述地址添加到码云

 


栏目分类
最近更新