2026-01-07
在 auto_execute_tasks.py 代码中新增功能,完成工作任务后,自动将生成的脚本和 n8n 工作流发布到生产服务器 192.168.3.143 上。
get_ssh_connection()deploy_script_to_production(local_script_path, remote_filename)deploy_n8n_workflow_to_production(workflow_file)/opt/dataops-platform/n8n/workflowsauto_deploy_completed_task(task_info)test_ssh_connection()系统会按以下优先级查找工作流文件:
n8n_workflow_*.json 文件datafactory/n8n_workflows/ 目录sync_completed_tasks_to_db() 函数在任务状态更新为 completed 后,自动触发部署:
# 自动部署到生产服务器(如果启用)
if ENABLE_AUTO_DEPLOY:
logger.info(f"🚀 开始自动部署任务 {task_id} 到生产服务器...")
if auto_deploy_completed_task(t):
logger.info(f"✅ 任务 {task_id} 已成功部署到生产服务器")
else:
logger.warning(f"⚠️ 任务 {task_id} 部署到生产服务器失败")
PRODUCTION_SERVER = {
"host": "192.168.3.143",
"port": 22,
"username": "ubuntu",
"password": "citumxl2357",
"script_path": "/opt/dataops-platform/datafactory/scripts",
"workflow_path": "/opt/dataops-platform/n8n/workflows",
}
ENABLE_AUTO_DEPLOY: bool = True # 默认启用自动部署
| 参数 | 类型 | 说明 |
|---|---|---|
--enable-deploy |
flag | 启用自动部署(默认) |
--no-deploy |
flag | 禁用自动部署 |
--deploy-now TASK_ID |
string | 立即部署指定任务 |
--test-connection |
flag | 测试 SSH 连接 |
# 测试连接
python scripts/auto_execute_tasks.py --test-connection
# 启动自动部署(默认)
python scripts/auto_execute_tasks.py --chat-loop --use-agent
# 禁用自动部署
python scripts/auto_execute_tasks.py --chat-loop --use-agent --no-deploy
# 手动部署指定任务
python scripts/auto_execute_tasks.py --deploy-now 123
sync_completed_tasks_to_db() 函数scripts/install_deploy_deps.py
scripts/test_deploy.py
docs/auto_deploy_guide.md
docs/auto_deploy_quick_reference.md
docs/CHANGELOG_auto_deploy.md
docs/auto_deploy_implementation_summary.md
README_AUTO_DEPLOY.md
paramiko>=2.7.0
pip install paramiko
或使用安装脚本:
python scripts/install_deploy_deps.py
┌─────────────────────────────────────────────────────────┐
│ 任务完成检测 │
│ (pending_tasks.json) │
└────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ 同步任务状态到数据库 │
│ (update_task_status → completed) │
└────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ 更新 DataFlow 节点 script_path │
│ (update_dataflow_script_path) │
└────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ 检查是否启用自动部署 │
│ (ENABLE_AUTO_DEPLOY) │
└────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ 建立 SSH 连接到生产服务器 │
│ (get_ssh_connection) │
└────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ 部署 Python 脚本文件 │
│ (deploy_script_to_production) │
│ • 创建远程目录 │
│ • 上传脚本文件 │
│ • 设置权限 755 │
└────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ 查找相关的 n8n 工作流文件 │
│ • 同目录 n8n_workflow_*.json │
│ • datafactory/n8n_workflows/*.json │
│ • 任务名称匹配 │
└────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ 部署工作流文件 │
│ (deploy_n8n_workflow_to_production) │
│ • 创建远程目录 │
│ • 上传工作流 JSON │
└────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ 记录部署日志 │
│ 关闭 SSH 连接 │
└─────────────────────────────────────────────────────────┘
| 函数名 | 行数 | 功能 |
|---|---|---|
get_ssh_connection() |
30 | SSH 连接 |
test_ssh_connection() |
50 | 连接测试 |
deploy_script_to_production() |
70 | 脚本部署 |
deploy_n8n_workflow_to_production() |
60 | 工作流部署 |
auto_deploy_completed_task() |
90 | 部署协调 |
python scripts/test_deploy.py
预期输出:
============================================================
🧪 自动部署功能测试套件
============================================================
✅ 所有测试通过 (5/5)
🎉 自动部署功能已就绪!
pip install paramikopython scripts/auto_execute_tasks.py --test-connectionpython scripts/test_deploy.pypython scripts/auto_execute_tasks.py --deploy-now <task_id>python scripts/auto_execute_tasks.py --chat-loop --use-agent本次实现完成了以下目标:
✅ 自动将完成的脚本部署到生产服务器 ✅ 自动查找并部署相关工作流文件 ✅ 提供灵活的控制选项 ✅ 完善的错误处理和日志记录 ✅ 详细的文档和测试工具
该功能已经可以投入使用,建议先在测试环境验证后再在生产环境启用。
如有问题或建议,请联系开发团队。
实现者: AI Assistant
审核者: 待定
最后更新: 2026-01-07