agentic_huge_data_base / wiki
页面 Dify · 11 开发与 CI/CD·DeepWiki 中文全文译文

11 · 开发与 CI/CD(Development and CI/CD)

应用编排与外部知识接入 · 本章是 Dify DeepWiki 中文译文的独立章节页,保留原始链接、源码锚点、模块标签和章节层级。

项目Dify 章节11 状态全文译文 模块测试、发布与运维、安装与启动、工作流与编排、界面与交互
源码线索
  • .devcontainer/Dockerfile
  • .devcontainer/README.md
  • .devcontainer/devcontainer.json
  • .devcontainer/post_create_command.sh
  • .devcontainer/post_start_command.sh
  • .github/CODE_OF_CONDUCT.md
  • .github/dependabot.yml
  • .github/labeler.yml
  • .github/pull_request_template.md
  • .github/workflows/api-tests.yml
模块标签
  • 测试、发布与运维
  • 安装与启动
  • 工作流与编排
  • 界面与交互
  • 配置治理

中文译文

开发与 CI/CD(中文译文)

原始 DeepWiki 页面:https://deepwiki.com/langgenius/dify/11-development-and-ci-cd
翻译时间:2026-05-27T08:44:28.194Z
翻译模型:deepseek-chat
原文字符数:12762
项目:Dify (dify)

--- 好的,作为一名资深技术文档翻译专家,我将严格遵循您的要求,将这份 DeepWiki 技术文档全文翻译为中文,并进行专业润色。

---

开发与持续集成/持续部署(CI/CD)

相关源文件

本 Wiki 页面的生成基于以下文件:

  • .devcontainer/Dockerfile
  • .devcontainer/README.md
  • .devcontainer/devcontainer.json
  • .devcontainer/post_create_command.sh
  • .devcontainer/post_start_command.sh
  • .github/CODE_OF_CONDUCT.md
  • .github/dependabot.yml
  • .github/labeler.yml
  • .github/pull_request_template.md
  • .github/workflows/api-tests.yml
  • .github/workflows/autofix.yml
  • .github/workflows/build-push.yml
  • .github/workflows/db-migration-test.yml
  • .github/workflows/deploy-agent-dev.yml
  • .github/workflows/deploy-dev.yml
  • .github/workflows/deploy-hitl.yml
  • .github/workflows/docker-build.yml
  • .github/workflows/labeler.yml
  • .github/workflows/main-ci.yml
  • .github/workflows/pyrefly-diff-comment.yml
  • .github/workflows/pyrefly-diff.yml
  • .github/workflows/stale.yml
  • .github/workflows/style.yml
  • .github/workflows/tool-test-sdks.yaml
  • .github/workflows/translate-i18n-claude.yml
  • .github/workflows/trigger-i18n-sync.yml
  • .github/workflows/vdb-tests-full.yml
  • .github/workflows/vdb-tests.yml
  • .github/workflows/web-tests.yml
  • .gitignore
  • .vite-hooks/pre-commit
  • .vscode/README.md
  • .vscode/launch.json.template
  • CONTRIBUTING.md
  • api/README.md
  • api/docker/entrypoint.sh
  • api/tasks/generate_summary_index_task.py
  • api/tasks/regenerate_summary_index_task.py
  • api/tasks/remove_document_from_index_task.py
  • api/tests/unit_tests/tasks/test_summary_queue_isolation.py
  • depot.json
  • dev/start-web
  • dev/start-worker
  • web/service/fetch.spec.ts

本文档涵盖了 Dify 平台的开发工作流、本地环境搭建、测试基础设施以及持续集成/持续部署(CI/CD)管线。它解释了如何搭建本地开发环境、运行测试,并理解每次代码变更时自动运行的质量检查。

关于将 Dify 部署到生产环境的信息,请参阅部署与运维。关于 Docker 构建过程和多阶段镜像的详细信息,请参阅 Docker 构建过程与多阶段镜像

---

开发环境概览

Dify 使用现代化的开发工具,针对 Python(后端)和 TypeScript(前端)开发进行了优化:

  • Python:使用 uv 包管理器,实现快速、可复现的依赖管理 api/README.md:7-10
  • 前端:使用 pnpm 进行高效的 Node.js 包管理 api/README.md:11-12
  • 数据库:PostgreSQL 或 MySQL(可配置)api/docker/entrypoint.sh:97-98
  • 缓存:Redis,用于会话管理和 Celery 消息代理 api/docker/entrypoint.sh:170
  • 容器化:Docker Compose,用于编排本地服务 api/README.md:25-29
  • 持续集成/持续部署(CI/CD):GitHub Actions,用于自动化测试和质量检查 .github/workflows/api-tests.yml:1-4

