Browse Source

feat(deploy): add CI/CD pipeline and deployment script

- Introduced a new Drone CI/CD pipeline configuration for automated deployment on the 'deploy-branch'.
- Created a deployment script that builds the project, manages temporary directories, and handles git operations for pushing build artifacts.
- Updated the .env.example file to reflect new environment variable configurations for deployment.
- Enhanced .gitignore to include temporary distribution files.

These changes streamline the deployment process and improve project management for production releases.
main
npmrun 2 weeks ago
parent
commit
f8b4321486
  1. 37
      .drone.yml
  2. 8
      .env.example
  3. 1
      .gitignore
  4. 1
      package.json
  5. 42
      scripts/deploy.sh

37
.drone.yml

@ -0,0 +1,37 @@
kind: pipeline
type: exec
name: deploy
trigger:
branch:
- deploy-branch
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:
- 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 git.xieyaxin.top >> "$HOME/.ssh/known_hosts"'
- chmod 644 "$HOME/.ssh/known_hosts"
- 'if [ ! -d "$HOME/projects/nuxt4-demo/nuxt4-demo" ]; then mkdir -p "$HOME/projects/nuxt4-demo" && GIT_SSH_COMMAND="ssh -i $HOME/.ssh/id_rsa -o IdentitiesOnly=yes" git clone -b deploy-branch "ssh://root@git.xieyaxin.top:8892/topuser/nuxt4-demo.git" "$HOME/projects/nuxt4-demo/nuxt4-demo"; else cd "$HOME/projects/nuxt4-demo/nuxt4-demo" && GIT_SSH_COMMAND="ssh -i $HOME/.ssh/id_rsa -o IdentitiesOnly=yes" git pull origin deploy-branch; fi'
- pm2 stop nuxt4-demo
- pm2 start nuxt4-demo

8
.env.example

@ -5,11 +5,11 @@ STATIC_DIR=static
MEDIA_UPLOAD_SUBDIR=media
TMP_DIR=.tmp
# 导出任务限制(防止服务器快速占满)
EXPORT_MAX_RUNNING_TASKS=2
EXPORT_MAX_QUEUED_TASKS=30
EXPORT_MAX_RETAINED_BYTES=2147483648
# EXPORT_MAX_RUNNING_TASKS=2
# EXPORT_MAX_QUEUED_TASKS=30
# EXPORT_MAX_RETAINED_BYTES=2147483648
# 站点对外根 URL(含协议与域名,可带端口)。用于:① 媒体库复制绝对链接 ② 文章/资料里绝对地址图片是否计为本站 /static/media/ 引用。生产环境务必设置,与浏览器访问地址一致。
NUXT_PUBLIC_SITE_URL=https://example.com
# NUXT_PUBLIC_SITE_URL=https://example.com
# Optional: first admin for an empty instance. Creates an admin only when no user has role=admin yet (same username/password rules as registration).
BOOTSTRAP_ADMIN_USERNAME=
BOOTSTRAP_ADMIN_PASSWORD=

1
.gitignore

@ -5,6 +5,7 @@
.nitro
.cache
dist
.tmp_dist
# Node dependencies
node_modules

1
package.json

@ -9,6 +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",
"sync:vditor": "sh scripts/sync-vditor-assets.sh",
"cp:db": "cp build-files/run.sh .output/run.sh && 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",

42
scripts/deploy.sh

@ -0,0 +1,42 @@
#!/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/* .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 "✅ 发布完成!"
Loading…
Cancel
Save