1、建站建站在防火墙开放端口访问默认网页2、为git添加部署密钥在1panel的服务器上创建公钥密钥文件# 一路回车默认保存在 ~/.ssh/id_rsa ssh-keygen -t rsa -C for_git_pull -f ~/.ssh/id_rsa_for_git_pull # 新增让容器信任该 host避免首次连接报错 # mkdir -p /root/.ssh chmod 700 /root/.ssh ssh-keyscan xxx.git /root/.ssh/known_hosts 2/dev/nullgit中创建部署密钥复制公钥文件 id_rsa_for_git_pull.pub 的内容到git中创建部署密钥3、写脚本获取站点资源目录点击文件夹进入网站目录在项目根目录创建code目录和update.shcode用于存放git拉取的代码update.sh用于写更新脚本如果目录所有权不是1000/1000的用户和组需要执行下面的命令修改文件夹所有权chown -R 1000:1000 /opt/1panel/www点击脚本名称编写脚本#!/bin/bash set -e SITE_DIR/opt/1panel/www/sites/lcx.test DEPLOY_DIR$SITE_DIR/index PROJECT_DIR$SITE_DIR/code DIST_DIR$PROJECT_DIR/dist echo 拉代码 安装依赖 docker run --rm \ -v /opt/1panel/www/sites/ruoyi-cloud-vue3/code:/work \ -v ~/.ssh/id_rsa_for_git_pull:/root/.ssh/id_rsa \ -v ~/.ssh/known_hosts:/root/.ssh/known_hosts \ -w /work \ --entrypointsh \ node:20 \ -c git config --global --add safe.directory /work if [ ! -d .git ]; then echo 未检测到 git 仓库执行 clone git clone -b base-frame gitxxx.git . else echo 已存在 git 仓库检查远程更新... git fetch origin base-frame LOCAL$(git rev-parse HEAD) REMOTE$(git rev-parse origin/base-frame) if [ $LOCAL $REMOTE ]; then echo ✅ 无代码更新跳过拉取和打包 exit 1 fi echo 检测到代码更新开始拉取 git pull origin base-frame fi echo 安装依赖 yarn --registryhttps://registry.npmmirror.com echo ️ 构建生产环境 yarn build:prod echo ✅ 构建完成 echo ✅ Vue 构建完成 echo dist 目录$DIST_DIR echo 备份旧 dist mv $DEPLOY_DIR $DEPLOY_DIR-$(date %Y%m%d-%H%M%S) echo 部署 dist cp -r $DIST_DIR $DEPLOY_DIR/ echo ✅ 站点更新完成$DEPLOY_DIR4、在计划任务下创建可执行的脚本库sh /opt/1panel/www/sites/lcx.test/update.sh5、执行脚本库 - 拉取代码并更新服务代码无更新时6、配置站点nginxserver { listen 31964 ; server_name lcx.test 192.168.20.108; index index.php index.html index.htm default.php default.htm default.html; access_log /www/sites/lcx.test/log/access.log main; error_log /www/sites/lcx.test/log/error.log; location ~ ^/(\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md) { return 404; } location ^~ /.well-known/acme-challenge { allow all; root /usr/share/nginx/html; } if ( $uri ~ ^/\.well-known/.*\.(php|jsp|py|js|css|lua|ts|go|zip|tar\.gz|rar|7z|sql|bak)$ ) { return 403; } # root /www/sites/lcx.test/index; location / { root /www/sites/lcx.test/index; # 核心配置处理路由模式为 history 时的404问题 try_files $uri $uri/ /index.html; index index.html index.htm; } # 后端接口配置 location /prod-api/{ proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://localhost:38080/; } error_page 404 /404.html; }配置解释try_files $uri $uri/ /index.html;的意思是首先尝试查找是否有$uri这个真实文件比如CSS、JS、图片。如果没找到再尝试查找是否有$uri/这个目录。如果还是没找到最后将请求重写到/index.html。这样Vue应用和Vue Router就能接管这个路径并正确渲染了。7、知识点set -e在 Shell 脚本中set -e是一个非常常用的指令它的核心作用是“遇到错误立刻退出”。具体表现默认情况下Shell 脚本在执行命令时如果某一条命令执行失败返回值非 0比如某个命令拼写错误、拉取代码失败等脚本会继续往下执行后面的代码。而加上set -e之后只要脚本中任何一条命令执行失败整个脚本就会立即停止运行不再执行后续的命令。exit 0 和 exit 1 的区别exit 1 和 exit 0都可以让git发现没有代码更新的时候退出 nodejs 的 docker 命令使用 exit 0 update.js 不会退出依然会执行后续的站点代码更新操作使用 exit 1 结合 set -e 会让 docker 后续的命令都不执行直接结束 update.jsupdate.js 的退出状态是1即异常退出使用 exit 1 结合 下面的代码也可以让 update.js 退出update.js 的退出状态是0即正常退出if [ $? -ne 0 ]; then echo 无代码更新后续操作全部跳过 exit 0 figit 的用户不一致问题fatal: detected dubious ownership in repository at /workTo add an exception for this directory, call:git config --global --add safe.directory /work这个报错是Git 的安全机制触发的意思是Git 发现/work目录的拥有者和当前执行 git 的用户不一致担心你可能在“伪装的仓库”里执行命令于是拒绝操作。一、为什么会出现这个问题/opt/1panel/www/sites/lcx.test 目录属于宿主机用户uid1000Docker 容器docker run --rm -v /opt/xxx:/work ...容器里是以rootuid0 身份执行git pullGit 检测到目录在磁盘上属于 A当前执行 git 的是 B 判定为dubious ownership可疑所有权于是抛出这个错误。二、解决方案按推荐程度✅ 在容器里加 safe.directory直接在 Docker 构建命令里解决不需要动宿主机。修改你现在的docker run命令在 bash 里先执行一次git config --global --add safe.directory /work✅优点不影响宿主机不改文件权限最安全、最干净三、为什么 Git 要搞这个这是Git 2.35 引入的安全机制防止攻击者诱导你在“别人控制的目录”里执行 git通过 hook 注入恶意代码所以不是 bug是特性 ✅
1Panel 脚本库 实现服务一键更新
1、建站建站在防火墙开放端口访问默认网页2、为git添加部署密钥在1panel的服务器上创建公钥密钥文件# 一路回车默认保存在 ~/.ssh/id_rsa ssh-keygen -t rsa -C for_git_pull -f ~/.ssh/id_rsa_for_git_pull # 新增让容器信任该 host避免首次连接报错 # mkdir -p /root/.ssh chmod 700 /root/.ssh ssh-keyscan xxx.git /root/.ssh/known_hosts 2/dev/nullgit中创建部署密钥复制公钥文件 id_rsa_for_git_pull.pub 的内容到git中创建部署密钥3、写脚本获取站点资源目录点击文件夹进入网站目录在项目根目录创建code目录和update.shcode用于存放git拉取的代码update.sh用于写更新脚本如果目录所有权不是1000/1000的用户和组需要执行下面的命令修改文件夹所有权chown -R 1000:1000 /opt/1panel/www点击脚本名称编写脚本#!/bin/bash set -e SITE_DIR/opt/1panel/www/sites/lcx.test DEPLOY_DIR$SITE_DIR/index PROJECT_DIR$SITE_DIR/code DIST_DIR$PROJECT_DIR/dist echo 拉代码 安装依赖 docker run --rm \ -v /opt/1panel/www/sites/ruoyi-cloud-vue3/code:/work \ -v ~/.ssh/id_rsa_for_git_pull:/root/.ssh/id_rsa \ -v ~/.ssh/known_hosts:/root/.ssh/known_hosts \ -w /work \ --entrypointsh \ node:20 \ -c git config --global --add safe.directory /work if [ ! -d .git ]; then echo 未检测到 git 仓库执行 clone git clone -b base-frame gitxxx.git . else echo 已存在 git 仓库检查远程更新... git fetch origin base-frame LOCAL$(git rev-parse HEAD) REMOTE$(git rev-parse origin/base-frame) if [ $LOCAL $REMOTE ]; then echo ✅ 无代码更新跳过拉取和打包 exit 1 fi echo 检测到代码更新开始拉取 git pull origin base-frame fi echo 安装依赖 yarn --registryhttps://registry.npmmirror.com echo ️ 构建生产环境 yarn build:prod echo ✅ 构建完成 echo ✅ Vue 构建完成 echo dist 目录$DIST_DIR echo 备份旧 dist mv $DEPLOY_DIR $DEPLOY_DIR-$(date %Y%m%d-%H%M%S) echo 部署 dist cp -r $DIST_DIR $DEPLOY_DIR/ echo ✅ 站点更新完成$DEPLOY_DIR4、在计划任务下创建可执行的脚本库sh /opt/1panel/www/sites/lcx.test/update.sh5、执行脚本库 - 拉取代码并更新服务代码无更新时6、配置站点nginxserver { listen 31964 ; server_name lcx.test 192.168.20.108; index index.php index.html index.htm default.php default.htm default.html; access_log /www/sites/lcx.test/log/access.log main; error_log /www/sites/lcx.test/log/error.log; location ~ ^/(\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md) { return 404; } location ^~ /.well-known/acme-challenge { allow all; root /usr/share/nginx/html; } if ( $uri ~ ^/\.well-known/.*\.(php|jsp|py|js|css|lua|ts|go|zip|tar\.gz|rar|7z|sql|bak)$ ) { return 403; } # root /www/sites/lcx.test/index; location / { root /www/sites/lcx.test/index; # 核心配置处理路由模式为 history 时的404问题 try_files $uri $uri/ /index.html; index index.html index.htm; } # 后端接口配置 location /prod-api/{ proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://localhost:38080/; } error_page 404 /404.html; }配置解释try_files $uri $uri/ /index.html;的意思是首先尝试查找是否有$uri这个真实文件比如CSS、JS、图片。如果没找到再尝试查找是否有$uri/这个目录。如果还是没找到最后将请求重写到/index.html。这样Vue应用和Vue Router就能接管这个路径并正确渲染了。7、知识点set -e在 Shell 脚本中set -e是一个非常常用的指令它的核心作用是“遇到错误立刻退出”。具体表现默认情况下Shell 脚本在执行命令时如果某一条命令执行失败返回值非 0比如某个命令拼写错误、拉取代码失败等脚本会继续往下执行后面的代码。而加上set -e之后只要脚本中任何一条命令执行失败整个脚本就会立即停止运行不再执行后续的命令。exit 0 和 exit 1 的区别exit 1 和 exit 0都可以让git发现没有代码更新的时候退出 nodejs 的 docker 命令使用 exit 0 update.js 不会退出依然会执行后续的站点代码更新操作使用 exit 1 结合 set -e 会让 docker 后续的命令都不执行直接结束 update.jsupdate.js 的退出状态是1即异常退出使用 exit 1 结合 下面的代码也可以让 update.js 退出update.js 的退出状态是0即正常退出if [ $? -ne 0 ]; then echo 无代码更新后续操作全部跳过 exit 0 figit 的用户不一致问题fatal: detected dubious ownership in repository at /workTo add an exception for this directory, call:git config --global --add safe.directory /work这个报错是Git 的安全机制触发的意思是Git 发现/work目录的拥有者和当前执行 git 的用户不一致担心你可能在“伪装的仓库”里执行命令于是拒绝操作。一、为什么会出现这个问题/opt/1panel/www/sites/lcx.test 目录属于宿主机用户uid1000Docker 容器docker run --rm -v /opt/xxx:/work ...容器里是以rootuid0 身份执行git pullGit 检测到目录在磁盘上属于 A当前执行 git 的是 B 判定为dubious ownership可疑所有权于是抛出这个错误。二、解决方案按推荐程度✅ 在容器里加 safe.directory直接在 Docker 构建命令里解决不需要动宿主机。修改你现在的docker run命令在 bash 里先执行一次git config --global --add safe.directory /work✅优点不影响宿主机不改文件权限最安全、最干净三、为什么 Git 要搞这个这是Git 2.35 引入的安全机制防止攻击者诱导你在“别人控制的目录”里执行 git通过 hook 注入恶意代码所以不是 bug是特性 ✅