lang: zh slug: dagger title: ‘Dagger:具有 15K+ Stars 的可编程 CI/CD’ description: ‘Dagger 是一个可编程 CI/CD 引擎,可在容器中运行管道。 与 Docker、Go、Python、TypeScript 兼容。 涵盖 Dagger 设置、教程、VS GitHub Actions 以及生产强化。’ tags: [“automation”, “ci-cd”, “github”, “open-source”] date: 2026-05-19 00:00:00+08:00 lastmod: 2026-05-19 00:00:00+08:00 tech_stack: [] application_domain: Dev Utils source_version: ’' licensing_model: Open Source license_type: Apache-2.0 file_size: ’' file_md5: ’' download_url: ’' backup_url: ’' github_repo: ‘https://github.com/dagger/dagger' last_maintained: ‘2026-05-19’ draft: false categories: [‘dev-utils’] aliases:- /帖子/匕首/ 常见问题解答:

  • q:“您可以使用哪些编程语言来编写 Dagger 管道?” featureImage: /images/articles/dagger-15k-stars-cicd.png a: ‘Dagger 提供了八种语言的第一方 SDK:Go、Python、TypeScript、PHP、Java、.NET、Elixir 和 Rust。 Go、Python 和 TypeScript SDK 是最成熟且使用最广泛的。
  • q:“Dagger 可以完全取代 GitHub Actions 这样的 CI 服务器吗?” ’ ‘不。 Dagger 是一个管道执行引擎,而不是 CI 编排平台,因此它无法自行触发推送、拉取请求或调度运行。 您仍然需要 GitHub Actions、GitLab CI、Jenkins 或 cron 作业来调用 Dagger 管道; Dagger 取代了 YAML 作业定义,而不是调度程序。”
  • q: ‘Dagger 缓存与 Docker 层缓存有何不同?’ a: ‘Dagger 在单个操作级别使用内容寻址缓存,这比 Docker 层缓存更细粒度。 如果您更改一个源文件,Dagger 只会重新运行依赖于该文件的操作,而 Docker 层缓存会使更改后的所有层失效。
  • q: ‘如何在 Linux 上安装 Dagger CLI?’ a: ‘运行官方安装脚本:curl -fsSL https://dl.dagger.io/dagger/install.sh | BIN_DIR=/usr/local/bin sh. 您可以通过设置 DAGGER_VERSION 来固定版本,然后使用 dagger 版本进行确认。 Dagger 还需要在本地运行 Docker Engine 24.0+ 或 Podman 4.0+。
  • q: ‘Dagger 需要什么容器运行时?’ a: ‘Dagger 需要 Linux 容器运行时:Docker Engine 24.0+、Podman 4.0+、containerd 或任何符合 OCI 的运行时。 在 macOS 和 Windows 上,需要 Docker Desktop 或 Podman Desktop,并且支持无根 Docker 和 Podman,但有一些配置注意事项。 特征图片:/images/articles/dagger-programmable-ci-cd-with-15kplus-stars-comparison.png——{{< 资源信息 >}}## 介绍开发人员平均每天推送 3.2 次提交,并等待每次 CI 运行完成 12 分钟。 如果将这个数字乘以 20 名工程师的团队,您每天的集体空闲时间将达到 4 小时——这一切都是因为 CI/CD 管道无法在本地进行测试,被锁定为特定于供应商的 YAML 语法,并且在环境之间会出现不可预测的故障。Dagger 由 Solomon Hykes(Docker 联合创始人)创建,采用了不同的方法:它将 CI/CD 管道视为常规代码。 您无需费力使用 GitHub Actions YAML 或 GitLab CI DSL,而是使用 Go、Python 或 TypeScript 编写自动化。 该管道使用与 Docker 相同的 BuildKit 引擎在容器内运行,从而在笔记本电脑上和生产环境中提供相同的行为。截至 2026 年 5 月,Dagger 拥有 15,829 个 GitHub star、878 个分支和 304 个贡献者,已成为传统 CI/CD 的重要替代品。 本 Dagger 教程 介绍了完整的 Dagger 设置,将其与 GitHub Actions、GitLab CI 和 Jenkins 进行了正面比较,并涵盖了您现在可以部署的生产强化模式。
    Dagger CI/CD 引擎架构
    ## 什么是匕首?一个可编程 CI/CD 引擎,可在 OCI 容器内执行自动化管道,让开发人员可以使用通用编程语言(而不是 YAML 或专有 DSL)定义构建、测试和部署逻辑。### 核心价值主张Dagger 体现了 CI/CD 即代码(也称为 cicd 即代码)理念 — 您的构建、测试和部署逻辑与您的应用程序位于同一存储库中,并使用您的团队已使用的相同语言编写。- 一次编写,随处运行:相同的管道在开发人员的笔记本电脑、GitHub Actions、GitLab CI 或裸机服务器上以相同的方式执行。
  • 语言原生 SDK:对 Go、Python、TypeScript、PHP、Java、.NET、Elixir 和 Rust 的一流支持 - 具有自动完成、类型检查和单元测试功能。
  • 容器本机执行:每个步骤都在隔离的容器中运行,消除了“在我的机器上运行”的差异。
  • 智能缓存:操作级别的内容寻址缓存意味着未更改的步骤永远不会重新运行。
  • 默认情况下可观察:每个操作都会发出 OpenTelemetry 跟踪,可在终端中查看或导出到 Jaeger、Honeycomb 或任何 OTel 后端。
    Dagger SDK 和语言
    ## 匕首的工作原理### 架构概述Dagger 的架构由四层组成:1. 您的管道代码 (Go / Python / TypeScript) — 使用 Dagger 的 SDK 定义逻辑。
  1. Dagger SDK — 从本机函数调用生成 GraphQL 查询。
  2. Dagger Engine — 一个基于 BuildKit 的容器运行时,用于执行管道图。
  3. 容器运行时 — Docker、Podman 或任何托管引擎的符合 OCI 的运行时。┌──────────────────────────────────────────────────────────────┐ │ 管道代码 (Go/Python/TS) │ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │ │ 构建 │ │ 测试 │ │ 部署 │ │ │ └────┬──────┘ └────┬──────┘ └────┬──────┘ │ └────────┼────────────┼──────────────┼────────────────────────┘ │ │ │ ▼ ▼ ▼ ┌──────────────────────────────────────────────────────────────┐ │ Dagger SDK (GraphQL 客户端) │ └──────────────────────┬────────────────────────────────────┘ │ ▼ ┌──────────────────────────────────────────────────────────────┐ │ Dagger 引擎 (BuildKit) │ │ ┌──────────────────────────────────────────────────────┐ │ │ │ DAG 执行器 → 缓存层 → 容器操作 │ │ │ └──────────────────────────────────────────────────────┘ │ └──────────────────────┬────────────────────────────────────┘ │ ▼ ┌──────────────────────────────────────────────────────────────┐ │ Docker / Podman / OCI 运行时 │ └──────────────────────────────────────────────────────────────┘### 执行模型当您运行 Dagger 管道时,SDK 会将您的函数调用转换为操作的有向无环图 (DAG)。 DAG 中的每个节点代表一个容器操作:拉取映像、复制文件、运行命令或导出工件。 Dagger 引擎通过自动并行性安排这些操作并缓存每个中间结果。````
