宝塔Webhook: 轻松实现自动化部署

本文介绍的是使用宝塔Webhook实现自动化部署Springboot和vite项目。相比于jenkins,使用宝塔Webhook占用的内存空间小,在服务器资源紧张的情况下非常适合使用

1.安装宝塔 Webhook

2.私有仓库创建密钥

然后如果是私有项目的话就去终端输入cd ~/.ssh && ls查看密钥

如果没有的话就输入ssh-keygen -t rsa -C "[email protected]"来生成密钥,会让你输入三个东西,没有必要的话直接回车留空即可

然后输入 cat id_rsa.pub来查看证书内容,复制他,一会要去git中配置

接着去git仓库上(我这里用的是gitee)把ssh链接保存

3.添加 hook

去宝塔上点击配置、新增,把下面这个脚本配置进去,注意要把你的git项目和git链接更换成你自己的

这个脚本是部署springboot项目的,需要提前在服务器安装jdk,maven,git。 如果部署的是前端项目,只需要修改脚本,打包前端项目到nginx配置的资源存放的路径即可

bash 复制代码
 #!/bin/bash
     echo ""
 ​
     # 环境变量
     export JAVA_HOME="/www/server/java/jdk1.8.0_371"
     PATH=$PATH:$MVN/bin
     export $PATH
 ​
     export MVN="/opt/maven/bin"
     PATH=$PATH:$MVN
     export $PATH
 ​
 ​
     # 设置 MAVEN_HOME 环境变量
     export MAVEN_HOME="/opt/maven/bin"
 ​
     # 将 Maven 的 bin 目录添加到 PATH 环境变量中
     export PATH=$MAVEN_HOME:$PATH
 ​
 ​
     # 输出当前时间
     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/BackEnd_dev1_cd"
     # git 网址, 这里设置里git仓库的地址
     gitHttp="[email protected]:xxxxx/xxxxxxxbackend.git"
 ​
     echo "Web站点路径:$gitPath"
 ​
     # 判断项目路径是否存在
     if [ -d "$gitPath" ]; then
             cd $gitPath
             # 判断是否存在git目录
             if [ ! -d "xxxxxxxbackend" ]; then
                     echo "在该目录下克隆 git"
                     git clone -b deploy $gitHttp 
             fi
 ​
         # 这里也需要修改成你项目代码的路径
             cd /www/wwwroot/BackEnd_dev1_cd/xxxxxxxbackend
             
              echo "" >> nohup.out
              echo "" >> nohup.out
 ​
 ​
              echo "配置安全目录开始" >> nohup.out
 ​
 ​
              git config --global --add safe.directory /www/wwwroot/BackEnd_dev1_cd/xxxxxxxbackend >> nohup.out
 ​
              chown -R $(whoami):$(whoami) /www/wwwroot/BackEnd_dev1_cd/xxxxxxxbackend >> nohup.out
 ​
 ​
              echo "配置安全目录结束" >> nohup.out
 ​
             # 切换到 deploy 分支, 这里需要在你的项目建立deploy分支
             git checkout deploy
             
             
             # 获取当前分支名
             currentBranch=$(git rev-parse --abbrev-ref HEAD)
             echo "当前分支:$currentBranch"
 ​
             if [ "$currentBranch" != "deploy" ]; then
                 echo "当前分支不是 deploy,跳过更新操作"
                 echo "End"
                 exit
             fi
 ​
             # 检查远程是否有更新
             echo "检查远程更新..."
             git fetch origin deploy
             localHash=$(git rev-parse HEAD)
             remoteHash=$(git rev-parse origin/deploy)
 ​
             echo "本地提交哈希: $localHash"
             echo "远程提交哈希: $remoteHash"
 ​
             if [ "$localHash" == "$remoteHash" ]; then
                 echo "代码已是最新,无需更新"
                 echo "End"
                 exit
             fi
 ​
             
             
             
 ​
             # 设置目录权限
             chown -R www:www $gitPath
 ​
             cd ./target
 ​
             ps -ef | grep xxxxxxxbackend-1.0.0-SNAPSHOT.jar | grep -v grep | awk '{print $2}' | xargs kill -9 >> nohup.out 2>&1
 ​
                 # 检查是否在 target 目录
             if [[ $(basename $PWD) == "target" ]]; then
                 echo "当前在 target 目录,返回上一级目录"
                 cd ../
             fi
             
             
             echo "" >> nohup.out
              echo "" >> nohup.out
 ​
 ​
              echo "配置安全目录开始" >> nohup.out
 ​
 ​
              git config --global --add safe.directory /www/wwwroot/BackEnd_dev1_cd/xxxxxxxbackend >> nohup.out
 ​
              chown -R $(whoami):$(whoami) /www/wwwroot/BackEnd_dev1_cd/xxxxxxxbackend >> nohup.out
 ​
 ​
              echo "配置安全目录结束" >> nohup.out
 ​
 ​
 ​
              echo "重置git仓库状态为远程仓库的deploy开始" >> nohup.out
 ​
              # 拉取最新的项目文件
              git reset --hard origin/deploy >> nohup.out 2>&1
 ​
              echo "重置git仓库状态为远程仓库的deploy结束" >> nohup.out
 ​
 ​
              date --date='0 days ago' "+%Y-%m-%d %H:%M:%S" >> nohup.out
              echo "开始拉取代码..." >> nohup.out
              echo "执行 git pull..." >> nohup.out
              git pull >> nohup.out 2>&1
 ​
 ​
 ​
             mvn clean >> nohup.out 2>&1
 ​
             mvn install >> nohup.out 2>&1
 ​
             cd ./target
 ​
             # 运行新的 Java 程序
             nohup /www/server/java/jdk1.8.0_371/bin/java -jar xxxxxxxbackend-1.0.0-SNAPSHOT.jar >> nohup.out 2>&1 &
    
             echo "End"
             exit
     else
             echo "该项目路径不存在"
             echo "End"
             exit
     fi

