- deploy.sh: 目录级原子切换(current.new/current.old)替代 rm -rf 空窗式替换; 健康检查改为轮询 /api/version HTTP 接口;失败自动回滚上一版本 - 新增 deploy-watch.sh: 服务器本地 systemd timer 轮询 /ci-artifacts 产物指纹, 发现新版本自动部署——CI 与 AI 不再需要 SSH 到服务器执行任何命令 - server-deploy.yml: 部署包补入 web/ 后台静态资源;移除永不生效的前端构建死代码; 通知文案如实改为'构建成功'(部署由服务器侧 watcher 完成) - miniapp-preview.yml: 移除无意义的 --runInBand 测试参数 - 新增 PULL_DEPLOY.md 一次性安装文档(管理员手工执行)
116 lines
3.8 KiB
YAML
116 lines
3.8 KiB
YAML
name: Build and Deploy Server
|
||
|
||
on:
|
||
push:
|
||
branches:
|
||
- main
|
||
paths:
|
||
- "server/**"
|
||
- ".gitea/workflows/server-deploy.yml"
|
||
|
||
jobs:
|
||
build:
|
||
# 专属运行器:docker://golang:1.22-bookworm(注册于 doc79)
|
||
runs-on: lunar-ci
|
||
steps:
|
||
- uses: https://gitea.neatcn.com/gouki/action-checkout@v4
|
||
with:
|
||
fetch-depth: 20
|
||
|
||
- name: Verify Go runtime
|
||
run: go version
|
||
|
||
- name: Install dependencies
|
||
working-directory: server
|
||
run: go mod download
|
||
|
||
- name: Run tests
|
||
working-directory: server
|
||
run: go test ./...
|
||
|
||
- name: Build application
|
||
working-directory: server
|
||
env:
|
||
GITEA_SHA: ${{ gitea.sha }}
|
||
GITEA_RUN_NUMBER: ${{ gitea.run_number }}
|
||
run: |
|
||
mkdir -p bin
|
||
|
||
# 生成版本号(使用构建编号)
|
||
VERSION="1.0.${GITEA_RUN_NUMBER}"
|
||
BUILD_TIME="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
||
COMMIT_SHORT="${GITEA_SHA:0:7}"
|
||
|
||
echo "Building server version: $VERSION"
|
||
echo "Build time: $BUILD_TIME"
|
||
echo "Commit SHA: $COMMIT_SHORT"
|
||
|
||
# 使用 ldflags 注入版本信息
|
||
go build -ldflags "\
|
||
-X main.Version=$VERSION \
|
||
-X main.BuildTime=$BUILD_TIME \
|
||
-X main.CommitSha=$COMMIT_SHORT" \
|
||
-o bin/server cmd/main.go
|
||
|
||
# 同时保存版本信息到文件(用于部署时读取)
|
||
cat > bin/version.env <<EOF
|
||
APP_VERSION=$VERSION
|
||
APP_BUILD_TIME=$BUILD_TIME
|
||
APP_COMMIT_SHA=$COMMIT_SHORT
|
||
EOF
|
||
|
||
echo "Version info saved to bin/version.env:"
|
||
cat bin/version.env
|
||
|
||
- name: Create deployment package
|
||
working-directory: server
|
||
run: |
|
||
mkdir -p deploy
|
||
cp -r bin/ deploy/
|
||
cp -r migrations/ deploy/
|
||
cp -r scripts/ deploy/
|
||
# 管理后台静态资源必须随包发布(服务以相对路径 ./web 提供)
|
||
if [ -d "web" ]; then
|
||
cp -r web/ deploy/web/
|
||
fi
|
||
tar -czf deploy.tar.gz deploy/
|
||
|
||
- name: Persist deployment package on runner host
|
||
run: |
|
||
test -f server/deploy.tar.gz
|
||
test -d /ci-artifacts
|
||
cp server/deploy.tar.gz "/ci-artifacts/server-deploy-${{ gitea.sha }}.tar.gz"
|
||
cp server/deploy.tar.gz /ci-artifacts/server-deploy-latest.tar.gz
|
||
ls -la /ci-artifacts/server-deploy-latest.tar.gz
|
||
echo "Deployment package saved on runner host: /opt/lunar/ci-artifacts/server-deploy-latest.tar.gz"
|
||
|
||
- name: Send success notification
|
||
if: success()
|
||
env:
|
||
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
|
||
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
|
||
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
|
||
GITEA_RUN_NUMBER: ${{ gitea.run_number }}
|
||
run: |
|
||
VERSION="1.0.${GITEA_RUN_NUMBER}"
|
||
bash .gitea/scripts/notify.sh success \
|
||
"后端构建成功" \
|
||
"版本: v${VERSION}\n产物: /opt/lunar/ci-artifacts/server-deploy-latest.tar.gz\n部署由服务器侧 watcher 自动拉取完成"
|
||
|
||
- name: Send failure notification
|
||
if: failure()
|
||
env:
|
||
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
|
||
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
|
||
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
|
||
run: |
|
||
bash .gitea/scripts/notify.sh failure \
|
||
"后端构建失败" \
|
||
"请检查 Actions 日志了解失败原因"
|
||
|
||
- name: Cleanup
|
||
if: always()
|
||
run: |
|
||
rm -rf server/deploy/
|
||
rm -f server/deploy.tar.gz
|