DAG执行流程示例 #

拉取基础镜像 ──┬── 安装 deps ──┬── 运行测试 ──┬── 导出二进制文件 │ │ │ └── 缓存命中? 跳过└──缓存命中? └── 缓存命中?

|---------|-------------|
| **Module** | A reusable package of Dagger functions defined in a `dagger.json` manifest |
| **Function** | A typed, sandboxed operation that accepts inputs and produces outputs |
| **Directory** | A content-addressed filesystem tree passed between functions |
| **Container** | An OCI container image or running container manipulated through the API |
| **Secret** | A secure value (token, key) that is never logged or exposed in traces |
| **Service** | A long-running container exposed as a network endpoint for integration tests |## 安装和设置### 先决条件- Docker Engine 24.0+ 或 Podman 4.0+ 在本地运行
 - 可通过默认套接字访问的容器运行时### 安装 Dagger CLI**macOS(自制):**````
bas
h
# 通过 Homebrew Tap 安装
 酿造安装匕首/点击/匕首# 验证安装
 匕首版
 # 预期:dagger v0.19.7 (registry.dagger.io/engine:v0.19.7)
 ````**Linux:**````
bas
h
# 使用官方安装脚本安装
 卷曲-fsSL https://dl.dagger.io/```
 # DAG执行流程示例
 拉取基础镜像 ──┬── 安装 deps ──┬── 运行测试 ──┬── 导出二进制文件
 │ │ │
 └── 缓存命中? 跳过└──缓存命中? └── 缓存命中? 