开发工作流同时支持容器化和原生开发,允许开发者选择最适合自己的工作方式。

来源:api/README.md:7-29, api/docker/entrypoint.sh:97-170, .github/workflows/api-tests.yml:1-4

---

本地开发环境搭建

Docker Compose 开发栈

主要开发环境使用 Docker Compose 来编排所有必需的服务。该栈在几个关键文件中定义:

graph TB
    Template["docker-compose-template.yaml<br/>服务定义"]
    EnvExample[".env.example<br/>环境变量"]
    Generator["generate_docker_compose<br/>Python 脚本"]
    FinalCompose["docker-compose.yaml<br/>生成的文件"]
    MiddlewareCompose["docker-compose.middleware.yaml<br/>独立服务"]

    Template --> Generator
    EnvExample --> Generator
    Generator --> FinalCompose

    FinalCompose --> Services["服务栈"]
    MiddlewareCompose --> Services

    Services --> API["api<br/>Flask API 服务器"]
    Services --> Worker["worker<br/>Celery Worker"]
    Services --> Beat["worker_beat<br/>Celery Beat"]
    Services --> Web["web<br/>Next.js 前端"]
    Services --> DB["db_postgres/db_mysql<br/>主数据库"]
    Services --> Redis["redis<br/>缓存与消息代理"]

关键文件

  • docker/generate_docker_compose:将模板和环境变量合并,生成最终的 compose 文件 .github/workflows/autofix.yml:31-34
  • docker/docker-compose.middleware.yaml:在本地开发和测试期间使用的独立中间件服务 .devcontainer/post_create_command.sh:13

来源:.github/workflows/autofix.yml:31-34, .devcontainer/post_create_command.sh:13

环境配置

环境变量通过 .env 文件管理。仓库提供了模板和自动化脚本用于设置:

配置文件用途关键变量
docker/.env.exampleDocker Compose 环境DB_TYPE, DB_HOST, REDIS_HOST
api/.env.exampleAPI 服务配置SECRET_KEY, CONSOLE_API_URL, 存储设置
docker/middleware.env中间件服务配置数据库凭据和 Redis 设置

详情请参阅开发环境搭建

来源:api/README.md:23-24, .devcontainer/post_create_command.sh:13-14

---

使用 UV 进行 Python 开发

UV 包管理器

Dify 使用 uv 进行 Python 依赖管理,替代了 poetry 以获得更佳性能:

graph LR
    PyProject["pyproject.toml<br/>依赖与配置"]
    UVLock["uv.lock<br/>锁定版本"]

    PyProject --> UVSync["uv sync<br/>安装依赖"]
    UVLock --> UVSync

    UVSync --> VirtualEnv[".venv<br/>虚拟环境"]

    VirtualEnv --> DevGroups["依赖组"]

    DevGroups --> Dev["dev<br/>pytest, ruff, mypy"]

关键 UV 命令

# 安装所有依赖,包括开发组
uv sync --project api --dev

# 本地运行测试
uv run pytest

来源:api/README.md:7-10, api/README.md:86-88, api/README.md:94-96

---

测试基础设施

测试套件架构
graph TB
    subgraph "测试类型"
        UnitTests["单元测试<br/>pytest / Vitest"]
        IntegrationTests["集成测试<br/>Docker 服务"]
        VDBTests["向量数据库冒烟测试<br/>Chroma, PGVector, Qdrant, Weaviate"]
        MigrationTests["迁移测试<br/>PostgreSQL & MySQL"]
    end

    subgraph "Python 测试"
        Pytest["pytest<br/>测试运行器"]
        PytestXdist["pytest-xdist<br/>并行执行"]
        PytestCov["coverage<br/>覆盖率报告"]

        Pytest --> PytestXdist
        Pytest --> PytestCov
    end

    UnitTests --> Pytest
    IntegrationTests --> Pytest
    VDBTests --> Pytest
    MigrationTests --> Pytest
API 测试执行

本地运行测试

# 运行所有 API 测试
uv run pytest

