151 lines
4.3 KiB
YAML
151 lines
4.3 KiB
YAML
name: PR合并自动部署
|
|
|
|
on:
|
|
pull_request:
|
|
branches:
|
|
- main
|
|
types:
|
|
- closed
|
|
|
|
concurrency:
|
|
group: production-deploy
|
|
cancel-in-progress: false
|
|
|
|
jobs:
|
|
test:
|
|
if: ${{ github.event.pull_request.merged == true }}
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: 检出 main
|
|
uses: actions/checkout@v4
|
|
with:
|
|
ref: main
|
|
|
|
- name: 配置 Python
|
|
uses: actions/setup-python@v5
|
|
with:
|
|
python-version: "3.11"
|
|
cache: pip
|
|
|
|
- name: 安装 MySQL 编译依赖
|
|
run: |
|
|
sudo apt-get update
|
|
sudo apt-get install -y default-libmysqlclient-dev pkg-config
|
|
|
|
- name: 安装测试依赖
|
|
run: pip install -r requirements-dev.txt
|
|
|
|
- name: 检查迁移文件
|
|
working-directory: backend
|
|
run: python manage.py makemigrations --check --dry-run
|
|
|
|
- name: Django 系统检查
|
|
working-directory: backend
|
|
run: python manage.py check
|
|
|
|
- name: ASGI 启动导入检查
|
|
working-directory: backend
|
|
run: python -c "from config.asgi import application; print(type(application).__name__)"
|
|
|
|
- name: 运行测试
|
|
run: pytest -q
|
|
|
|
- name: 启动 MySQL 8.0.35
|
|
run: |
|
|
docker rm -f hulumath-deploy-mysql 2>/dev/null || true
|
|
docker run -d --name hulumath-deploy-mysql \
|
|
-p 3307:3306 \
|
|
-e MYSQL_ROOT_PASSWORD=ci-root-password \
|
|
-e MYSQL_DATABASE=hulumath \
|
|
mysql:8.0.35 \
|
|
--character-set-server=utf8mb4 \
|
|
--collation-server=utf8mb4_0900_ai_ci
|
|
for i in $(seq 1 60); do
|
|
if docker exec hulumath-deploy-mysql \
|
|
mysqladmin ping -h 127.0.0.1 -uroot -pci-root-password --silent; then
|
|
exit 0
|
|
fi
|
|
sleep 2
|
|
done
|
|
docker logs hulumath-deploy-mysql
|
|
exit 1
|
|
|
|
- name: MySQL 迁移与全量测试
|
|
env:
|
|
DATABASE_URL: mysql://root:ci-root-password@127.0.0.1:3307/hulumath
|
|
run: |
|
|
python backend/manage.py check --database default
|
|
python backend/manage.py check_mysql
|
|
python backend/manage.py migrate --noinput
|
|
pytest -q
|
|
|
|
deploy:
|
|
if: ${{ github.event.pull_request.merged == true }}
|
|
needs:
|
|
- test
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: 配置 SSH 环境
|
|
env:
|
|
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
|
|
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
|
|
run: |
|
|
set -eu
|
|
mkdir -p ~/.ssh
|
|
printf '%s\n' "$DEPLOY_SSH_KEY" > ~/.ssh/deploy_key
|
|
chmod 600 ~/.ssh/deploy_key
|
|
ssh-keyscan -H "$DEPLOY_HOST" >> ~/.ssh/known_hosts
|
|
|
|
- name: 远程执行 Django 部署
|
|
env:
|
|
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
|
|
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
|
|
run: |
|
|
ssh -T -i ~/.ssh/deploy_key \
|
|
"$DEPLOY_USER@$DEPLOY_HOST" <<'EOF'
|
|
|
|
set -Eeuo pipefail
|
|
|
|
PROJECT="/www/wwwroot/Hulumath-Web"
|
|
REPOSITORY_URL="http://117.72.28.96:8765/Jacky/Hulumath-Web.git"
|
|
PYTHON_BIN="/www/server/pyporject_evn/versions/3.12.13/bin/python3"
|
|
|
|
exec 9>/tmp/hulumath-production-deploy.lock
|
|
if ! flock -n 9; then
|
|
echo "已有部署正在执行,本次退出"
|
|
exit 1
|
|
fi
|
|
|
|
echo "========================================"
|
|
echo "开始部署 Django 生产版"
|
|
echo "项目目录: $PROJECT"
|
|
echo "========================================"
|
|
|
|
cd "$PROJECT"
|
|
git config --global --add safe.directory "$PROJECT" || true
|
|
|
|
if [ ! -d .git ]; then
|
|
echo "当前目录不是 Git 仓库"
|
|
exit 1
|
|
fi
|
|
|
|
PREVIOUS_REVISION="$(git rev-parse HEAD)"
|
|
|
|
echo "获取远程 main..."
|
|
git remote set-url origin "$REPOSITORY_URL"
|
|
git fetch origin main
|
|
git reset --hard origin/main
|
|
|
|
chmod +x scripts/deploy_production.sh
|
|
|
|
PROJECT_DIR="$PROJECT" \
|
|
PYTHON_BIN="$PYTHON_BIN" \
|
|
PREVIOUS_REVISION="$PREVIOUS_REVISION" \
|
|
bash scripts/deploy_production.sh
|
|
|
|
echo "========================================"
|
|
echo "部署完成"
|
|
echo "========================================"
|
|
|
|
EOF
|