查看密钥链接 和密钥 并且保存,一会要用

4.git 平台配置

这里我用gitee的仓库来做示例

首先进入你的仓库,然后点击管理->公钥管理->添加公钥 把刚刚的公钥配置进去 ps:标题默认是你密钥的邮箱,可以自定义

添加完成后会有一个可部署公钥

添加完成后点击webhook配置链接

配置好后去你要存放的目录中用ssh来克隆项目测试

5.设置分支

新建 deploy 分支,将 master 分支的 rebase 到 deploy 分支,并打出 jar 包,放到 deploy 目录中,并 push 到远端

6.看日志

最后去看日志,就会有最近的一次提交

在 nohup.out 文件中有项目运行的日志

参考: 宝塔使用webhook自动部署前言 大家在部署项目的时候是不是还是传到git中后去服务器pull,其实git大多数是有自 - 掘金

这里有非常多的坑,下面一一列举

1.Webhook 的 shell 找不到 Linux 环境变量的指令

在 Webhook 的 shell 中不能直接使用 mvn install 或 java -jar 这种命令,因为找不到命令,需要在 shell 中自行导入,导入了还可能不成功,可能还需要使用全类名指令

bash 复制代码
 #!/bin/bash
     echo ""
 ​
     # 环境变量
     export JAVA_HOME="/www/server/java/jdk1.8.0_371"
     PATH=$PATH:$MVN/bin
     export $PATH
 ​
     export MVN="/opt/maven/bin"
     PATH=$PATH:$MVN
     export $PATH
 ​
 ​
     # 设置 MAVEN_HOME 环境变量
     export MAVEN_HOME="/opt/maven/bin"
 ​
     # 将 Maven 的 bin 目录添加到 PATH 环境变量中
     export PATH=$MAVEN_HOME:$PATH
 ​
 ​
     # 输出当前时间
     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/BackEnd_dev1_cd"
     # git 网址, 这里设置里git仓库的地址
     gitHttp="[email protected]:xxxxx/xxxxxxxbackend.git"
 ​
     echo "Web站点路径:$gitPath"
 ​
     # 判断项目路径是否存在
     if [ -d "$gitPath" ]; then
             cd $gitPath
             # 判断是否存在git目录
             if [ ! -d "xxxxxxxbackend" ]; then
                     echo "在该目录下克隆 git"
                     git clone -b deploy $gitHttp 
             fi
 ​
         # 这里也需要修改成你项目代码的路径
             cd /www/wwwroot/BackEnd_dev1_cd/xxxxxxxbackend
             
              echo "" >> nohup.out
              echo "" >> nohup.out
 ​
 ​
              echo "配置安全目录开始" >> nohup.out
 ​
 ​
              git config --global --add safe.directory /www/wwwroot/BackEnd_dev1_cd/xxxxxxxbackend >> nohup.out
 ​
              chown -R $(whoami):$(whoami) /www/wwwroot/BackEnd_dev1_cd/xxxxxxxbackend >> nohup.out
 ​
 ​
              echo "配置安全目录结束" >> nohup.out
 ​
             # 切换到 deploy 分支, 这里需要在你的项目建立deploy分支
             git checkout deploy
             
             
             # 获取当前分支名
             currentBranch=$(git rev-parse --abbrev-ref HEAD)
             echo "当前分支:$currentBranch"
 ​
             if [ "$currentBranch" != "deploy" ]; then
                 echo "当前分支不是 deploy,跳过更新操作"
                 echo "End"
                 exit
             fi
 ​
             # 检查远程是否有更新
             echo "检查远程更新..."
             git fetch origin deploy
             localHash=$(git rev-parse HEAD)
             remoteHash=$(git rev-parse origin/deploy)
 ​
             echo "本地提交哈希: $localHash"
             echo "远程提交哈希: $remoteHash"
 ​
             if [ "$localHash" == "$remoteHash" ]; then
                 echo "代码已是最新,无需更新"
                 echo "End"
                 exit
             fi
 ​
             
             
             
 ​
             # 设置目录权限
             chown -R www:www $gitPath
 ​
             cd ./target
 ​
             ps -ef | grep xxxxxxxbackend-1.0.0-SNAPSHOT.jar | grep -v grep | awk '{print $2}' | xargs kill -9 >> nohup.out 2>&1
 ​
                 # 检查是否在 target 目录
             if [[ $(basename $PWD) == "target" ]]; then
                 echo "当前在 target 目录,返回上一级目录"
                 cd ../
             fi
             
             
             echo "" >> nohup.out
              echo "" >> nohup.out
 ​
 ​
              echo "配置安全目录开始" >> nohup.out
 ​
 ​
              git config --global --add safe.directory /www/wwwroot/BackEnd_dev1_cd/xxxxxxxbackend >> nohup.out
 ​
              chown -R $(whoami):$(whoami) /www/wwwroot/BackEnd_dev1_cd/xxxxxxxbackend >> nohup.out
 ​
 ​
              echo "配置安全目录结束" >> nohup.out
 ​
 ​
 ​
              echo "重置git仓库状态为远程仓库的deploy开始" >> nohup.out
 ​
              # 拉取最新的项目文件
              git reset --hard origin/deploy >> nohup.out 2>&1
 ​
              echo "重置git仓库状态为远程仓库的deploy结束" >> nohup.out
 ​
 ​
              date --date='0 days ago' "+%Y-%m-%d %H:%M:%S" >> nohup.out
              echo "开始拉取代码..." >> nohup.out
              echo "执行 git pull..." >> nohup.out
              git pull >> nohup.out 2>&1
 ​
 ​
 ​
             mvn clean >> nohup.out 2>&1
 ​
             mvn install >> nohup.out 2>&1
 ​
             cd ./target
 ​
             # 运行新的 Java 程序
             nohup /www/server/java/jdk1.8.0_371/bin/java -jar xxxxxxxbackend-1.0.0-SNAPSHOT.jar >> nohup.out 2>&1 &
    
             echo "End"
             exit
     else
             echo "该项目路径不存在"
             echo "End"
             exit
     fi