``Windows:**````
powershel
l
# 通过勺子安装
 铲斗添加匕首 https://github.com/dagger/scoop-bucket
 舀安装匕首# 验证
 匕首版
 ````### 初始化你的第一个项目````
bas
h
# 创建一个新的 Dagger 模块
 dagger init --sdk=python --source=./dagger my-pipeline# 或者使用 Go
 dagger init --sdk=go --source=./dagger my-pipeline# 或者使用 TypeScript
 dagger init --sdk=typescript --source=./dagger my-pipeline# 该命令创建:
 # ├── 匕首/
 # │ └── src/main.py(或main.go,或index.ts)
 # ├── dagger.json
 # └── .gitignore
 ````### 快速本地测试````蟒蛇
# dagger/src/main.py — 最小的 Dagger 管道
 进口匕首
 从 dagger 导入 dag、函数、object_type@object_type
class MyPipeline:
    @function
    async def hello(self, name: str = "World") -> str:
        return await dag.container()
            .from_("alpine:latest")
            .with_exec(["echo", f"Hello, {name}!"])
            .stdout()

bas h

在本地运行该函数 #

匕首打招呼 –name=“Dagger”# 输出:

你好,``` #

bas h

通过 Homebrew Tap 安装 #

酿造安装匕首/点击/匕首

验证安装 #

匕首版

预期:dagger v0.19.7 (registry.dagger.io/engine:v0.19.7) #

``Docker 生态系统。 以下是构建、标记和推送 Docker 映像的完整管道:````蟒蛇

dagger/src/main.py — 构建并推送 Docker 镜像 #

进口匕首 来自 dagger``` bas h

使用官方安装脚本安装 #

卷曲-fsSL https://dl.dagger.io/dagger/install.sh | BIN_DIR=/usr/local/bin sh

或者使用 sudo 进行系统范围的安装 #

卷曲-fsSL https://dl.dagger.io/dagger/install.sh |
DAGGER_VERSION=0.19.7 BIN_DIR=/usr/local/bin sudo -E sh

验证 #

匕首版

