Files
tuliao_py/.github/workflows/deploy.yml
2025-02-10 22:06:49 +08:00

50 lines
1.6 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: 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