后台增加自动部署更新功能

This commit is contained in:
2026-08-10 21:41:36 +08:00
parent 2022664e7e
commit df35018805
9 changed files with 513 additions and 5 deletions
+82 -1
View File
@@ -1,7 +1,12 @@
from django.contrib import admin from django.contrib import admin
from django.db import models from django.db import models
from django.shortcuts import redirect, render
from django.urls import path, reverse
from django.utils.html import format_html
from django.contrib import messages
from ckeditor_uploader.widgets import CKEditorUploadingWidget from ckeditor_uploader.widgets import CKEditorUploadingWidget
from .models import SiteSettings, Banner from .models import SiteSettings, Banner, DeployLog
from .deploy import start_deploy
@admin.register(SiteSettings) @admin.register(SiteSettings)
@@ -64,3 +69,79 @@ class BannerAdmin(admin.ModelAdmin):
'fields': ('link_url', 'sort_order', 'is_active') 'fields': ('link_url', 'sort_order', 'is_active')
}), }),
) )
@admin.register(DeployLog)
class DeployLogAdmin(admin.ModelAdmin):
list_display = ('started_at', 'status', 'triggered_by', 'duration', 'view_output_link')
list_filter = ('status',)
readonly_fields = ('id', 'triggered_by', 'started_at', 'finished_at', 'status', 'output')
ordering = ('-started_at',)
list_per_page = 20
def has_add_permission(self, request):
return False
def has_change_permission(self, request, obj=None):
return False
def has_delete_permission(self, request, obj=None):
return request.user.is_superuser
def view_output_link(self, obj):
if not obj.id:
return '-'
url = reverse('admin:core_deploylog_detail', args=[obj.id])
return format_html('<a href="{}" target="_blank">查看日志</a>', url)
view_output_link.short_description = '输出日志'
def get_urls(self):
urls = super().get_urls()
custom_urls = [
path(
'deploy/',
self.admin_site.admin_view(self.deploy_view),
name='core_deploy_trigger',
),
path(
'<path:log_id>/detail/',
self.admin_site.admin_view(self.detail_view),
name='core_deploylog_detail',
),
]
return custom_urls + urls
def deploy_view(self, request):
"""触发一次部署。"""
# 防止并发部署:检查是否有正在执行的任务
if DeployLog.objects.filter(status='running').exists():
messages.error(request, '已有部署任务正在执行,请等待其完成后再试。')
return redirect('admin:core_deploylog_changelist')
log = start_deploy(user=request.user)
messages.success(request, f'部署已启动(日志 ID: {str(log.id)[:8]}),请刷新页面查看进度。')
return redirect('admin:core_deploylog_detail', log_id=str(log.id))
def detail_view(self, request, log_id):
"""展示单条部署日志的实时输出。"""
try:
log = DeployLog.objects.get(id=log_id)
except (DeployLog.DoesNotExist, ValueError):
messages.error(request, '部署日志不存在。')
return redirect('admin:core_deploylog_changelist')
context = {
**self.admin_site.each_context(request),
'opts': self.model._meta,
'log': log,
'title': f'部署日志 - {log.started_at:%Y-%m-%d %H:%M:%S}',
'is_running': log.status == 'running',
}
return render(request, 'admin/core/deploylog/detail.html', context)
def changelist_view(self, request, extra_context=None):
"""在列表页顶部插入"一键部署"按钮。"""
extra_context = extra_context or {}
extra_context['deploy_url'] = reverse('admin:core_deploy_trigger')
extra_context['has_running'] = DeployLog.objects.filter(status='running').exists()
return super().changelist_view(request, extra_context)
+167
View File
@@ -0,0 +1,167 @@
"""在线部署模块:从 Git 拉取最新代码并完成更新。
执行流程:
1. git pull origin <branch>
2. pip install -r requirements.txt
3. python manage.py migrate
4. python manage.py collectstatic --noinput
5. sudo supervisorctl restart tgoim(可选,需配置 sudoers)
为避免 supervisor 重启时中断当前请求,部署在后台线程中执行,
用户可在后台管理界面查看实时日志。
"""
import os
import sys
import subprocess
import threading
from pathlib import Path
from django.conf import settings
from django.utils import timezone
BASE_DIR = settings.BASE_DIR
# 单步命令超时(秒)
STEP_TIMEOUT = 180
# 拉取的远程分支,可通过 settings.GIT_DEPLOY_BRANCH 覆盖
GIT_BRANCH = getattr(settings, 'GIT_DEPLOY_BRANCH', 'main')
# Supervisor 服务名,可通过 settings.SUPERVISOR_SERVICE_NAME 覆盖
SUPERVISOR_SERVICE = getattr(settings, 'SUPERVISOR_SERVICE_NAME', 'tgoim')
def _venv_python():
"""返回虚拟环境中的 python 可执行文件路径。"""
if os.name == 'nt':
candidate = BASE_DIR / 'venv' / 'Scripts' / 'python.exe'
else:
candidate = BASE_DIR / 'venv' / 'bin' / 'python'
if candidate.exists():
return str(candidate)
return sys.executable
def _venv_pip():
"""返回虚拟环境中的 pip 可执行文件路径。"""
if os.name == 'nt':
candidate = BASE_DIR / 'venv' / 'Scripts' / 'pip.exe'
else:
candidate = BASE_DIR / 'venv' / 'bin' / 'pip'
if candidate.exists():
return str(candidate)
return [sys.executable, '-m', 'pip']
def _run_step(log_lines, cmd, timeout=STEP_TIMEOUT):
"""执行一条命令,将输出追加到 log_lines,返回是否成功。"""
if isinstance(cmd, str):
display = cmd
args = cmd.split()
else:
display = ' '.join(str(c) for c in cmd)
args = [str(c) for c in cmd]
log_lines.append(f'\n$ {display}')
try:
result = subprocess.run(
args, cwd=str(BASE_DIR),
capture_output=True, text=True, timeout=timeout,
)
if result.stdout:
log_lines.append(result.stdout.rstrip())
if result.stderr:
log_lines.append(result.stderr.rstrip())
if result.returncode != 0:
log_lines.append(f'命令失败,退出码: {result.returncode}')
return False
return True
except subprocess.TimeoutExpired:
log_lines.append(f'命令超时({timeout}s)')
return False
except FileNotFoundError as e:
log_lines.append(f'命令未找到: {e}')
return False
except Exception as e:
log_lines.append(f'执行异常: {e}')
return False
def _run_deploy(log_id):
"""后台线程执行部署流程。"""
from .models import DeployLog
log = DeployLog.objects.get(id=log_id)
log_lines = []
venv_python = _venv_python()
venv_pip = _venv_pip()
pip_cmd = venv_pip if isinstance(venv_pip, list) else [venv_pip]
try:
log_lines.append(f'开始部署,分支: {GIT_BRANCH}')
log_lines.append(f'工作目录: {BASE_DIR}')
log_lines.append(f'Python: {venv_python}')
log.output = '\n'.join(log_lines)
log.save()
# 1. 拉取代码
log_lines.append('\n========== [1/5] 拉取最新代码 ==========')
log.output = '\n'.join(log_lines)
log.save()
if not _run_step(log_lines, ['git', 'pull', 'origin', GIT_BRANCH]):
raise RuntimeError('git pull 失败')
# 2. 安装依赖
log_lines.append('\n========== [2/5] 安装 Python 依赖 ==========')
log.output = '\n'.join(log_lines)
log.save()
if not _run_step(log_lines, pip_cmd + ['install', '-r', 'requirements.txt']):
raise RuntimeError('pip install 失败')
# 3. 数据库迁移
log_lines.append('\n========== [3/5] 执行数据库迁移 ==========')
log.output = '\n'.join(log_lines)
log.save()
if not _run_step(log_lines, [venv_python, 'manage.py', 'migrate']):
raise RuntimeError('migrate 失败')
# 4. 收集静态文件
log_lines.append('\n========== [4/5] 收集静态文件 ==========')
log.output = '\n'.join(log_lines)
log.save()
if not _run_step(log_lines, [venv_python, 'manage.py', 'collectstatic', '--noinput']):
raise RuntimeError('collectstatic 失败')
# 5. 重启服务
log_lines.append('\n========== [5/5] 重启应用服务 ==========')
# 先把成功状态写入数据库,因为 supervisorctl restart 可能会终止当前进程
log.status = 'success'
log_lines.append(f'代码更新成功,正在重启服务 {SUPERVISOR_SERVICE} ...')
log.output = '\n'.join(log_lines)
log.finished_at = timezone.now()
log.save()
restart_ok = _run_step(log_lines, ['sudo', '-n', 'supervisorctl', 'restart', SUPERVISOR_SERVICE])
if not restart_ok:
log_lines.append(
f'提示: 自动重启失败。请在服务器手动执行: '
f'sudo supervisorctl restart {SUPERVISOR_SERVICE}'
)
# 状态仍记为 success,因为代码已更新成功,仅重启未生效
log_lines.append('\n部署流程结束。')
log.output = '\n'.join(log_lines)
log.save()
except Exception as e:
log.status = 'failed'
log_lines.append(f'\n部署失败: {e}')
log.output = '\n'.join(log_lines)
log.finished_at = timezone.now()
log.save()
def start_deploy(user=None):
"""启动一次部署(后台线程执行),返回 DeployLog 对象。"""
from .models import DeployLog
log = DeployLog.objects.create(triggered_by=user, status='running')
thread = threading.Thread(target=_run_deploy, args=(str(log.id),), daemon=True)
thread.start()
return log
+32
View File
@@ -0,0 +1,32 @@
# Generated manually for DeployLog model
import uuid
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
('core', '0002_sitesettings_about_content_en_and_more'),
]
operations = [
migrations.CreateModel(
name='DeployLog',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('started_at', models.DateTimeField(auto_now_add=True, verbose_name='开始时间')),
('finished_at', models.DateTimeField(blank=True, null=True, verbose_name='结束时间')),
('status', models.CharField(choices=[('running', '执行中'), ('success', '成功'), ('failed', '失败')], default='running', max_length=20, verbose_name='状态')),
('output', models.TextField(blank=True, default='', verbose_name='输出日志')),
('triggered_by', models.ForeignKey(null=True, on_delete=models.SET_NULL, related_name='deploy_logs', to=settings.AUTH_USER_MODEL, verbose_name='触发者')),
],
options={
'verbose_name': '部署日志',
'verbose_name_plural': '部署日志',
'ordering': ['-started_at'],
},
),
]
+34
View File
@@ -1,3 +1,4 @@
import uuid
from django.db import models from django.db import models
@@ -63,3 +64,36 @@ class Banner(models.Model):
def __str__(self): def __str__(self):
return self.title_zh or f'Banner #{self.id}' return self.title_zh or f'Banner #{self.id}'
class DeployLog(models.Model):
STATUS_CHOICES = [
('running', '执行中'),
('success', '成功'),
('failed', '失败'),
]
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
triggered_by = models.ForeignKey(
'auth.User', on_delete=models.SET_NULL, null=True,
verbose_name='触发者', related_name='deploy_logs'
)
started_at = models.DateTimeField('开始时间', auto_now_add=True)
finished_at = models.DateTimeField('结束时间', null=True, blank=True)
status = models.CharField('状态', max_length=20, choices=STATUS_CHOICES, default='running')
output = models.TextField('输出日志', blank=True, default='')
class Meta:
ordering = ['-started_at']
verbose_name = '部署日志'
verbose_name_plural = verbose_name
def __str__(self):
return f'{self.started_at:%Y-%m-%d %H:%M:%S} - {self.get_status_display()}'
@property
def duration(self):
if self.finished_at:
delta = self.finished_at - self.started_at
return f'{delta.total_seconds():.1f}s'
return '-'
+3 -3
View File
@@ -70,7 +70,7 @@ a:hover { color: var(--primary-dark); }
.section-title h2 { margin-bottom: 0.75rem; } .section-title h2 { margin-bottom: 0.75rem; }
.section-title p { color: var(--text-secondary); margin-bottom: 0; } .section-title p { color: var(--text-secondary); margin-bottom: 0; }
.banner-carousel .carousel-item { height: 650px; background: #000; } .banner-carousel .carousel-item { height: 750px; background: #000; }
.banner-carousel .carousel-item img { width: 100%; height: 100%; object-fit: cover; opacity: 0.85; } .banner-carousel .carousel-item img { width: 100%; height: 100%; object-fit: cover; opacity: 0.85; }
.banner-carousel .carousel-caption { bottom: 25%; text-align: left; } .banner-carousel .carousel-caption { bottom: 25%; text-align: left; }
.banner-carousel .carousel-caption h1 { font-size: 2.5rem; margin-bottom: 1rem; } .banner-carousel .carousel-caption h1 { font-size: 2.5rem; margin-bottom: 1rem; }
@@ -261,13 +261,13 @@ a:hover { color: var(--primary-dark); }
.contact-form textarea.form-control { resize: vertical; min-height: 120px; } .contact-form textarea.form-control { resize: vertical; min-height: 120px; }
@media (max-width: 991.98px) { @media (max-width: 991.98px) {
.banner-carousel .carousel-item { height: 480px; } .banner-carousel .carousel-item { height: 560px; }
.banner-carousel .carousel-caption h1 { font-size: 1.75rem; } .banner-carousel .carousel-caption h1 { font-size: 1.75rem; }
.section { padding: 2.5rem 0; } .section { padding: 2.5rem 0; }
} }
@media (max-width: 767.98px) { @media (max-width: 767.98px) {
.banner-carousel .carousel-item { height: 380px; } .banner-carousel .carousel-item { height: 440px; }
.banner-carousel .carousel-caption { bottom: 20%; text-align: center; left: 10%; right: 10%; } .banner-carousel .carousel-caption { bottom: 20%; text-align: center; left: 10%; right: 10%; }
.banner-carousel .carousel-caption h1 { font-size: 1.25rem; } .banner-carousel .carousel-caption h1 { font-size: 1.25rem; }
.banner-carousel .carousel-caption p { font-size: 0.875rem; margin-bottom: 1rem; } .banner-carousel .carousel-caption p { font-size: 0.875rem; margin-bottom: 1rem; }
@@ -0,0 +1,23 @@
{% extends "admin/change_list.html" %}
{% load i18n %}
{% block object-tools-items %}
{% if deploy_url %}
{% if has_running %}
<li>
<span style="display:inline-block;padding:8px 12px;color:#999;cursor:not-allowed;">
{% trans '部署进行中…' %}
</span>
</li>
{% else %}
<li>
<a href="{{ deploy_url }}" class="addlink"
style="background-color:#165DFF;color:#fff;"
onclick="return confirm('确认从 Git 拉取最新代码并重启服务吗?\n\n执行步骤:\n1. git pull\n2. pip install\n3. migrate\n4. collectstatic\n5. supervisorctl restart');">
{% trans '一键部署更新' %}
</a>
</li>
{% endif %}
{% endif %}
{{ block.super }}
{% endblock %}
+110
View File
@@ -0,0 +1,110 @@
{% extends "admin/base_site.html" %}
{% load i18n %}
{% block extrastyle %}
{{ block.super }}
<style>
.deploy-header {
display: flex;
align-items: center;
gap: 16px;
margin-bottom: 16px;
flex-wrap: wrap;
}
.deploy-status {
display: inline-block;
padding: 4px 12px;
border-radius: 4px;
font-weight: 600;
font-size: 0.9rem;
}
.deploy-status.running { background: #fff7e6; color: #d46b08; }
.deploy-status.success { background: #f6ffed; color: #389e0d; }
.deploy-status.failed { background: #fff1f0; color: #cf1322; }
.deploy-meta {
color: #666;
font-size: 0.9rem;
margin-bottom: 12px;
}
.deploy-output {
background: #1e1e1e;
color: #d4d4d4;
font-family: "Consolas", "Monaco", "Courier New", monospace;
font-size: 0.85rem;
line-height: 1.5;
padding: 16px;
border-radius: 6px;
white-space: pre-wrap;
word-break: break-all;
max-height: 600px;
overflow-y: auto;
border: 1px solid #333;
}
.deploy-actions { margin-top: 16px; }
.deploy-actions a {
display: inline-block;
padding: 8px 16px;
background: #165DFF;
color: #fff;
border-radius: 4px;
text-decoration: none;
margin-right: 8px;
font-size: 0.9rem;
}
.deploy-actions a:hover { background: #0E42D2; }
.spinner {
display: inline-block;
width: 14px;
height: 14px;
border: 2px solid #d46b08;
border-top-color: transparent;
border-radius: 50%;
animation: spin 0.8s linear infinite;
vertical-align: middle;
margin-right: 6px;
}
@keyframes spin { to { transform: rotate(360deg); } }
</style>
{% endblock %}
{% block content %}
<div class="deploy-header">
<h1 style="margin:0;">{{ title }}</h1>
{% if log.status == 'running' %}
<span class="deploy-status running">
<span class="spinner"></span>{% trans '执行中' %}
</span>
{% elif log.status == 'success' %}
<span class="deploy-status success">{% trans '成功' %}</span>
{% elif log.status == 'failed' %}
<span class="deploy-status failed">{% trans '失败' %}</span>
{% endif %}
</div>
<div class="deploy-meta">
{% if log.triggered_by %}{% trans '触发者' %}: {{ log.triggered_by.username }} · {% endif %}
{% trans '开始' %}: {{ log.started_at|date:"Y-m-d H:i:s" }}
{% if log.finished_at %} · {% trans '结束' %}: {{ log.finished_at|date:"Y-m-d H:i:s" }} · {% trans '耗时' %}: {{ log.duration }}{% endif %}
</div>
<div class="deploy-output" id="deploy-output">{{ log.output|default:'(暂无输出)' }}</div>
<div class="deploy-actions">
{% if is_running %}
<a href="" onclick="location.reload();return false;">{% trans '刷新查看进度' %}</a>
{% else %}
<a href="{% url 'admin:core_deploylog_changelist' %}">{% trans '返回列表' %}</a>
<a href="{% url 'admin:core_deploy_trigger' %}"
onclick="return confirm('确认从 Git 拉取最新代码并重启服务吗?');">
{% trans '再次部署' %}
</a>
{% endif %}
</div>
{% if is_running %}
<script>
// 自动刷新:每 3 秒拉取最新日志
setTimeout(function() { location.reload(); }, 3000);
</script>
{% endif %}
{% endblock %}
+1 -1
View File
@@ -44,7 +44,7 @@
<div class="col-lg-4 col-md-6"> <div class="col-lg-4 col-md-6">
<div class="card h-100"> <div class="card h-100">
{% if product.main_image %} {% if product.main_image %}
<img src="{{ product.main_image.url }}" class="card-img-top" alt="{% ml_field product 'name' %}" style="height: 220px; object-fit: cover;"> <img src="{{ product.main_image.url }}" class="card-img-top" alt="{% ml_field product 'name' %}" style="height: 260px; object-fit: contain; background: #f7f8fa; padding: 0.5rem;">
{% endif %} {% endif %}
<div class="card-body"> <div class="card-body">
<h5 class="card-title">{% ml_field product 'name' %}</h5> <h5 class="card-title">{% ml_field product 'name' %}</h5>
+61
View File
@@ -418,6 +418,67 @@ curl -I http://127.0.0.1/ --header "Host: www.tgoim.com"
### 10.1 更新代码 ### 10.1 更新代码
#### 方式一:后台一键部署(推荐)
网站后台已集成"在线部署"功能,可在浏览器中一键从 Git 拉取最新代码并自动完成更新,无需登录服务器。
**使用步骤:**
1. 登录后台管理 `https://www.tgoim.com/admin/`
2. 在左侧菜单进入「部署日志」(核心 → 部署日志)
3. 点击右上角「一键部署更新」按钮
4. 在确认对话框中点击「确定」
5. 页面会跳转到日志详情页,每 3 秒自动刷新显示部署进度
6. 部署完成后会自动重启服务
**部署流程会依次执行:**
1. `git pull origin main` 拉取最新代码
2. `pip install -r requirements.txt` 安装/更新依赖
3. `python manage.py migrate` 执行数据库迁移
4. `python manage.py collectstatic --noinput` 收集静态文件
5. `sudo supervisorctl restart tgoim` 重启服务
**前置条件(一次性配置):**
为了让运行 Django 的 `www-data` 用户能够执行 `git pull` 和重启服务,需要配置 sudoers 允许其无密码执行特定命令。在服务器上执行:
```bash
# 创建 sudoers 配置,允许 www-data 无密码执行 supervisorctl
sudo cat > /etc/sudoers.d/tgoim-deploy << 'EOF'
# 允许 www-data 无密码重启 tgoim 服务(用于在线部署)
www-data ALL=(root) NOPASSWD: /usr/bin/supervisorctl restart tgoim
www-data ALL=(root) NOPASSWD: /usr/bin/supervisorctl status tgoim
EOF
# 设置正确的权限
sudo chmod 440 /etc/sudoers.d/tgoim-deploy
# 验证配置语法(重要!避免 sudo 配置错误导致系统问题)
sudo visudo -c
```
**Git 仓库权限配置:**
确保 `www-data` 用户对项目目录有足够的权限执行 git pull:
```bash
# 设置项目目录所有者为 www-data
sudo chown -R www-data:www-data /opt/company_website
# 确保数据库和媒体目录可写
sudo chown www-data:www-data /opt/company_website/db.sqlite3
sudo chown -R www-data:www-data /opt/company_website/media
sudo chown -R www-data:www-data /opt/company_website/staticfiles
```
**注意事项:**
- 部署过程中请勿同时登录服务器手动执行 `git pull`,以免冲突
- 系统会自动防止并发部署(已有任务运行时按钮会禁用)
- 如果自动重启失败,日志会提示手动执行命令,按提示操作即可
- 所有部署操作会记录在「部署日志」中,可随时查看历史
#### 方式二:手动命令行部署
```bash ```bash
cd /opt/company_website cd /opt/company_website