宝塔面板webhook与Gitee配置实现服务器的代码同步更新

2023-05-2408:15:00服务器及运维Comments1,716 views字数 1682阅读模式

一、安装git文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

1、文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

激活宝塔Python虚拟环境(参见文章宝塔面板安装Python和Flask(虚拟环境))文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

source activate /www/wwwroot/ydbjcn/python/23eeeb4347bdd26bfc6b7ee9a3b755dd_venv/

2、文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

安装git文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

conda install git

二、获取公钥文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

配置并获取公钥(参考文章id_rsa.pub的作用和获取id_rsa.pub)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

id_rsa.pub的作用:上班的第一天,上司大哥都会说:待会给我发个公钥吧。这里说的公钥其实就是id_rsa.pub。上司大哥是要把我们的客户端公钥上传到服务器,然后再把这个客户端公钥添加到authorized_keys。添加后,服务器就会认为你这个客户端为可信任。你则可以访问这个服务器了。在实际的工作中和大多数的互联网公司,都是会使用证书去登陆的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

.ssh文件一般存放在用户根目录下,例如在“/root/.ssh/id_rsa.pub”、“/home/user/.ssh/id_rsa.pub”文件夹中。
查看是否有公钥查看~/.ssh下的id_rsa.pub
若没有配置生成文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

ssh-keygen -t rsa

查看公钥,获取密钥文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

cd ~/.ssh
ls
cat id_rsa.pub

屏幕打印出一个长字符串,复制即可:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

宝塔面板webhook与Gitee配置实现服务器的代码同步更新

三、为gitee仓库添加公钥文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

宝塔面板webhook与Gitee配置实现服务器的代码同步更新

四、宝塔软件商城里安装使用webhook文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

1、点击设置,点击添加Hook。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

宝塔面板webhook与Gitee配置实现服务器的代码同步更新

名称随意,脚本输入以下内容(注意根据自己情况修改其中的gitPath和gitHttp):文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

#!/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/ydbjcn/yd/"
#git 网址
gitHttp="git@gitee.com/yangdongbjcn/yd.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 "拉取成功"
        echo "End"
        exit
else
        echo "该项目路径不存在"
        echo "End"
        exit
fi

2、保存之后,出校如下界面文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

宝塔面板webhook与Gitee配置实现服务器的代码同步更新
宝塔面板webhook与Gitee配置实现服务器的代码同步更新

查看WebHook密钥文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

五、配置Gitee的WebHooks文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

宝塔面板webhook与Gitee配置实现服务器的代码同步更新

配置Gitee的WebHooks文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

宝塔面板webhook与Gitee配置实现服务器的代码同步更新

添加WebHooks文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

六、将gitee代码clone到服务器本地目录文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

在服务器终端配置git文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

git config --global user.name "用户名"
git config --global user.email "邮箱"
git config --global credential.helper store //会生成.gitconfig 的文件
cat .gitconfig   //如果报错: No such file or directory,就用下一行的代码
cat ~/.gitconfig  //显示内容

进入合适的目录,例如上文中的/www/wwwroot/ydbjcn/,然后git clone https://gitee.com/XXX/XXX.git文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

第一次clone可能需要输入用户名和密码,需要注意哈。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

七、
当clone成功后,就已经是配置好了,可以在自己电脑上传文件到配置的仓库,检查下宝塔界面的文件里仓库目录下有没有自动更新刚上传的文件。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

也可以在gitee管理 webhooks手动触发测试。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

宝塔面板webhook与Gitee配置实现服务器的代码同步更新

WebHooks文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

然后在宝塔的webhook查看日志文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41747.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/41747.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定