图像 = 等待 dag.container()
.build(源,dockerfile =“Dockerfile”)        # Publish to a container registry
       digest = await image.with_registry_auth(registry, username, password)
           .publish(f"{registry}/{repository}:```
powershel
l
# Install via scoop
scoop bucket add dagger https://github.com/dagger/scoop-bucket
scoop install dagger

# Verify
dagger version
``` --username=$GITHUB_USER \
 --password=env:GITHUB_TOKEN \
 --repository=my-org/my-app \
 --tag=v1.2.3
```### Go SDK — Full CI Pipeline``走吧
// dagger/main.go — 基于 Go 的 ```
bas
h
# 创建一个新的 Dagger 模块
dagger init --sdk=python --source=./dagger my-pipeline

# 或者使用 Go
dagger init --sdk=go --source=./dagger my-pipeline

# 或者使用 TypeScript
dagger init --sdk=typescript --source=./dagger my-pipeline

# 该命令创建:
# ├── 匕首/
# │ └── src/main.py(或main.go,或index.ts)
# ├── dagger.json
# └── .gitignore

iner ()。 来自(“golang:1.24-alpine”)。 WithMountedDirectory("/src", 源). WithWorkdir("/src")// 运行 linting lint := builder.WithExec([]string{“go”, “vet”, “./…”}) // Run tests with race detection tested := lint.WithExec([]string{ “go”, “test”, “-race”, “-coverprofile=coverage.out”, “./…”, })// 构建二进制文件

# dagger/src/main.py — 最小的 Dagger 管道
进口匕首
从 dagger 导入 dag、函数、object_type

@对象类型
类我的管道:
@函数
async def hello(self, name: str = "World") -> str:
返回等待 dag.container()
.from_("高山:最新")
.with_exec(["echo", f"你好,{name}!"])
.stdout()
````蟒蛇
# dagger/src/main.py — 与 PostgreSQL 服务的集成测试
进口匕首
从 dagger 导入 dag、函数、object_type、目录、服务@对象类型
测试管道类:
@函数
async def Integration_test(self, source: Directory) -> str:
# 启动 PostgreSQL 服务容器
postgres = dag.service(
dag.contain```
bas
h
# 在本地运行该函数
匕首打招呼 --name="Dagger"

# 输出:
# 你好,匕首! 
````)
.with_env_variable("POSTGRES_PASSWORD", "测试")
.with_env_variable("POSTGRES_DB", "testdb")
.with_expose_port(5432)
)# 针对数据库运行集成测试
测试结果 = 等待 (
dag.container()
.from_("python:3.12-slim")
.with_mounted_directo```
pytho
n
# dagger/src/main.py — 构建并推送 Docker 镜像
进口匕首
从 dagger 导入 dag、函数、object_type、目录

@对象类型
CiPipeline 类:
@函数
异步 def build_and_push(
自我,
来源:目录,
注册表:str,
用户名:str,
密码:dagger.Secret,
存储库:str,
标签:str =“最新”
) -> 字符串:
# 从源目录中的 Dockerfile 构建容器
图像 = 等待 dag.container()
.build(源,dockerfile =“Dockerfile”)

# 发布到容器注册表
摘要=等待image.with_registry_auth(注册表,用户名,密码)
.publish(f"{注册表}/{存储库}:{标签}")

返回摘要
```
rm
64"];
const image = dag.container().build(source);const 摘要 = 等待 Promise.all(
platforms.map(异步(平台)=> {
返回等待图像
.platform(平台)
.publish(`ghcr.io/my-org/my-app:${platform.replace("/", "-")}`);
})
);返回摘要;
}
}
````## 基准/实际用例### 缓存性能Dagger 的内容寻址缓存比传统 CI 系统提供了可测量的加速。 在受控基准测试中,在 10 次连续运行中构建 Go 微服务(大约 50 个依赖项):![Dagger 缓存性能比较](https://dagger.io/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Fdagger-call.d6f2````
bas
h
# 运行构建和推送功能
dagger 调用 build-and-push \
--来源=。 \
--registry=ghcr.io \
--用户名=$GITHUB_USER \
--password=env:GITHUB_TOKEN \
--repository=my-org/my-app \
--标签=v1.2.3
``` 的 | 3m 48s | 4m 05s | 4m 05s |
| 第二次运行(没有代码更改)| 3m 55s | 3m 30 秒 | 8 秒 | 8 秒 |
| 仅依赖项更改 | 4m 05s | 3m 42 秒 | 1m 15 秒 | 1m 15 秒 |
| 仅限源代码更改 | 3m 50 秒 | 3 分 35 秒 | 45 秒 | 45 秒 |关键洞察:GitHub Actions 和 Gi```
g
o
// dagger/main.go — 基于 Go 的 CI 管道和测试
匕首“dagger.io/dagger”

导入(
“上下文”
“FMMT”
“操作系统”
)

类型 CiPipeline 结构体{}

// Run 执行完整的 CI 管道:lint → 测试 → 构建
func (m *CiPipeline) Run(ctx context.Context, source *dagger.Directory) (*dagger.File, error) {
// 定义一个安装了源代码的 Go 构建器容器
构建器:= dag.Container()。 
来自(“golang:1.24-alpine”)。 
WithMountedDirectory("/src", 源).
WithWorkdir("/src")

// 运行 linting
lint := builder.WithExec([]string{"go", "vet", "./..."})

// 运行带有竞争检测的测试
测试过 := lint.WithExec([]string{
“去”,“测试”,“-race”,“-coverprofile=coverage.out”,“./...”,
})

// 构建二进制文件
二进制:=测试。WithExec([]字符串{
“去”,“构建”,“-ldflags=-s -w”,“-o”,“bin/myapp”,“./cmd/myapp”,
})

// 将构建的二进制文件提取为文件
返回binary.File("/src/bin/myapp"), nil
}
```
s
t
构建
- 云部署:AWS、GCP、Azure、Fly.io
- 安全扫描:Trivy、Snyk、SLSA 验证
- 测试:k6 负载测试、Playwright 浏览器测试````
bas
h
# 安装并使用 Daggerverse 的模块
dagger -m github.com/kpenfound/blueprints/go 调用构建 \
--来源=。 --args=./cmd/myapp# 列出已安装的模块
dagger 模块使用 github.com/Dudesons/daggerverse/node
````## 高级用法/生产强化### 秘密管理切勿将秘密作为普通字符串传递。 Dagger 的“Secret”类型确保敏感值在日志和跟踪中被屏蔽:````蟒蛇
进口匕首
from dagger import dag, 函数, object_type, Secret@对象类型
类安全管道:
@函数
异步定义部署(
自我,
kubeconfig:秘密,
图像摘要:str
) -> 字符串:
返回等待(
dag.container()
.from_("bitnami/kubectl:最新")
.with_mounted_secret("/root/.kube/config", kubeconfig)
.with_ex```
bas
h
# 从项目根运行 Go 管道
dagger 调用 run --source=. -o ./bin/myapp
``` f"myapp={image_digest}",
“--命名空间=生产”
])
.stdout()
)

