diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..35544cc --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,49 @@ +name: Deploy Django App + +on: + push: + branches: + - main # 触发条件:当代码推送到 main 分支时 + +jobs: + deploy: + runs-on: ubuntu-latest # 使用 GitHub 提供的 Ubuntu 环境 + + steps: + # 步骤 1:检出代码 + - name: Checkout code + uses: actions/checkout@v3 + + # 步骤 2:设置 Python 环境 + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.9' # 指定 Python 版本 + + # 步骤 3:安装依赖 + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + # 步骤 4:运行测试(可选) + - name: Run tests + run: | + python manage.py test + + # 步骤 5:部署到生产服务器 + - name: Deploy to production + uses: appleboy/ssh-action@v1 + with: + host: ${{ secrets.PRODUCTION_HOST }} # 生产服务器 IP 或域名 + username: ${{ secrets.PRODUCTION_USER }} # SSH 用户名 + key: ${{ secrets.PRODUCTION_SSH_KEY }} # SSH 私钥 + script: | + cd /opt/myapp # 项目部署目录 + git pull origin main # 拉取最新代码 + source /opt/myenv/bin/activate # 激活虚拟环境 + pip install -r requirements.txt # 安装依赖 + python manage.py collectstatic --noinput # 收集静态文件 + python manage.py migrate --noinput # 执行数据库迁移 + sudo systemctl restart gunicorn # 重启 Gunicorn + sudo systemctl restart nginx # 重启 Nginx