上面这段代码的前面使用 export 导入了环境变量,导入了环境变量可能还不够,在最后还使用了 java 指令的全路径执行命令。

这里还需要特别注意的是JAVA_HOME 是不需要/bin 的,只需要 java 的安装目录,而加入全局环境变量的PATH 是需要加上/bin 的。

这里还要使用中终端使用 which java , which mvn 找到这些指令的位置。

参考:关于 Webhook 部分指令无法运作的解决方案

www.bt.cn/bbs/thread-...

2.git pull 拉取失败,需要设置安全目录

上面的代码中设置了 git config --global --add safe.directory /www/wwwroot/BackEnd_dev1_cd/Comprehensive.Data.Management.Platform.BackEnd >> nohup.out 用于设置 git 安全目录。有时这个还不够,还需要设置 chown -R $(whoami):$(whoami) /www/wwwroot/BackEnd_dev1_cd/Comprehensive.Data.Management.Platform.BackEnd >> nohup.out将当前目录设置给这个用户。

3.maven 不要使用 yum 安装,有 bug

使用 yum 安装的 maven 不全,无法执行命令,使用 wget 下载安装包手动安装。

参考: maven 安装

总结

要经常使用 echo "xxxxx" >> nohup.out 2>&1 ,mvn install >> nohup.out 2>&1 将输出结果输出到日志中,方便观察哪里发生错误。

>> 用于将命令的输出追加到指定的文件中 。

2>&1

  • 2> 是重定向标准错误输出(stderr)的方式。2 是指标准错误输出,> 是重定向操作符。
  • &1 的意思是将标准错误输出(2)重定向到标准输出(1)的位置。也就是说,把标准错误输出流合并到标准输出流。
  • 因此,2>&1 的作用是将标准错误输出重定向到标准输出,并且标准输出将会重定向到 nohup.out 文件中。
相关推荐
爱掉发的小李4 分钟前
使用python爬取网络资源
开发语言·后端·python·动态规划
霍珵璁9 分钟前
Elixir语言的测试开发
开发语言·后端·golang
网络风云35 分钟前
Flask(三)路由与视图函数
后端·python·flask
Asthenia04121 小时前
Java 线程的状态转换 / 操作系统线程状态转换 / 线程上下文切换详解 / 如何避免线程切换
后端
江沉晚呤时1 小时前
深入解析外观模式(Facade Pattern)及其应用 C#
java·数据库·windows·后端·microsoft·c#·.netcore
uhakadotcom1 小时前
云原生数据仓库对比:Snowflake、Databricks与阿里云MaxCompute
后端·面试·github
Asthenia04121 小时前
常用索引有哪些?联合索引使用时要注意什么?什么是最左匹配原则?联合索引(a, b, c),使用(b, c) 可以命中索引吗?(a, c) 呢?
后端
Asthenia04121 小时前
Redis性能与优势/对比其他Key-Value存储/数据类型及底层结构/相同数据结构原因/对比Memcached优势/字符串最大容量/RDB与AOF分析
后端
计算机-秋大田2 小时前
基于Spring Boot的个性化商铺系统的设计与实现(LW+源码+讲解)
java·vue.js·spring boot·后端·课程设计
熬了夜的程序员2 小时前
Go 语言封装邮件发送功能
开发语言·后端·golang·log4j