bas h

传递秒``` #

pytho n

dagger/src/main.py — 与 PostgreSQL 服务的集成测试 #

进口匕首 从 dagger 导入 dag、函数、object_type、目录、服务

@对象类型 测试管道类: @函数 async def Integration_test(self, source: Directory) -> str:

启动 PostgreSQL 服务容器 #

postgres = dag.service( dag.container() .from_(“postgres:16-alpine”) .with_env_variable(“POSTGRES_USER”, “测试”) .with_env_variable(“POSTGRES_PASSWORD”, “测试”) .with_env_variable(“POSTGRES_DB”, “testdb”) .with_expose_port(5432) )

针对数据库运行集成测试 #

测试结果 = 等待 ( dag.container() .from_(“python:3.12-slim”) .with_mounted_directory("/app", 源) .with_service_binding(“db”, postgres) .with_env_variable(“DATABASE_URL”, “postgresql://test:test@db:5432/testdb”) .with_workdir(“/应用程序”) .with_exec([“pip”, “安装”, “-r”, “requirements-test.txt”]) .with_exec([“pytest”, “测试/集成”, “-v”, “–tb=short”]) .stdout() )

返回测试结果

ted
_directory("/src", 源)
.with_workdir("/src")
.with_exec(["执行", "测试", "-short", "./..."])
.stdout()async def security_scan(self, source: Directory) -> str:
返回等待 dag.container()
.from_("aquasec/trivy:最新")
.with_mounted_directory("/src", 源)
.with_workdir("/src")
.with_exec(["琐碎", "fs", "--scanners=vuln", "."])
.stdout()
````### Monitoring with OpenTelemetryDagger 为每个操作发出 OpenTelemetry 跟踪。 将它们导出到后端以实现管道可观察性:````
bas
h
# 使用 OTel 导出到 Jaeger 运行
导出 OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4317
导出 OTEL_EXPORTER_OTLP_PROTOCOL=grpcdagger 调用 run --source=. --otel-export=自动# 在 Jaeger UI 中查看 http://localhost:16686 中的跟踪
````### CI 集成 — GitHub 操作````
yam
l
# .github/workflows/dagger.yml
名称:匕首CI上:[推,拉请求]职位:
词:
运行:ubuntu-latest
步骤:
- 使用:actions/checkout@v4- 名称:运行 Dagger 管道
使用:dagger/dagger-for-github@v7
与:
版本:“0.19.7”
动词:呼叫
模块: .
参数:运行--source=.
环境:
``打字稿
// dagger/src/index.ts — 多平台容器构建
从“@dagger.io/dagger”导入 { dag, function, objectType, Directory };

@对象类型
类构建管道{
@函数
异步 multiPlatformBuild(来源:目录): Promise<string[]> {
const 平台 = ["linux/amd64", "linux/arm64"];
const image = dag.container().build(source);

const 摘要 = 等待 Promise.all(
platforms.map(异步(平台)=> {
返回等待图像
.platform(平台)
.publish(`ghcr.io/my-org/my-app:${platform.replace("/", "-")}`);
})
);

返回摘要;
}
}

e ‘) { 步骤{ 嘘’’’ 卷曲-fsSL https://dl.dagger.io/dagger/install.sh |
BIN_DIR=/usr/local/bin sh dagger 调用 run –source=. ’’’ } } } }

|---------|--------|---------------|-----------|---------|
| **Pipeline Definition** | Go/Python/TypeScript code | YAML workflows | YAML `.gitlab-ci.yml` | Groovy/Java DSL |
| **Local Execution** | Native — identical to CI | Not supported (act is partial) | Limited (`gitlab-runner exec`) | Full support |
| **Caching Granularity** | Per-operation (content-addressed) | Key-value + Docker layer cache | Key-value + cache layers | Plugin-dependent |
| **Vendor Lock-in** | None — runs on any CI | GitHub-only for orchestration | GitLab-only for orchestration | None (self-hosted) |
| **Learning Curve** | Moderate (requires Go/TS/Py) | Low (YAML + marketplace) | Low-Medium (YAML + DSL) | High (Groovy complexity) |
| **Ecosystem Size** | 800+ Daggerverse modules | 20,000+ Actions in Marketplace | Built-in integrations + partners | 1,800+ plugins |
| **Container-Native** | Yes — core design principle | Via Docker actions | Via Docker executor | Via Docker plugin |
| **Parallel Execution** | Automatic DAG scheduling | Job-level + matrix builds | DAG + parallel stages | Pipeline parallel stages |
| **Observability** | Built-in OpenTelemetry traces | Basic logging + third-party | Built-in metrics + monitoring | Plugin-dependent |
| **Pricing (2026)** | Free (OSS) / $50/mo Team | $4/user/mo (Team) | $29/user/mo (Premium) | Free (self-hosted infra) |### 何时选择每种工具**在以下情况下选择 Dagger:**
- 推送前需要在本地测试 CI/CD 管道
- 管道复杂性超过约 50 行 YAML
- 您的团队已经使用 Go、Python 或 TypeScript 进行工作
- 避免供应商锁定是战略重点**在以下情况下选择 GitHub Actions:**
- 您的存储库已经在 GitHub 上
- 工作流程简单并受益于市场行动
- 您的团队更喜欢最小的配置开销**在以下情况下选择 GitLab CI:**
- 您使用 GitLab 作为您的主要 DevOps 平台
- 您需要内置的安全扫描和合规功能
- 复杂作业依赖关系的 DAG 管道执行至关重要**在以下情况下选择 Jenkins:**
- 您需要最大的插件灵活性
- 需要具有完全控制能力的自托管基础设施
- 您拥有 Groovy 内部专业知识## 局限性/诚实评估Dagger 并不是适合所有情况的工具。 这是它做得不好的地方:````
bas
h
# 安装并使用 Daggerverse 的模块
dagger -m github.com/kpenfound/blueprints/go 调用构建 \
--来源=。 --args=./cmd/myapp

# 列出已安装的模块
dagger 模块使用 github.com/Dudesons/daggerverse/node
``` 更小。** 相对于 20,000 多个 GitHub Actions,大约有 800 个 Daggerverse 模块,您有时需要编写已经作为单行 Action 步骤存在的功能。**没有内置的 CI 调度。** Dagger 是管道执行器,而不是 CI 服务器。 您仍然需要 GitHub Actions、GitLab CI 或其他触发机制来按推送、PR 或计划运行管道。**复杂的调试。** 当 Dagger p````
pytho
n
进口匕首
from dagger import dag, 函数, object_type, Secret

