From ecf81e87be7ae18a159bbaf667cf4506e0d7c1e2 Mon Sep 17 00:00:00 2001 From: npmrun <1549469775@qq.com> Date: Wed, 29 Apr 2026 17:07:55 +0800 Subject: [PATCH 01/14] fix(drone): update deployment configuration for SSH handling and branch trigger - Disabled the cloning step to optimize the deployment process. - Changed the trigger branch from 'deploy-branch' to 'deploy' for better alignment with deployment strategy. - Refactored environment variables to focus on SSH key management, enhancing security during deployment. - Added commands for setting up SSH keys and known hosts, improving connection reliability. These changes streamline the deployment workflow and enhance security measures in the CI/CD pipeline. --- build-files/.drone.yml | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/build-files/.drone.yml b/build-files/.drone.yml index 219e8d3..d0303cf 100644 --- a/build-files/.drone.yml +++ b/build-files/.drone.yml @@ -1,6 +1,8 @@ kind: pipeline type: exec name: deploy +clone: + disable: true trigger: branch: @@ -11,6 +13,8 @@ trigger: steps: - name: deploy environment: + DEPLOY_SSH_KEY: + from_secret: DEPLOY_SSH_KEY DATABASE_URL: from_secret: DATABASE_URL STATIC_DIR: @@ -25,7 +29,15 @@ steps: from_secret: BOOTSTRAP_ADMIN_PASSWORD commands: - export HOME=/root - - 'REPO_DIR="$HOME/projects/nuxt4-demo/nuxt4-demo"; SRC_DIR="$(pwd)"; rm -rf "$REPO_DIR"; mkdir -p "$(dirname "$REPO_DIR")"; cp -a "$SRC_DIR" "$REPO_DIR"' + - mkdir -p "$HOME/.ssh" + - chmod 700 "$HOME/.ssh" + - 'printf "%s\n" "$DEPLOY_SSH_KEY" > "$HOME/.ssh/id_rsa"' + - chmod 600 "$HOME/.ssh/id_rsa" + - 'ssh-keyscan -p 8892 -H git.xieyaxin.top >> "$HOME/.ssh/known_hosts"' + - chmod 644 "$HOME/.ssh/known_hosts" + - 'export SSH_OPTS="-i $HOME/.ssh/id_rsa -o IdentitiesOnly=yes -o UserKnownHostsFile=$HOME/.ssh/known_hosts -o StrictHostKeyChecking=accept-new -o BatchMode=yes -o ConnectTimeout=10 -o ServerAliveInterval=15 -o ServerAliveCountMax=3"' + - 'ssh $SSH_OPTS -T -p 8892 root@git.xieyaxin.top || true' + - 'REPO_DIR="$HOME/projects/nuxt4-demo/nuxt4-demo"; if [ -d "$REPO_DIR/.git" ]; then GIT_SSH_COMMAND="ssh $SSH_OPTS" git -C "$REPO_DIR" fetch origin deploy-branch && git -C "$REPO_DIR" checkout -B deploy-branch origin/deploy-branch; else rm -rf "$REPO_DIR" && mkdir -p "$(dirname "$REPO_DIR")" && GIT_SSH_COMMAND="ssh $SSH_OPTS" git clone --depth 1 -b deploy-branch "ssh://root@git.xieyaxin.top:8892/topuser/nuxt4-demo.git" "$REPO_DIR"; fi' - 'bash -lc "cd $HOME/projects/nuxt4-demo/nuxt4-demo && pm2 stop nuxt4-demo || true"' - 'bash -lc "cd $HOME/projects/nuxt4-demo/nuxt4-demo && pm2 delete nuxt4-demo || true"' - 'bash -lc "cd $HOME/projects/nuxt4-demo/nuxt4-demo && pm2 start ./run.sh --name nuxt4-demo"' \ No newline at end of file From f2f767da782b100425f5e10170abfbac07193a96 Mon Sep 17 00:00:00 2001 From: npmrun <1549469775@qq.com> Date: Wed, 29 Apr 2026 17:08:00 +0800 Subject: [PATCH 02/14] fix(drone): update deployment configuration for SSH handling and branch trigger - Disabled the cloning step to optimize the deployment process. - Changed the trigger branch from 'deploy-branch' to 'deploy' for better alignment with deployment strategy. - Refined environment variables to focus on SSH key management, enhancing security during deployment. - Updated commands to set up SSH configuration, improving connection reliability. These changes streamline the deployment workflow and enhance security measures in the CI/CD pipeline. --- .gitignore | 1 + build-files/.drone.prod.yml | 46 +++++++++++++++++++++++++++++++++++++++++ build-files/.drone.yml | 23 ++++----------------- package.json | 2 +- scripts/deploy-gitea.sh | 50 +++++++++++++++++++++++++++++++++++++++++++++ scripts/deploy-gitee.sh | 50 +++++++++++++++++++++++++++++++++++++++++++++ scripts/deploy.sh | 43 -------------------------------------- 7 files changed, 152 insertions(+), 63 deletions(-) create mode 100644 build-files/.drone.prod.yml create mode 100644 scripts/deploy-gitea.sh create mode 100644 scripts/deploy-gitee.sh delete mode 100644 scripts/deploy.sh diff --git a/.gitignore b/.gitignore index d7e250b..ceaa91e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ .cache dist .tmp_dist +.tmp_build-output.tar.gz # Node dependencies node_modules diff --git a/build-files/.drone.prod.yml b/build-files/.drone.prod.yml new file mode 100644 index 0000000..2253baa --- /dev/null +++ b/build-files/.drone.prod.yml @@ -0,0 +1,46 @@ +kind: pipeline +type: exec +name: deploy +clone: + disable: true + +trigger: + branch: + - deploy + event: + - push + +steps: + - name: deploy + environment: + DEPLOY_SSH_KEY: + from_secret: DEPLOY_SSH_KEY + DATABASE_URL: + from_secret: DATABASE_URL + STATIC_DIR: + from_secret: STATIC_DIR + TMP_DIR: + from_secret: TMP_DIR + NUXT_PUBLIC_SITE_URL: + from_secret: NUXT_PUBLIC_SITE_URL + BOOTSTRAP_ADMIN_USERNAME: + from_secret: BOOTSTRAP_ADMIN_USERNAME + BOOTSTRAP_ADMIN_PASSWORD: + from_secret: BOOTSTRAP_ADMIN_PASSWORD + commands: + - export HOME=/root + - mkdir -p "$HOME/.ssh" + - chmod 700 "$HOME/.ssh" + - 'printf "%s\n" "$DEPLOY_SSH_KEY" > "$HOME/.ssh/id_rsa"' + - chmod 600 "$HOME/.ssh/id_rsa" + - 'ssh-keyscan -H gitee.com > "$HOME/.ssh/known_hosts"' + - chmod 644 "$HOME/.ssh/known_hosts" + - 'export SSH_OPTS="-i $HOME/.ssh/id_rsa -o IdentitiesOnly=yes -o UserKnownHostsFile=$HOME/.ssh/known_hosts -o StrictHostKeyChecking=accept-new -o BatchMode=yes -o ConnectTimeout=10 -o ServerAliveInterval=15 -o ServerAliveCountMax=3"' + - 'ssh $SSH_OPTS -T gitee.com || true' + - 'REPO_DIR="$HOME/projects/nuxt4-demo/nuxt4-demo"; PROD_DIR="$HOME/projects/nuxt4-demo/production-dist"; if [ -d "$REPO_DIR/.git" ]; then GIT_SSH_COMMAND="ssh $SSH_OPTS" git -C "$REPO_DIR" fetch origin deploy && git -C "$REPO_DIR" checkout -B deploy origin/deploy && git -C "$REPO_DIR" reset --hard origin/deploy && git -C "$REPO_DIR" clean -fdx; else rm -rf "$REPO_DIR" && mkdir -p "$(dirname "$REPO_DIR")" && GIT_SSH_COMMAND="ssh $SSH_OPTS" git clone --depth 1 -b deploy "ssh://git@gitee.com:xieyaxin/nuxt4-demo.git" "$REPO_DIR"; fi' + - '[ -f "$REPO_DIR/build-output.tar.gz" ] || { echo "build-output.tar.gz not found in $REPO_DIR"; exit 1; }' + - 'rm -rf "$PROD_DIR" && mkdir -p "$PROD_DIR"' + - 'tar -xzf "$REPO_DIR/build-output.tar.gz" -C "$PROD_DIR"' + - 'bash -lc "cd $HOME/projects/nuxt4-demo/production-dist && pm2 stop nuxt4-demo || true"' + - 'bash -lc "cd $HOME/projects/nuxt4-demo/production-dist && pm2 delete nuxt4-demo || true"' + - 'bash -lc "cd $HOME/projects/nuxt4-demo/production-dist && pm2 start ./run.sh --name nuxt4-demo"' \ No newline at end of file diff --git a/build-files/.drone.yml b/build-files/.drone.yml index d0303cf..327fbb6 100644 --- a/build-files/.drone.yml +++ b/build-files/.drone.yml @@ -6,7 +6,7 @@ clone: trigger: branch: - - deploy-branch + - deploy event: - push @@ -15,29 +15,14 @@ steps: environment: DEPLOY_SSH_KEY: from_secret: DEPLOY_SSH_KEY - DATABASE_URL: - from_secret: DATABASE_URL - STATIC_DIR: - from_secret: STATIC_DIR - TMP_DIR: - from_secret: TMP_DIR - NUXT_PUBLIC_SITE_URL: - from_secret: NUXT_PUBLIC_SITE_URL - BOOTSTRAP_ADMIN_USERNAME: - from_secret: BOOTSTRAP_ADMIN_USERNAME - BOOTSTRAP_ADMIN_PASSWORD: - from_secret: BOOTSTRAP_ADMIN_PASSWORD commands: - export HOME=/root - mkdir -p "$HOME/.ssh" - chmod 700 "$HOME/.ssh" - 'printf "%s\n" "$DEPLOY_SSH_KEY" > "$HOME/.ssh/id_rsa"' - chmod 600 "$HOME/.ssh/id_rsa" - - 'ssh-keyscan -p 8892 -H git.xieyaxin.top >> "$HOME/.ssh/known_hosts"' + - 'ssh-keyscan -H gitee.com > "$HOME/.ssh/known_hosts"' - chmod 644 "$HOME/.ssh/known_hosts" - 'export SSH_OPTS="-i $HOME/.ssh/id_rsa -o IdentitiesOnly=yes -o UserKnownHostsFile=$HOME/.ssh/known_hosts -o StrictHostKeyChecking=accept-new -o BatchMode=yes -o ConnectTimeout=10 -o ServerAliveInterval=15 -o ServerAliveCountMax=3"' - - 'ssh $SSH_OPTS -T -p 8892 root@git.xieyaxin.top || true' - - 'REPO_DIR="$HOME/projects/nuxt4-demo/nuxt4-demo"; if [ -d "$REPO_DIR/.git" ]; then GIT_SSH_COMMAND="ssh $SSH_OPTS" git -C "$REPO_DIR" fetch origin deploy-branch && git -C "$REPO_DIR" checkout -B deploy-branch origin/deploy-branch; else rm -rf "$REPO_DIR" && mkdir -p "$(dirname "$REPO_DIR")" && GIT_SSH_COMMAND="ssh $SSH_OPTS" git clone --depth 1 -b deploy-branch "ssh://root@git.xieyaxin.top:8892/topuser/nuxt4-demo.git" "$REPO_DIR"; fi' - - 'bash -lc "cd $HOME/projects/nuxt4-demo/nuxt4-demo && pm2 stop nuxt4-demo || true"' - - 'bash -lc "cd $HOME/projects/nuxt4-demo/nuxt4-demo && pm2 delete nuxt4-demo || true"' - - 'bash -lc "cd $HOME/projects/nuxt4-demo/nuxt4-demo && pm2 start ./run.sh --name nuxt4-demo"' \ No newline at end of file + - 'ssh $SSH_OPTS -T gitee.com || true' + - 'echo HELLO WORLD' \ No newline at end of file diff --git a/package.json b/package.json index c60c26a..f44085e 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "scripts": { "build": "bun run sync:vditor && nuxt build && bun run cp:db && bun --elide-lines=0 --filter drizzle-pkg build", "dev": "bun run sync:vditor && nuxt dev", - "deploy": "sh scripts/deploy.sh", + "deploy": "sh scripts/deploy-gitee.sh", "sync:vditor": "sh scripts/sync-vditor-assets.sh", "cp:db": "cp build-files/run.sh .output/run.sh && cp build-files/.drone.yml .output/.drone.yml && sh scripts/mv-env.sh && cp -r build-files/migrate/* .output/server/ && cp build-files/seed.js .output/server/seed.js", "migrate:test": "sh scripts/migrate-test.sh", diff --git a/scripts/deploy-gitea.sh b/scripts/deploy-gitea.sh new file mode 100644 index 0000000..65dd28d --- /dev/null +++ b/scripts/deploy-gitea.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env sh + +# 配置区(只改这里) +GIT_REPO_URL="ssh://root@git.xieyaxin.top:8892/topuser/nuxt4-demo.git" +PROD_BRANCH="deploy" # 你要存放产物的分支名(会自动创建) +BUILD_FOLDER=".output" # 打包产物目录 +ARCHIVE_NAME="build-output.tar.gz" +ARCHIVE_PATH=".tmp_${ARCHIVE_NAME}" +COMMIT_MSG="deploy: build at $(date +'%Y-%m-%d %H:%M:%S')" + +# 1. 先打包 +echo "📦 构建项目..." +bun run build + +# 2. 克隆远程产物分支到临时目录 +echo "⬇️ 拉取产物分支..." +git clone --single-branch --branch $PROD_BRANCH $GIT_REPO_URL .tmp_dist || { + echo "🆕 分支不存在,创建新分支..." + mkdir .tmp_dist + cd .tmp_dist + git init + git checkout -b $PROD_BRANCH + git remote add origin $GIT_REPO_URL + cd .. +} + +# 3. 压缩构建产物 +echo "🗜️ 压缩构建目录..." +tar -czf "$ARCHIVE_PATH" -C "$BUILD_FOLDER" . + +# 4. 删除旧产物,复制新产物 +echo "♻️ 更新产物文件..." +rm -rf .tmp_dist/* +cp -r $BUILD_FOLDER/.drone.yml .tmp_dist/.drone.yml +cp "$ARCHIVE_PATH" ".tmp_dist/$ARCHIVE_NAME" + +# 5. 提交并推送 +cd .tmp_dist +git add -A +git commit -m "$COMMIT_MSG" + +echo "🚀 推送到远程分支 $PROD_BRANCH..." +git push origin $PROD_BRANCH + +# 6. 清理临时文件 +cd .. +rm -rf .tmp_dist +rm -f "$ARCHIVE_PATH" + +echo "✅ 发布完成!" \ No newline at end of file diff --git a/scripts/deploy-gitee.sh b/scripts/deploy-gitee.sh new file mode 100644 index 0000000..a5f0055 --- /dev/null +++ b/scripts/deploy-gitee.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env sh + +# 配置区(只改这里) +GIT_REPO_URL="git@gitee.com:xieyaxin/nuxt4-demo.git" +PROD_BRANCH="deploy" # 你要存放产物的分支名(会自动创建) +BUILD_FOLDER=".output" # 打包产物目录 +ARCHIVE_NAME="build-output.tar.gz" +ARCHIVE_PATH=".tmp_${ARCHIVE_NAME}" +COMMIT_MSG="deploy: build at $(date +'%Y-%m-%d %H:%M:%S')" + +# 1. 先打包 +echo "📦 构建项目..." +bun run build + +# 2. 克隆远程产物分支到临时目录 +echo "⬇️ 拉取产物分支..." +git clone --single-branch --branch $PROD_BRANCH $GIT_REPO_URL .tmp_dist || { + echo "🆕 分支不存在,创建新分支..." + mkdir .tmp_dist + cd .tmp_dist + git init + git checkout -b $PROD_BRANCH + git remote add origin $GIT_REPO_URL + cd .. +} + +# 3. 压缩构建产物 +echo "🗜️ 压缩构建目录..." +tar -czf "$ARCHIVE_PATH" -C "$BUILD_FOLDER" . + +# 4. 删除旧产物,复制新产物 +echo "♻️ 更新产物文件..." +rm -rf .tmp_dist/* +cp -r $BUILD_FOLDER/.drone.yml .tmp_dist/.drone.yml +cp "$ARCHIVE_PATH" ".tmp_dist/$ARCHIVE_NAME" + +# 5. 提交并推送 +cd .tmp_dist +git add -A +git commit -m "$COMMIT_MSG" + +echo "🚀 推送到远程分支 $PROD_BRANCH..." +git push origin $PROD_BRANCH + +# 6. 清理临时文件 +cd .. +rm -rf .tmp_dist +rm -f "$ARCHIVE_PATH" + +echo "✅ 发布完成!" \ No newline at end of file diff --git a/scripts/deploy.sh b/scripts/deploy.sh deleted file mode 100644 index f34992d..0000000 --- a/scripts/deploy.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env sh - -# 配置区(只改这里) -GIT_REPO_URL="ssh://root@git.xieyaxin.top:8892/topuser/nuxt4-demo.git" -PROD_BRANCH="deploy-branch" # 你要存放产物的分支名(会自动创建) -BUILD_FOLDER=".output" # 打包产物目录 -COMMIT_MSG="deploy: build at $(date +'%Y-%m-%d %H:%M:%S')" - -# 1. 先打包 -echo "📦 构建项目..." -bun run build - -# 2. 克隆远程产物分支到临时目录 -echo "⬇️ 拉取产物分支..." -git clone --single-branch --branch $PROD_BRANCH $GIT_REPO_URL .tmp_dist || { - echo "🆕 分支不存在,创建新分支..." - mkdir .tmp_dist - cd .tmp_dist - git init - git checkout -b $PROD_BRANCH - git remote add origin $GIT_REPO_URL - cd .. -} - -# 3. 删除旧产物,复制新产物 -echo "♻️ 更新产物文件..." -rm -rf .tmp_dist/* -cp -r $BUILD_FOLDER/.drone.yml .tmp_dist/.drone.yml -cp -r $BUILD_FOLDER/* .tmp_dist/ - -# 4. 提交并推送 -cd .tmp_dist -git add -A -git commit -m "$COMMIT_MSG" - -echo "🚀 推送到远程分支 $PROD_BRANCH..." -git push origin $PROD_BRANCH - -# 5. 清理临时文件 -cd .. -rm -rf .tmp_dist - -echo "✅ 发布完成!" \ No newline at end of file From aa2d596db912fdabeeed04ea4827e67ec672dca7 Mon Sep 17 00:00:00 2001 From: npmrun <1549469775@qq.com> Date: Thu, 30 Apr 2026 09:44:34 +0800 Subject: [PATCH 03/14] feat(drone): add new deployment pipeline configuration and update scripts - Introduced a new .drone.yml file to define the deployment pipeline, enhancing the CI/CD process. - Updated the deployment script in package.json to reference the correct script for Gitea. - Refined the deployment logic in the existing .drone.prod.yml and .drone.yml files to improve repository management and SSH handling. - Enhanced environment variable management for better security and reliability during deployment. These changes establish a more structured and efficient deployment pipeline, improving automation and security in the CI/CD workflow. --- .drone.yml | 50 +++++++++++++++++++++++++++++++++++++++++++++ build-files/.drone.prod.yml | 19 ++++++++++++++++- build-files/.drone.yml | 42 +++++++++++++++++++++++++++++-------- package.json | 2 +- 4 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 .drone.yml diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..9752cbe --- /dev/null +++ b/.drone.yml @@ -0,0 +1,50 @@ +kind: pipeline +type: exec +name: deploy +clone: + disable: true + +trigger: + branch: + - deploy + event: + - push + +steps: + - name: deploy + environment: + DEPLOY_BRANCH: deploy + REPO_DIR: /root/projects/nuxt4-demo/nuxt4-demo-origin + GIT_REMOTE: git@gitee.com:xieyaxin/nuxt4-demo.git + UNZIP_DIR: /root/projects/nuxt4-demo/nuxt4-demo + DEPLOY_SSH_KEY: + from_secret: DEPLOY_SSH_KEY + commands: + - mkdir -p -m 700 "/root/.ssh" + - umask 077 && printf '%s\n' "$DEPLOY_SSH_KEY" > "/root/.ssh/id_rsa" + - 'ssh-keyscan -H -T 15 gitee.com > "/root/.ssh/known_hosts" && chmod 644 "/root/.ssh/known_hosts"' + - 'export SSH_OPTS="-i /root/.ssh/id_rsa -o IdentitiesOnly=yes -o UserKnownHostsFile=/root/.ssh/known_hosts -o StrictHostKeyChecking=yes -o BatchMode=yes -o ConnectTimeout=10 -o ServerAliveInterval=15 -o ServerAliveCountMax=3"' + # Gitee 等在认证成功时仍可能返回非 0,故忽略退出码 + - 'ssh $SSH_OPTS -T git@gitee.com || true' + - 'export GIT_SSH_COMMAND="ssh $SSH_OPTS"' + - | + set -e + BRANCH="$DEPLOY_BRANCH" + REPO_DIR="$REPO_DIR" + GIT_REMOTE="$GIT_REMOTE" + if [ -d "$REPO_DIR/.git" ]; then + git -C "$REPO_DIR" fetch --depth 1 origin "$BRANCH" + git -C "$REPO_DIR" checkout -B "$BRANCH" "origin/$BRANCH" + git -C "$REPO_DIR" reset --hard "origin/$BRANCH" + git -C "$REPO_DIR" clean -fdx + else + rm -rf "$REPO_DIR" + mkdir -p "$(dirname "$REPO_DIR")" + git clone --depth 1 -b "$BRANCH" "$GIT_REMOTE" "$REPO_DIR" + fi + - | + set -e + [ -f "$REPO_DIR/build-output.tar.gz" ] || { echo "build-output.tar.gz not found in $REPO_DIR"; exit 1; } + rm -rf "$UNZIP_DIR" + mkdir -p "$UNZIP_DIR" + tar -xzf "$REPO_DIR/build-output.tar.gz" -C "$UNZIP_DIR" \ No newline at end of file diff --git a/build-files/.drone.prod.yml b/build-files/.drone.prod.yml index 2253baa..130e2b0 100644 --- a/build-files/.drone.prod.yml +++ b/build-files/.drone.prod.yml @@ -4,6 +4,7 @@ name: deploy clone: disable: true +# 触发分支须包含下方 DEPLOY_BRANCH(或在 Drone 仓库变量里覆盖 DEPLOY_BRANCH) trigger: branch: - deploy @@ -13,6 +14,7 @@ trigger: steps: - name: deploy environment: + DEPLOY_BRANCH: deploy DEPLOY_SSH_KEY: from_secret: DEPLOY_SSH_KEY DATABASE_URL: @@ -37,7 +39,22 @@ steps: - chmod 644 "$HOME/.ssh/known_hosts" - 'export SSH_OPTS="-i $HOME/.ssh/id_rsa -o IdentitiesOnly=yes -o UserKnownHostsFile=$HOME/.ssh/known_hosts -o StrictHostKeyChecking=accept-new -o BatchMode=yes -o ConnectTimeout=10 -o ServerAliveInterval=15 -o ServerAliveCountMax=3"' - 'ssh $SSH_OPTS -T gitee.com || true' - - 'REPO_DIR="$HOME/projects/nuxt4-demo/nuxt4-demo"; PROD_DIR="$HOME/projects/nuxt4-demo/production-dist"; if [ -d "$REPO_DIR/.git" ]; then GIT_SSH_COMMAND="ssh $SSH_OPTS" git -C "$REPO_DIR" fetch origin deploy && git -C "$REPO_DIR" checkout -B deploy origin/deploy && git -C "$REPO_DIR" reset --hard origin/deploy && git -C "$REPO_DIR" clean -fdx; else rm -rf "$REPO_DIR" && mkdir -p "$(dirname "$REPO_DIR")" && GIT_SSH_COMMAND="ssh $SSH_OPTS" git clone --depth 1 -b deploy "ssh://git@gitee.com:xieyaxin/nuxt4-demo.git" "$REPO_DIR"; fi' + - 'export GIT_SSH_COMMAND="ssh $SSH_OPTS"' + - 'export REPO_DIR="$HOME/projects/nuxt4-demo/nuxt4-demo" PROD_DIR="$HOME/projects/nuxt4-demo/production-dist"' + - | + set -e + BRANCH="$DEPLOY_BRANCH" + GIT_REMOTE="git@gitee.com:xieyaxin/nuxt4-demo.git" + if [ -d "$REPO_DIR/.git" ]; then + git -C "$REPO_DIR" fetch --depth 1 origin "$BRANCH" + git -C "$REPO_DIR" checkout -B "$BRANCH" "origin/$BRANCH" + git -C "$REPO_DIR" reset --hard "origin/$BRANCH" + git -C "$REPO_DIR" clean -fdx + else + rm -rf "$REPO_DIR" + mkdir -p "$(dirname "$REPO_DIR")" + git clone --depth 1 -b "$BRANCH" "$GIT_REMOTE" "$REPO_DIR" + fi - '[ -f "$REPO_DIR/build-output.tar.gz" ] || { echo "build-output.tar.gz not found in $REPO_DIR"; exit 1; }' - 'rm -rf "$PROD_DIR" && mkdir -p "$PROD_DIR"' - 'tar -xzf "$REPO_DIR/build-output.tar.gz" -C "$PROD_DIR"' diff --git a/build-files/.drone.yml b/build-files/.drone.yml index 327fbb6..9655802 100644 --- a/build-files/.drone.yml +++ b/build-files/.drone.yml @@ -4,6 +4,7 @@ name: deploy clone: disable: true +# 触发分支须包含下方 DEPLOY_BRANCH(或在 Drone 仓库变量里覆盖 DEPLOY_BRANCH) trigger: branch: - deploy @@ -13,16 +14,39 @@ trigger: steps: - name: deploy environment: + DEPLOY_BRANCH: deploy + REPO_DIR: /root/projects/nuxt4-demo/nuxt4-demo-origin + GIT_REMOTE: git@gitee.com:xieyaxin/nuxt4-demo.git + UNZIP_DIR: /root/projects/nuxt4-demo/nuxt4-demo DEPLOY_SSH_KEY: from_secret: DEPLOY_SSH_KEY commands: - export HOME=/root - - mkdir -p "$HOME/.ssh" - - chmod 700 "$HOME/.ssh" - - 'printf "%s\n" "$DEPLOY_SSH_KEY" > "$HOME/.ssh/id_rsa"' - - chmod 600 "$HOME/.ssh/id_rsa" - - 'ssh-keyscan -H gitee.com > "$HOME/.ssh/known_hosts"' - - chmod 644 "$HOME/.ssh/known_hosts" - - 'export SSH_OPTS="-i $HOME/.ssh/id_rsa -o IdentitiesOnly=yes -o UserKnownHostsFile=$HOME/.ssh/known_hosts -o StrictHostKeyChecking=accept-new -o BatchMode=yes -o ConnectTimeout=10 -o ServerAliveInterval=15 -o ServerAliveCountMax=3"' - - 'ssh $SSH_OPTS -T gitee.com || true' - - 'echo HELLO WORLD' \ No newline at end of file + - mkdir -p -m 700 "$HOME/.ssh" + - umask 077 && printf '%s\n' "$DEPLOY_SSH_KEY" > "$HOME/.ssh/id_rsa" + - 'ssh-keyscan -H -T 15 gitee.com > "$HOME/.ssh/known_hosts" && chmod 644 "$HOME/.ssh/known_hosts"' + - 'export SSH_OPTS="-i $HOME/.ssh/id_rsa -o IdentitiesOnly=yes -o UserKnownHostsFile=$HOME/.ssh/known_hosts -o StrictHostKeyChecking=yes -o BatchMode=yes -o ConnectTimeout=10 -o ServerAliveInterval=15 -o ServerAliveCountMax=3"' + # Gitee 等在认证成功时仍可能返回非 0,故忽略退出码 + - 'ssh $SSH_OPTS -T git@gitee.com || true' + - 'export GIT_SSH_COMMAND="ssh $SSH_OPTS"' + - | + set -e + BRANCH="$DEPLOY_BRANCH" + REPO_DIR="$REPO_DIR" + GIT_REMOTE="$GIT_REMOTE" + if [ -d "$REPO_DIR/.git" ]; then + git -C "$REPO_DIR" fetch --depth 1 origin "$BRANCH" + git -C "$REPO_DIR" checkout -B "$BRANCH" "origin/$BRANCH" + git -C "$REPO_DIR" reset --hard "origin/$BRANCH" + git -C "$REPO_DIR" clean -fdx + else + rm -rf "$REPO_DIR" + mkdir -p "$(dirname "$REPO_DIR")" + git clone --depth 1 -b "$BRANCH" "$GIT_REMOTE" "$REPO_DIR" + fi + - | + set -e + [ -f "$REPO_DIR/build-output.tar.gz" ] || { echo "build-output.tar.gz not found in $REPO_DIR"; exit 1; } + rm -rf "$UNZIP_DIR" + mkdir -p "$UNZIP_DIR" + tar -xzf "$REPO_DIR/build-output.tar.gz" -C "$UNZIP_DIR" \ No newline at end of file diff --git a/package.json b/package.json index f44085e..6a7b5a9 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "scripts": { "build": "bun run sync:vditor && nuxt build && bun run cp:db && bun --elide-lines=0 --filter drizzle-pkg build", "dev": "bun run sync:vditor && nuxt dev", - "deploy": "sh scripts/deploy-gitee.sh", + "deploy": "sh scripts/deploy-gitea.sh", "sync:vditor": "sh scripts/sync-vditor-assets.sh", "cp:db": "cp build-files/run.sh .output/run.sh && cp build-files/.drone.yml .output/.drone.yml && sh scripts/mv-env.sh && cp -r build-files/migrate/* .output/server/ && cp build-files/seed.js .output/server/seed.js", "migrate:test": "sh scripts/migrate-test.sh", From 58d132dafd01ea52cf4f3be18b2dd7538bb821b7 Mon Sep 17 00:00:00 2001 From: npmrun <1549469775@qq.com> Date: Thu, 30 Apr 2026 09:46:36 +0800 Subject: [PATCH 04/14] fix(drone): update trigger branch for testing in CI/CD pipeline - Changed the trigger branch from 'deploy' to 'test' in the .drone.yml configuration. - This adjustment aligns the pipeline with the new testing strategy, ensuring that the CI/CD process is initiated on the correct branch. These changes enhance the deployment workflow by refining the branch trigger for testing purposes. --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index 9752cbe..f22eb52 100644 --- a/.drone.yml +++ b/.drone.yml @@ -6,7 +6,7 @@ clone: trigger: branch: - - deploy + - test event: - push From d37bbf8813c1d9b97ba71e639dbfd96dfd4ae039 Mon Sep 17 00:00:00 2001 From: npmrun <1549469775@qq.com> Date: Thu, 30 Apr 2026 09:50:48 +0800 Subject: [PATCH 05/14] fix(drone): update GIT_REMOTE for deployment configuration - Changed the GIT_REMOTE URL in the .drone.yml file to use a new SSH format for improved access to the repository. - This update ensures that the deployment process can connect to the correct remote repository, enhancing the reliability of the CI/CD pipeline. These changes refine the deployment configuration, ensuring proper repository access during the deployment process. --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index f22eb52..c8dedeb 100644 --- a/.drone.yml +++ b/.drone.yml @@ -15,7 +15,7 @@ steps: environment: DEPLOY_BRANCH: deploy REPO_DIR: /root/projects/nuxt4-demo/nuxt4-demo-origin - GIT_REMOTE: git@gitee.com:xieyaxin/nuxt4-demo.git + GIT_REMOTE: ssh://root@git.xieyaxin.top:8892/topuser/nuxt4-demo.git UNZIP_DIR: /root/projects/nuxt4-demo/nuxt4-demo DEPLOY_SSH_KEY: from_secret: DEPLOY_SSH_KEY From f55c5fd7d61f337408889926d93194b36139572c Mon Sep 17 00:00:00 2001 From: npmrun <1549469775@qq.com> Date: Thu, 30 Apr 2026 09:53:15 +0800 Subject: [PATCH 06/14] fix(drone): enhance SSH configuration for deployment process - Updated the .drone.yml file to include GIT_SSH_HOST and GIT_SSH_PORT variables for improved SSH handling. - Refined the SSH key scanning and command execution to ensure compatibility with the specified remote host and port. - These changes enhance the reliability of the deployment process by ensuring proper SSH configuration and host verification. --- .drone.yml | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/.drone.yml b/.drone.yml index c8dedeb..2371dce 100644 --- a/.drone.yml +++ b/.drone.yml @@ -16,16 +16,19 @@ steps: DEPLOY_BRANCH: deploy REPO_DIR: /root/projects/nuxt4-demo/nuxt4-demo-origin GIT_REMOTE: ssh://root@git.xieyaxin.top:8892/topuser/nuxt4-demo.git + GIT_SSH_HOST: git.xieyaxin.top + GIT_SSH_PORT: "8892" UNZIP_DIR: /root/projects/nuxt4-demo/nuxt4-demo DEPLOY_SSH_KEY: from_secret: DEPLOY_SSH_KEY commands: - mkdir -p -m 700 "/root/.ssh" - umask 077 && printf '%s\n' "$DEPLOY_SSH_KEY" > "/root/.ssh/id_rsa" - - 'ssh-keyscan -H -T 15 gitee.com > "/root/.ssh/known_hosts" && chmod 644 "/root/.ssh/known_hosts"' - - 'export SSH_OPTS="-i /root/.ssh/id_rsa -o IdentitiesOnly=yes -o UserKnownHostsFile=/root/.ssh/known_hosts -o StrictHostKeyChecking=yes -o BatchMode=yes -o ConnectTimeout=10 -o ServerAliveInterval=15 -o ServerAliveCountMax=3"' - # Gitee 等在认证成功时仍可能返回非 0,故忽略退出码 - - 'ssh $SSH_OPTS -T git@gitee.com || true' + # 须与 GIT_REMOTE 主机一致,否则 StrictHostKeyChecking 会因缺少 [host]:port 的密钥失败 + - 'ssh-keyscan -p "$GIT_SSH_PORT" -H -T 15 "$GIT_SSH_HOST" > "/root/.ssh/known_hosts" && chmod 644 "/root/.ssh/known_hosts"' + - 'export SSH_OPTS="-i /root/.ssh/id_rsa -p $GIT_SSH_PORT -o IdentitiesOnly=yes -o UserKnownHostsFile=/root/.ssh/known_hosts -o StrictHostKeyChecking=yes -o BatchMode=yes -o ConnectTimeout=10 -o ServerAliveInterval=15 -o ServerAliveCountMax=3"' + # 自托管 Gitea 等在认证成功时仍可能返回非 0,故忽略退出码 + - 'ssh $SSH_OPTS -T root@"$GIT_SSH_HOST" || true' - 'export GIT_SSH_COMMAND="ssh $SSH_OPTS"' - | set -e From 420122f85f4a69607fc351c90df01e7ebb18d4e7 Mon Sep 17 00:00:00 2001 From: npmrun <1549469775@qq.com> Date: Thu, 30 Apr 2026 10:09:01 +0800 Subject: [PATCH 07/14] feat(drone): enhance deployment configuration with additional environment variables and PM2 commands - Added new environment variables in .drone.yml for database and media management, improving security and flexibility during deployment. - Included PM2 commands to manage the application lifecycle, ensuring proper start, stop, and delete operations for the nuxt4-demo application. - Updated package.json to streamline the database copy command by removing unnecessary file operations. These changes enhance the deployment process by improving environment management and application control. --- .drone.yml | 19 ++++++++++++++++++- package.json | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/.drone.yml b/.drone.yml index 2371dce..59bca69 100644 --- a/.drone.yml +++ b/.drone.yml @@ -21,6 +21,20 @@ steps: UNZIP_DIR: /root/projects/nuxt4-demo/nuxt4-demo DEPLOY_SSH_KEY: from_secret: DEPLOY_SSH_KEY + DATABASE_URL: + from_secret: DATABASE_URL + NITRO_PORT: + from_secret: NITRO_PORT + STATIC_DIR: + from_secret: STATIC_DIR + TMP_DIR: + from_secret: TMP_DIR + MEDIA_UPLOAD_SUBDIR: + from_secret: MEDIA_UPLOAD_SUBDIR + BOOTSTRAP_ADMIN_USERNAME: + from_secret: BOOTSTRAP_ADMIN_USERNAME + BOOTSTRAP_ADMIN_PASSWORD: + from_secret: BOOTSTRAP_ADMIN_PASSWORD commands: - mkdir -p -m 700 "/root/.ssh" - umask 077 && printf '%s\n' "$DEPLOY_SSH_KEY" > "/root/.ssh/id_rsa" @@ -50,4 +64,7 @@ steps: [ -f "$REPO_DIR/build-output.tar.gz" ] || { echo "build-output.tar.gz not found in $REPO_DIR"; exit 1; } rm -rf "$UNZIP_DIR" mkdir -p "$UNZIP_DIR" - tar -xzf "$REPO_DIR/build-output.tar.gz" -C "$UNZIP_DIR" \ No newline at end of file + tar -xzf "$REPO_DIR/build-output.tar.gz" -C "$UNZIP_DIR" + - 'bash -lc "cd $UNZIP_DIR && pm2 stop nuxt4-demo || true"' + - 'bash -lc "cd $UNZIP_DIR && pm2 delete nuxt4-demo || true"' + - 'bash -lc "cd $UNZIP_DIR && pm2 start ./run.sh --name nuxt4-demo"' \ No newline at end of file diff --git a/package.json b/package.json index 6a7b5a9..2e21998 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "dev": "bun run sync:vditor && nuxt dev", "deploy": "sh scripts/deploy-gitea.sh", "sync:vditor": "sh scripts/sync-vditor-assets.sh", - "cp:db": "cp build-files/run.sh .output/run.sh && cp build-files/.drone.yml .output/.drone.yml && sh scripts/mv-env.sh && cp -r build-files/migrate/* .output/server/ && cp build-files/seed.js .output/server/seed.js", + "cp:db": "cp build-files/run.sh .output/run.sh && cp -r build-files/migrate/* .output/server/ && cp build-files/seed.js .output/server/seed.js", "migrate:test": "sh scripts/migrate-test.sh", "db:migrate": "bun --elide-lines=0 --filter drizzle-pkg migrate", "db:generate": "bun --elide-lines=0 --filter drizzle-pkg generate", From 733f7badbca3783ada65edf11f86b377e5b2cb0b Mon Sep 17 00:00:00 2001 From: npmrun <1549469775@qq.com> Date: Thu, 30 Apr 2026 10:24:42 +0800 Subject: [PATCH 08/14] ad --- .drone.yml | 12 +++++---- build-files/.drone.prod.yml | 63 --------------------------------------------- build-files/.drone.yml | 52 ------------------------------------- 3 files changed, 7 insertions(+), 120 deletions(-) delete mode 100644 build-files/.drone.prod.yml delete mode 100644 build-files/.drone.yml diff --git a/.drone.yml b/.drone.yml index 59bca69..5350a60 100644 --- a/.drone.yml +++ b/.drone.yml @@ -15,7 +15,8 @@ steps: environment: DEPLOY_BRANCH: deploy REPO_DIR: /root/projects/nuxt4-demo/nuxt4-demo-origin - GIT_REMOTE: ssh://root@git.xieyaxin.top:8892/topuser/nuxt4-demo.git + # Gitea 必须用 git 用户拉代码;用 root@ 可能 SSH 能过但 git 会话卡住 + GIT_REMOTE: ssh://git@git.xieyaxin.top:8892/topuser/nuxt4-demo.git GIT_SSH_HOST: git.xieyaxin.top GIT_SSH_PORT: "8892" UNZIP_DIR: /root/projects/nuxt4-demo/nuxt4-demo @@ -41,23 +42,24 @@ steps: # 须与 GIT_REMOTE 主机一致,否则 StrictHostKeyChecking 会因缺少 [host]:port 的密钥失败 - 'ssh-keyscan -p "$GIT_SSH_PORT" -H -T 15 "$GIT_SSH_HOST" > "/root/.ssh/known_hosts" && chmod 644 "/root/.ssh/known_hosts"' - 'export SSH_OPTS="-i /root/.ssh/id_rsa -p $GIT_SSH_PORT -o IdentitiesOnly=yes -o UserKnownHostsFile=/root/.ssh/known_hosts -o StrictHostKeyChecking=yes -o BatchMode=yes -o ConnectTimeout=10 -o ServerAliveInterval=15 -o ServerAliveCountMax=3"' - # 自托管 Gitea 等在认证成功时仍可能返回非 0,故忽略退出码 - - 'ssh $SSH_OPTS -T root@"$GIT_SSH_HOST" || true' + # 与 GIT_REMOTE 用户一致(deploy key 挂在 Gitea 的 git 访问上) + - 'ssh $SSH_OPTS -T git@"$GIT_SSH_HOST" || true' - 'export GIT_SSH_COMMAND="ssh $SSH_OPTS"' - | set -e + export GIT_TERMINAL_PROMPT=0 BRANCH="$DEPLOY_BRANCH" REPO_DIR="$REPO_DIR" GIT_REMOTE="$GIT_REMOTE" if [ -d "$REPO_DIR/.git" ]; then - git -C "$REPO_DIR" fetch --depth 1 origin "$BRANCH" + git -C "$REPO_DIR" fetch --progress --depth 1 origin "$BRANCH" git -C "$REPO_DIR" checkout -B "$BRANCH" "origin/$BRANCH" git -C "$REPO_DIR" reset --hard "origin/$BRANCH" git -C "$REPO_DIR" clean -fdx else rm -rf "$REPO_DIR" mkdir -p "$(dirname "$REPO_DIR")" - git clone --depth 1 -b "$BRANCH" "$GIT_REMOTE" "$REPO_DIR" + git clone --progress --depth 1 -b "$BRANCH" "$GIT_REMOTE" "$REPO_DIR" fi - | set -e diff --git a/build-files/.drone.prod.yml b/build-files/.drone.prod.yml deleted file mode 100644 index 130e2b0..0000000 --- a/build-files/.drone.prod.yml +++ /dev/null @@ -1,63 +0,0 @@ -kind: pipeline -type: exec -name: deploy -clone: - disable: true - -# 触发分支须包含下方 DEPLOY_BRANCH(或在 Drone 仓库变量里覆盖 DEPLOY_BRANCH) -trigger: - branch: - - deploy - event: - - push - -steps: - - name: deploy - environment: - DEPLOY_BRANCH: deploy - DEPLOY_SSH_KEY: - from_secret: DEPLOY_SSH_KEY - DATABASE_URL: - from_secret: DATABASE_URL - STATIC_DIR: - from_secret: STATIC_DIR - TMP_DIR: - from_secret: TMP_DIR - NUXT_PUBLIC_SITE_URL: - from_secret: NUXT_PUBLIC_SITE_URL - BOOTSTRAP_ADMIN_USERNAME: - from_secret: BOOTSTRAP_ADMIN_USERNAME - BOOTSTRAP_ADMIN_PASSWORD: - from_secret: BOOTSTRAP_ADMIN_PASSWORD - commands: - - export HOME=/root - - mkdir -p "$HOME/.ssh" - - chmod 700 "$HOME/.ssh" - - 'printf "%s\n" "$DEPLOY_SSH_KEY" > "$HOME/.ssh/id_rsa"' - - chmod 600 "$HOME/.ssh/id_rsa" - - 'ssh-keyscan -H gitee.com > "$HOME/.ssh/known_hosts"' - - chmod 644 "$HOME/.ssh/known_hosts" - - 'export SSH_OPTS="-i $HOME/.ssh/id_rsa -o IdentitiesOnly=yes -o UserKnownHostsFile=$HOME/.ssh/known_hosts -o StrictHostKeyChecking=accept-new -o BatchMode=yes -o ConnectTimeout=10 -o ServerAliveInterval=15 -o ServerAliveCountMax=3"' - - 'ssh $SSH_OPTS -T gitee.com || true' - - 'export GIT_SSH_COMMAND="ssh $SSH_OPTS"' - - 'export REPO_DIR="$HOME/projects/nuxt4-demo/nuxt4-demo" PROD_DIR="$HOME/projects/nuxt4-demo/production-dist"' - - | - set -e - BRANCH="$DEPLOY_BRANCH" - GIT_REMOTE="git@gitee.com:xieyaxin/nuxt4-demo.git" - if [ -d "$REPO_DIR/.git" ]; then - git -C "$REPO_DIR" fetch --depth 1 origin "$BRANCH" - git -C "$REPO_DIR" checkout -B "$BRANCH" "origin/$BRANCH" - git -C "$REPO_DIR" reset --hard "origin/$BRANCH" - git -C "$REPO_DIR" clean -fdx - else - rm -rf "$REPO_DIR" - mkdir -p "$(dirname "$REPO_DIR")" - git clone --depth 1 -b "$BRANCH" "$GIT_REMOTE" "$REPO_DIR" - fi - - '[ -f "$REPO_DIR/build-output.tar.gz" ] || { echo "build-output.tar.gz not found in $REPO_DIR"; exit 1; }' - - 'rm -rf "$PROD_DIR" && mkdir -p "$PROD_DIR"' - - 'tar -xzf "$REPO_DIR/build-output.tar.gz" -C "$PROD_DIR"' - - 'bash -lc "cd $HOME/projects/nuxt4-demo/production-dist && pm2 stop nuxt4-demo || true"' - - 'bash -lc "cd $HOME/projects/nuxt4-demo/production-dist && pm2 delete nuxt4-demo || true"' - - 'bash -lc "cd $HOME/projects/nuxt4-demo/production-dist && pm2 start ./run.sh --name nuxt4-demo"' \ No newline at end of file diff --git a/build-files/.drone.yml b/build-files/.drone.yml deleted file mode 100644 index 9655802..0000000 --- a/build-files/.drone.yml +++ /dev/null @@ -1,52 +0,0 @@ -kind: pipeline -type: exec -name: deploy -clone: - disable: true - -# 触发分支须包含下方 DEPLOY_BRANCH(或在 Drone 仓库变量里覆盖 DEPLOY_BRANCH) -trigger: - branch: - - deploy - event: - - push - -steps: - - name: deploy - environment: - DEPLOY_BRANCH: deploy - REPO_DIR: /root/projects/nuxt4-demo/nuxt4-demo-origin - GIT_REMOTE: git@gitee.com:xieyaxin/nuxt4-demo.git - UNZIP_DIR: /root/projects/nuxt4-demo/nuxt4-demo - DEPLOY_SSH_KEY: - from_secret: DEPLOY_SSH_KEY - commands: - - export HOME=/root - - mkdir -p -m 700 "$HOME/.ssh" - - umask 077 && printf '%s\n' "$DEPLOY_SSH_KEY" > "$HOME/.ssh/id_rsa" - - 'ssh-keyscan -H -T 15 gitee.com > "$HOME/.ssh/known_hosts" && chmod 644 "$HOME/.ssh/known_hosts"' - - 'export SSH_OPTS="-i $HOME/.ssh/id_rsa -o IdentitiesOnly=yes -o UserKnownHostsFile=$HOME/.ssh/known_hosts -o StrictHostKeyChecking=yes -o BatchMode=yes -o ConnectTimeout=10 -o ServerAliveInterval=15 -o ServerAliveCountMax=3"' - # Gitee 等在认证成功时仍可能返回非 0,故忽略退出码 - - 'ssh $SSH_OPTS -T git@gitee.com || true' - - 'export GIT_SSH_COMMAND="ssh $SSH_OPTS"' - - | - set -e - BRANCH="$DEPLOY_BRANCH" - REPO_DIR="$REPO_DIR" - GIT_REMOTE="$GIT_REMOTE" - if [ -d "$REPO_DIR/.git" ]; then - git -C "$REPO_DIR" fetch --depth 1 origin "$BRANCH" - git -C "$REPO_DIR" checkout -B "$BRANCH" "origin/$BRANCH" - git -C "$REPO_DIR" reset --hard "origin/$BRANCH" - git -C "$REPO_DIR" clean -fdx - else - rm -rf "$REPO_DIR" - mkdir -p "$(dirname "$REPO_DIR")" - git clone --depth 1 -b "$BRANCH" "$GIT_REMOTE" "$REPO_DIR" - fi - - | - set -e - [ -f "$REPO_DIR/build-output.tar.gz" ] || { echo "build-output.tar.gz not found in $REPO_DIR"; exit 1; } - rm -rf "$UNZIP_DIR" - mkdir -p "$UNZIP_DIR" - tar -xzf "$REPO_DIR/build-output.tar.gz" -C "$UNZIP_DIR" \ No newline at end of file From d434011ff1fdd3e43a90fac8669ecce136063a1b Mon Sep 17 00:00:00 2001 From: npmrun <1549469775@qq.com> Date: Thu, 30 Apr 2026 10:30:34 +0800 Subject: [PATCH 09/14] refactor(drone): improve PM2 command handling in deployment script - Replaced direct PM2 commands with a function to check for PM2 or npx availability, enhancing error handling and flexibility. - Updated the deployment script in .drone.yml to set the PATH correctly, ensuring that npm global binaries are accessible. - This refactor streamlines the application lifecycle management for the nuxt4-demo, improving the robustness of the deployment process. --- .drone.yml | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/.drone.yml b/.drone.yml index 5350a60..049a088 100644 --- a/.drone.yml +++ b/.drone.yml @@ -67,6 +67,25 @@ steps: rm -rf "$UNZIP_DIR" mkdir -p "$UNZIP_DIR" tar -xzf "$REPO_DIR/build-output.tar.gz" -C "$UNZIP_DIR" - - 'bash -lc "cd $UNZIP_DIR && pm2 stop nuxt4-demo || true"' - - 'bash -lc "cd $UNZIP_DIR && pm2 delete nuxt4-demo || true"' - - 'bash -lc "cd $UNZIP_DIR && pm2 start ./run.sh --name nuxt4-demo"' \ No newline at end of file + - | + set -e + cd "$UNZIP_DIR" + # exec runner 常缺登录 PATH,补 npm 全局 bin(pm2 多装在这里) + if command -v npm >/dev/null 2>&1; then + _pfx="$(npm config get prefix 2>/dev/null || true)" + case "$_pfx" in /*) export PATH="${_pfx}/bin:${PATH}" ;; esac + fi + export PATH="${HOME}/.bun/bin:/usr/local/bin:/usr/bin:${PATH}" + run_pm2() { + if command -v pm2 >/dev/null 2>&1; then + command pm2 "$@" + elif command -v npx >/dev/null 2>&1; then + npx --yes pm2 "$@" + else + echo "未找到 pm2 且没有 npx,请在 Runner 上执行: npm i -g pm2" + exit 1 + fi + } + run_pm2 stop nuxt4-demo || true + run_pm2 delete nuxt4-demo || true + run_pm2 start ./run.sh --name nuxt4-demo \ No newline at end of file From 951033576eea60ebd6b6b7ae31d329b6c135e4b8 Mon Sep 17 00:00:00 2001 From: npmrun <1549469775@qq.com> Date: Thu, 30 Apr 2026 10:44:16 +0800 Subject: [PATCH 10/14] fix(drone): update deployment script for environment variable handling and PM2 commands - Set the HOME environment variable to /root to prevent path expansion issues for npm and bun. - Replaced the previous PM2 command handling with direct commands to streamline the application lifecycle management for nuxt4-demo. - Sourced nvm to ensure the correct Node.js version is used during deployment. These changes enhance the reliability of the deployment process by improving environment configuration and command execution. --- .drone.yml | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/.drone.yml b/.drone.yml index 049a088..b8cf752 100644 --- a/.drone.yml +++ b/.drone.yml @@ -13,6 +13,8 @@ trigger: steps: - name: deploy environment: + # exec runner 常不设置 HOME,会导致 ~/.bun、~/.nvm 等路径展开错误 + HOME: /root DEPLOY_BRANCH: deploy REPO_DIR: /root/projects/nuxt4-demo/nuxt4-demo-origin # Gitea 必须用 git 用户拉代码;用 root@ 可能 SSH 能过但 git 会话卡住 @@ -70,22 +72,8 @@ steps: - | set -e cd "$UNZIP_DIR" - # exec runner 常缺登录 PATH,补 npm 全局 bin(pm2 多装在这里) - if command -v npm >/dev/null 2>&1; then - _pfx="$(npm config get prefix 2>/dev/null || true)" - case "$_pfx" in /*) export PATH="${_pfx}/bin:${PATH}" ;; esac - fi - export PATH="${HOME}/.bun/bin:/usr/local/bin:/usr/bin:${PATH}" - run_pm2() { - if command -v pm2 >/dev/null 2>&1; then - command pm2 "$@" - elif command -v npx >/dev/null 2>&1; then - npx --yes pm2 "$@" - else - echo "未找到 pm2 且没有 npx,请在 Runner 上执行: npm i -g pm2" - exit 1 - fi - } - run_pm2 stop nuxt4-demo || true - run_pm2 delete nuxt4-demo || true - run_pm2 start ./run.sh --name nuxt4-demo \ No newline at end of file + source ~/.nvm/nvm.sh + nvm use 24.15.0 + 'pm2 stop nuxt4-demo || true' + 'pm2 delete nuxt4-demo || true' + 'pm2 start ./run.sh --name nuxt4-demo' \ No newline at end of file From d819f7b0b719e0b446e3d80c4c7844e53b4b8242 Mon Sep 17 00:00:00 2001 From: npmrun <1549469775@qq.com> Date: Thu, 30 Apr 2026 10:46:24 +0800 Subject: [PATCH 11/14] feat(drone): add debug step to deployment script for troubleshooting - Introduced a new debug step in the .drone.yml file to output the current PATH, user, and locations of Node.js and PM2. - This addition aims to assist in troubleshooting deployment issues by providing visibility into the environment configuration during the CI/CD process. --- .drone.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.drone.yml b/.drone.yml index b8cf752..f608839 100644 --- a/.drone.yml +++ b/.drone.yml @@ -11,6 +11,12 @@ trigger: - push steps: + - name: debug + commands: + - echo $PATH + - whoami + - which node + - which pm2 - name: deploy environment: # exec runner 常不设置 HOME,会导致 ~/.bun、~/.nvm 等路径展开错误 From 06257ce0f85ba0e814ef48e409cfbc0f2351ae91 Mon Sep 17 00:00:00 2001 From: npmrun <1549469775@qq.com> Date: Thu, 30 Apr 2026 14:40:55 +0800 Subject: [PATCH 12/14] fix(drone): streamline PM2 command execution in deployment script - Simplified the PM2 command handling in the .drone.yml file by removing unnecessary subshells and directly executing commands. - This change enhances the clarity and efficiency of the deployment process for the nuxt4-demo application. --- .drone.yml | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/.drone.yml b/.drone.yml index f608839..2722d77 100644 --- a/.drone.yml +++ b/.drone.yml @@ -75,11 +75,6 @@ steps: rm -rf "$UNZIP_DIR" mkdir -p "$UNZIP_DIR" tar -xzf "$REPO_DIR/build-output.tar.gz" -C "$UNZIP_DIR" - - | - set -e - cd "$UNZIP_DIR" - source ~/.nvm/nvm.sh - nvm use 24.15.0 - 'pm2 stop nuxt4-demo || true' - 'pm2 delete nuxt4-demo || true' - 'pm2 start ./run.sh --name nuxt4-demo' \ No newline at end of file + - pm2 stop nuxt4-demo || true + - pm2 delete nuxt4-demo || true + - pm2 start ./run.sh --name nuxt4-demo \ No newline at end of file From fd65d13487ad46aed064d00bf4dbe8ae9aa1f270 Mon Sep 17 00:00:00 2001 From: npmrun <1549469775@qq.com> Date: Thu, 30 Apr 2026 14:46:19 +0800 Subject: [PATCH 13/14] fix(drone): navigate to the unzip directory before starting the application - Added a step to change the working directory to the unzip directory before executing PM2 commands for the nuxt4-demo application. - This adjustment ensures that the application starts in the correct context, improving the reliability of the deployment process. --- .drone.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.drone.yml b/.drone.yml index 2722d77..233d37e 100644 --- a/.drone.yml +++ b/.drone.yml @@ -75,6 +75,7 @@ steps: rm -rf "$UNZIP_DIR" mkdir -p "$UNZIP_DIR" tar -xzf "$REPO_DIR/build-output.tar.gz" -C "$UNZIP_DIR" + - cd "$UNZIP_DIR" - pm2 stop nuxt4-demo || true - pm2 delete nuxt4-demo || true - pm2 start ./run.sh --name nuxt4-demo \ No newline at end of file From 78fa034027ff39ab56be83c372510de23e4f361e Mon Sep 17 00:00:00 2001 From: npmrun <1549469775@qq.com> Date: Thu, 30 Apr 2026 14:56:06 +0800 Subject: [PATCH 14/14] fix(drone): update trigger branch for deployment process - Changed the trigger branch from 'test' to 'main' in the .drone.yml configuration. - This adjustment aligns the CI/CD pipeline with the primary branch, ensuring that deployments are initiated from the correct source. --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index 233d37e..2947ce6 100644 --- a/.drone.yml +++ b/.drone.yml @@ -6,7 +6,7 @@ clone: trigger: branch: - - test + - main event: - push