Dagger
Dagger
A programmable CI/CD engine. Container-based and can run locally.
Dagger
A programmable CI/CD engine. Container-based and can run locally.
Dagger is an innovative tool that lets you write CI/CD pipelines in a programming language. Created by Solomon Hykes, co-founder of Docker, this tool defines pipelines in familiar programming languages such as Go, Python, and Node.js instead of YAML or Jenkins scripts.
// main.go - Dagger CI ํ์ดํ๋ผ์ธ
package main
import (
"context"
"fmt"
"os"
"dagger.io/dagger"
)
func main() {
if err := build(context.Background()); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func build(ctx context.Context) error {
// Dagger ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
if err != nil {
return err
}
defer client.Close()
// ์์ค ์ฝ๋ ๋ง์ดํธ
src := client.Host().Directory(".", dagger.HostDirectoryOpts{
Exclude: []string{"node_modules", ".git"},
})
// Node.js ์ปจํ
์ด๋ ์ค์
node := client.Container().
From("node:20-alpine").
WithDirectory("/app", src).
WithWorkdir("/app")
// ์์กด์ฑ ์ค์น
node = node.WithExec([]string{"npm", "install"})
// ํ
์คํธ ์คํ
node = node.WithExec([]string{"npm", "test"})
// ๋น๋ ์คํ
node = node.WithExec([]string{"npm", "run", "build"})
// ๋น๋ ๊ฒฐ๊ณผ๋ฌผ ๋ด๋ณด๋ด๊ธฐ
_, err = node.Directory("/app/dist").Export(ctx, "./dist")
if err != nil {
return err
}
fmt.Println("Build completed successfully!")
return nil
}
// Docker ์ด๋ฏธ์ง ๋น๋ ๋ฐ ํธ์
func buildAndPush(ctx context.Context, registry, tag string) error {
client, err := dagger.Connect(ctx)
if err != nil {
return err
}
defer client.Close()
// ๋ ์ง์คํธ๋ฆฌ ์ธ์ฆ
secret := client.SetSecret("registry-password", os.Getenv("REGISTRY_PASSWORD"))
// ์ด๋ฏธ์ง ๋น๋
src := client.Host().Directory(".")
image := client.Container().
Build(src).
WithRegistryAuth(registry, "username", secret)
// ์ด๋ฏธ์ง ํธ์
addr, err := image.Publish(ctx, fmt.Sprintf("%s/myapp:%s", registry, tag))
if err != nil {
return err
}
fmt.Printf("Published: %s\n", addr)
return nil
}
# ci.py - Dagger Python SDK
import sys
import anyio
import dagger
async def main():
config = dagger.Config(log_output=sys.stdout)
async with dagger.Connection(config) as client:
# Python ์ปจํ
์ด๋ ์ค์
python = (
client.container()
.from_("python:3.11-slim")
.with_directory("/app", client.host().directory("."))
.with_workdir("/app")
)
# ์์กด์ฑ ์ค์น
python = python.with_exec(["pip", "install", "-r", "requirements.txt"])
# ๋ฆฐํธ ๊ฒ์ฌ
python = python.with_exec(["flake8", "."])
# ํ
์คํธ ์คํ
python = python.with_exec(["pytest", "-v"])
# ๊ฒฐ๊ณผ ์ถ๋ ฅ
output = await python.stdout()
print(output)
if __name__ == "__main__":
anyio.run(main)
{
"name": "my-pipeline",
"sdk": "go",
"dependencies": [],
"source": "dagger"
}
Dagger requires Docker (or Podman, containerd). It cannot run in an environment without Docker, and the CI runner must have Docker permissions.
Teams accustomed to YAML-based CI need time to adapt at first. An understanding of how to use the SDK and of container concepts is required.
Because it runs across multiple container layers, debugging can be complex. Skill with the `--debug` flag and log analysis is needed.
The first run takes time because of base image pulling. Once caching is applied, subsequent builds are faster.