@对象类型
类安全管道:
@函数
异步定义部署(
自我,
kubeconfig:秘密,
图像摘要:str
) -> 字符串:
返回等待(
dag.container()
.from_("bitnami/kubectl:最新")
.with_mounted_secret("/root/.kube/config", kubeconfig)
.with_exec([
“kubectl”、“设置”、“图像”、
“部署/myapp”,
f"myapp={image_digest}",
“--命名空间=生产”
])
.stdout()
)
``d Rust。 Go、Python 和 TypeScript SDK 是最成熟且使用最广泛的。 每个 SDK 都是根据 Dagger 的 GraphQL 架构生成的,为您提供完整的类型安全性和 IDE 自动完成功能。**问:Dagger 可以完全取代我的 CI 服务器吗?**
不。Dagger 是一个管道执行引擎,而不是 CI 编排平台。 您仍然需要一个触发机制 - GitHub Actions、GitLab CI、Jenkins 或 cron 作业 - 来在推送、拉取请求或计划运行等事件上调用 Dagger 管道。 Dagger 取代了基于 YAML 的作业定义,而不是 CI 调度程序本身。**问:Dagger 缓存与 Docker 层缓存相比如何?**
达格````
bas
h
# 从环境变量传递秘密
匕首调用部署\
--kubeconfig=文件:$HOME/.kube/config \
--image-digest=ghcr.io/my-org/my-app@sha256:abc123...
``取决于该文件。 Docker 层缓存会使更改后的所有层失效。 实际上,这意味着 Dagger 缓存命中更频繁,重建速度更快。**问:Dagger 适合大型 monorepos 吗?**
是的,但有注意事项。 Dagger 的内容寻址缓存在 monorepos 中运行良好,因为 unchange```
pytho
n
导入异步
从 dagger 导入 dag、函数、object_type、目录

@对象类型
并行管道类:
@函数
async def run_parallel(self, 来源: Directory) -> list[str]:
# 这三个操作自动并行运行
结果=等待 asyncio.gather(
self.lint(来源),
self.unit_tests(来源),
self.security_scan(来源),
)
返回列表(结果)

async def lint(self, 来源: 目录) -> str:
返回等待 dag.container()
.from_("golangci/golangci-lint:最新")
.with_mounted_directory("/src", 源)
.with_workdir("/src")
.with_exec(["golangci-lint", "运行", "--timeout=5m"])
.stdout()

async def unit_tests(self, source: Directory) -> str:
返回等待 dag.container()
.from_("golang:1.24")
.with_mounted_directory("/src", 源)
.with_workdir("/src")
.with_exec(["执行", "测试", "-short", "./..."])
.stdout()

async def security_scan(self, source: Directory) -> str:
返回等待 dag.container()
.from_("aquasec/trivy:最新")
.with_mounted_directory("/src", 源)
.with_workdir("/src")
.with_exec(["琐碎", "fs", "--scanners=vuln", "."])
.stdout()
``用例。## 结论Dagger 为 CI/CD 带来了一种根本不同的方法:管道作为真实代码,在容器中执行,在每台机器上具有相同的行为。 15,829 颗 GitHub 星星反映了开发者社区厌倦了 YAML 调试和特定于供应商的锁定。对于使用 Go、Python 或 TypeScript 工作的团队来说,Dagger 消除了在应用程序代码和 CI 配置之间切换的认知开销。 仅本地优先执行模型(在笔记本电脑上 8 秒内运行 4 分钟的 CI 管道)就证明了团队每天多次迁移的迁移工作的合理性。### 行动项目1.安装Dagger CLI:`brew install dagger/tap/dagger`
2. 运行快速入门:`dagger init --sdk=python --source=./dagger my-pipeline`
3. 首先移植你的构建作业——保留现有的 CI 作为触发层
4. 加入 [Discord](https://discord.com/invite/dagger-io) 上的 Dagger 社区以获得支持
5. 探索 [Daggerverse](https://daggerverse.dev) 以获取可重用模块关注 dibi8 DevOps Telegram 小组,了解每周 CI/CD 工具更新和生产部署模式:**https://t.me/dibi8dev**







## 推荐的托管和基础设施在将上述任何工具部署到生产环境之前,您需要坚实的基础设施。 dibi8实际使用和推荐的两个选项:- **{< aff "digitalocean" "footer-cta-legacy" "DigitalOcean" >}}** — 200 美元免费赠金,为期 60 天,覆盖全球 14 个以上区域。 运行开源人工智能工具的独立开发者的默认选项。 
- **{```
bas
h
# 使用 OTel 导出到 Jaeger 运行
导出 OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4317
导出 OTEL_EXPORTER_OTLP_PROTOCOL=grpc

