Files
jiu/.gitea/workflows/reset-db.yml
T
wangjia 2b180be926
Deploy / deploy (push) Successful in 1m16s
chore(deploy): 新增 reset-db workflow,清库重建表结构并写入 seed 数据
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-24 12:58:34 +08:00

66 lines
2.2 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
name: Reset DB
on:
workflow_dispatch:
inputs:
shops:
description: '写入的门店(逗号分隔,如 S001,S002,S003'
required: true
default: 'S001,S002,S003'
jobs:
reset:
runs-on: mac
steps:
- uses: actions/checkout@v4
- name: Setup SSH
run: |
mkdir -p ~/.ssh
printf '%s' "${{ secrets.EC2_SSH_KEY }}" > ~/.ssh/ec2_deploy.pem
chmod 600 ~/.ssh/ec2_deploy.pem
ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts
- name: Reset database & reseed
env:
EC2_HOST: ${{ secrets.EC2_HOST }}
EC2_USER: ${{ secrets.EC2_USER }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
SHOPS: ${{ github.event.inputs.shops }}
run: |
# 1. 清库重建
ssh -i ~/.ssh/ec2_deploy.pem ${EC2_USER}@${EC2_HOST} \
"docker exec jiu_mysql mysql -uroot -p${DB_PASSWORD} -e 'DROP DATABASE IF EXISTS jiu_db; CREATE DATABASE jiu_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;'"
echo "✓ Database reset"
# 2. 重启 jiu-serverAutoMigrate 重建表结构
ssh -i ~/.ssh/ec2_deploy.pem ${EC2_USER}@${EC2_HOST} \
"sudo systemctl restart jiu"
echo "✓ jiu-server restarted, waiting for AutoMigrate..."
# 3. 等待健康检查通过
for i in $(seq 1 30); do
ssh -i ~/.ssh/ec2_deploy.pem ${EC2_USER}@${EC2_HOST} \
"curl -sf http://localhost:8080/health" && break
sleep 2
done
echo "✓ Health check passed"
# 4. 写入 seed 数据
for SHOP in $(echo "$SHOPS" | tr ',' ' '); do
FILE="backend/seeds/${SHOP}.sql"
if [ ! -f "$FILE" ]; then
echo "⚠️ $FILE not found, skipping"
continue
fi
echo "→ Seeding $SHOP ..."
ssh -i ~/.ssh/ec2_deploy.pem ${EC2_USER}@${EC2_HOST} \
"docker exec -i jiu_mysql mysql -uroot -p${DB_PASSWORD} jiu_db" \
< "$FILE"
echo "✓ $SHOP done"
done
- name: Cleanup SSH key
if: always()
run: rm -f ~/.ssh/ec2_deploy.pem