@@ -0,0 +1,62 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: "3.11"
|
||||
cache: pip
|
||||
- name: Install MySQL build dependencies
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y default-libmysqlclient-dev pkg-config
|
||||
- name: Install dependencies
|
||||
run: pip install -r requirements-dev.txt
|
||||
- name: Check migrations
|
||||
working-directory: backend
|
||||
run: python manage.py makemigrations --check --dry-run
|
||||
- name: Django checks
|
||||
working-directory: backend
|
||||
run: python manage.py check
|
||||
- name: ASGI import check
|
||||
working-directory: backend
|
||||
run: python -c "from config.asgi import application; print(type(application).__name__)"
|
||||
- name: Unit tests
|
||||
run: pytest -q
|
||||
- name: Start MySQL 8.0.35
|
||||
run: |
|
||||
docker rm -f hulumath-ci-mysql 2>/dev/null || true
|
||||
docker run -d --name hulumath-ci-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-ci-mysql \
|
||||
mysqladmin ping -h 127.0.0.1 -uroot -pci-root-password --silent; then
|
||||
exit 0
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
docker logs hulumath-ci-mysql
|
||||
exit 1
|
||||
- name: MySQL migrations and tests
|
||||
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
|
||||
- name: Build image
|
||||
run: docker build -t hulumath:${{ gitea.sha }} .
|
||||
@@ -0,0 +1,150 @@
|
||||
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
|
||||
Reference in New Issue
Block a user