dagger 调用 run --source=. --otel-export=自动

# 在 Jaeger UI 中查看 http://localhost:16686 中的跟踪
```帮助
dibi8.com 保持运行。*## 资料来源和进一步阅读- [Dagger 官方文档](https://docs.dagger.io/)
- [Dagger GitHub 存储库](https://github.com/dagger/dagger) — 15.8k 星,Apache-2.0
- [Daggerverse 模块注册表](https://daggerverse.dev)
- [达格```
yam
l
# .github/workflows/dagger.yml
名称:匕首CI

上:[推,拉请求]

职位:
词:
运行:ubuntu-latest
步骤:
- 使用:actions/checkout@v4

- 名称:运行 Dagger 管道
使用:dagger/dagger-for-github@v7
与:
版本:“0.19.7”
动词:呼叫
模块: .
参数:运行--source=.
环境:
GITHUB_TOKEN:${{ Secrets.GITHUB_TOKEN }}
```//byteiota.com/dagger-ci-cd-tutorial-write-pipelines-as-code-not-yaml/)
- [2026 年最佳 CI/CD 工具:数据显示什么](https://blog.jetbrains.com/teamcity/2026/03/best-ci-tools/)
- [GitHub Action 的 Dagger](https://github.com/dagger/dagger-for-github)
- [Dagger 架构深度探究](https://www.gocodeo.com/post/what-is-dagger-a-new-take-on-ci-cd-for-kubernetes-native-devops)<!--自动引用-->
## 参考文献和来源- [匕首](https://gi```
yam
l
# .gitlab-ci.yml
阶段:[构建]

匕首:构建:
阶段:构建
图片:docker:24-dind
服务:
- 码头工人:24-dind
变量:
DAGGER_VERSION:“0.19.7”
之前的脚本:
- apk添加--无缓存curl
-curl -fsSL https://dl.dagger.io/dagger/install.sh | BIN_DIR=/usr/local/bin sh
脚本:
- dagger 调用 run --source=.
缓存:
关键:匕首缓存
路径:
- .dagger-cache/
```
iners
/podman)
`` 时髦的
// 詹金斯文件
管道{
代理任何
环境{
DAGGER_VERSION = '0.19.7'
}
阶段{
stage('运行 Dagger Pipeline') {
步骤{
嘘'''
卷曲-fsSL https://dl.dagger.io/dagger/install.sh | \
BIN_DIR=/usr/local/bin sh
dagger 调用 run --source=.
'''
}
}
}
}

💬 留言讨论