# 运行特定类型测试
uv run pytest api/tests/unit_tests/
uv run pytest api/tests/integration_tests/

持续集成(CI)配置

  • API 单元测试在 depot-ubuntu-24.04 上运行,使用 pytest-xdist 进行并行化 .github/workflows/api-tests.yml:17-59
  • API 集成测试使用完整的中间件栈,并验证工作流和工具 .github/workflows/api-tests.yml:112-121
  • 向量存储冒烟测试验证了 chromapgvectorqdrantweaviate 的集成 .github/workflows/vdb-tests.yml:61-67

详情请参阅测试基础设施与策略

来源:api/README.md:94-96, .github/workflows/api-tests.yml:17-121, .github/workflows/vdb-tests.yml:61-67

---

持续集成/持续部署(CI/CD)管线架构

GitHub Actions 工作流结构

CI 管线使用智能路径过滤,仅根据变更的文件运行相关测试:

graph TB
    PRPush["拉取请求或推送"]

    PRPush --> MainCI["主编排器"]

    MainCI --> StyleCheck["代码风格检查<br/>style.yml"]
    MainCI --> APITests["运行 Pytest<br/>api-tests.yml"]
    MainCI --> WebTests["Web 测试<br/>web-tests.yml"]
    MainCI --> VDBTests["向量数据库冒烟测试<br/>vdb-tests.yml"]

    StyleCheck --> PyStyle["Python 风格<br/>Ruff, Mypy, Import Linter"]
    StyleCheck --> WebStyle["Web 风格<br/>ESLint, tsslint, knip"]
    StyleCheck --> SuperLinter["SuperLinter<br/>Bash, YAML, Dockerfile"]
管线优化
  • 并发控制:工作流使用并发组,当有新提交推送时取消正在进行的运行 .github/workflows/api-tests.yml:12-14
  • 路径过滤:像 style.yml 这样的工作流使用 tj-actions/changed-files,如果相关目录(例如 api/web/)未变更,则跳过任务 .github/workflows/style.yml:26-33
  • 分片:Web 测试在多个运行器上进行分片,以减少执行时间 .github/workflows/web-tests.yml:22-26

详情请参阅CI 管线架构

来源:.github/workflows/api-tests.yml:12-14, .github/workflows/style.yml:26-33, .github/workflows/web-tests.yml:22-26

---

代码质量与代码检查

Python 代码质量

Dify 使用多种工具来维护后端代码质量:

  • Ruff:快速的代码检查器和格式化工具 api/README.md:100-101
  • Mypy:通过 make type-check-core 进行静态类型检查 .github/workflows/style.yml:52
  • Import Linter:通过检查模块依赖关系来强制实施架构边界 .github/workflows/style.yml:48
  • Dotenv Linter:验证 .env.example 文件之间的一致性 .github/workflows/style.yml:56
前端代码质量
  • ESLint:使用自定义配置进行 JavaScript/TypeScript 代码检查 .github/workflows/style.yml:107
  • tsslint:TypeScript 专用的代码检查 .github/workflows/style.yml:114
  • Knip:检测 web 目录中未使用的文件、依赖和导出 .github/workflows/style.yml:124

详情请参阅代码质量与代码检查

来源:api/README.md:100-101, .github/workflows/style.yml:48-124

---

自动修复与代码现代化

autofix.ci 工作流会自动对拉取请求应用修复:

  • Ruff 修复:自动修复代码检查违规并格式化代码 api/README.md:100
  • ast-grep:执行结构性代码重写,例如将 SQLAlchemy 查询语法从 .filter() 现代化为 .where() .github/workflows/autofix.yml:89-90,以及将 Optional[T] 转换为 T | None .github/workflows/autofix.yml:94-110
  • ESLint 自动修复:使用 eslint --fix 自动修复前端风格问题 .github/workflows/autofix.yml:132

详情请参阅自动修复与代码现代化

来源:.github/workflows/autofix.yml:89-132, api/README.md:100

---

AI 编码代理技能

仓库包含针对 AI 编码助手的专门自动化功能:

  • 翻译同步:使用 Claude 根据变更的键,将 i18n 文件与英文源文件同步 .github/workflows/translate-i18n-claude.yml:1-170
  • 代码审查技能:对拉取请求进行自动化检查,以确保与仓库模式的一致性。

详情请参阅AI 编码代理技能

来源:.github/workflows/translate-i18n-claude.yml:1-170