Files
Hulumath-Web/.gitea/workflows/deploy.yml
T
Jacky 1f800d713f
CI / test (pull_request) Failing after 1m48s
ci: use domestic Python package mirror
2026-08-08 23:23:49 +08:00

145 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
services:
mysql:
image: mysql:8.0.35
env:
MYSQL_ROOT_PASSWORD: ci-root-password
MYSQL_DATABASE: hulumath
options: >-
--health-cmd "mysqladmin ping -h 127.0.0.1 -uroot -pci-root-password --silent"
--health-interval 5s
--health-timeout 5s
--health-retries 20
steps:
- name: 检出 main
env:
REPOSITORY_URL: http://117.72.28.96:8765/Jacky/Hulumath-Web.git
run: git clone --branch main --single-branch "$REPOSITORY_URL" .
- name: 显示 Python 版本
run: python3 --version
- name: 安装 MySQL 编译依赖
run: |
sudo apt-get update
sudo apt-get install -y default-libmysqlclient-dev pkg-config python3-venv
- name: 安装测试依赖
run: |
python3 -m venv .venv-ci
.venv-ci/bin/python -m pip install \
--index-url https://mirrors.aliyun.com/pypi/simple \
--timeout 120 \
--retries 5 \
-r requirements-dev.txt
- name: 检查迁移文件
working-directory: backend
run: ../.venv-ci/bin/python manage.py makemigrations --check --dry-run
- name: Django 系统检查
working-directory: backend
run: ../.venv-ci/bin/python manage.py check
- name: ASGI 启动导入检查
working-directory: backend
run: ../.venv-ci/bin/python -c "from config.asgi import application; print(type(application).__name__)"
- name: 运行测试
run: .venv-ci/bin/python -m pytest -q
- name: MySQL 迁移与全量测试
env:
DATABASE_URL: mysql://root:ci-root-password@mysql:3306/hulumath
run: |
.venv-ci/bin/python backend/manage.py check --database default
.venv-ci/bin/python backend/manage.py check_mysql
.venv-ci/bin/python backend/manage.py migrate --noinput
.venv-ci/bin/python -m 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