Why this lab exists
Every time you push a commit, something runs a sequence of commands on your behalf: check out the code, install dependencies, compile, run tests, package, deploy. At first glance this looks like a shell script. But production CI systems — GitHub Actions, Jenkins, CircleCI, Buildkite — are not shell scripts. They are distributed systems that solve hard problems: how do you run 50 test jobs in parallel and cancel them all when one fails? How do you avoid re-compiling code that has not changed? How do you prevent a malicious step from stealing secrets from the next pipeline run?
This lab builds a CI/CD engine from scratch in Go, in three stages, exposing exactly those problems. By the end you will understand:
- Why
exec.Commandwithcontext.WithTimeoutis the correct primitive for step isolation (and what it cannot do) - Why a DAG scheduler with Kahn’s topological sort is the right data structure for pipeline dependencies
- Why content-addressed storage (SHA256 of content, not of names) is the correct foundation for build caching
- Why Google’s Bazel and Meta’s Buck invest heavily in cache correctness — and what a wrong cache hit costs
The mental shift this lab produces: a CI/CD pipeline is a DAG of subprocesses connected by an artifact store. The scheduling, caching, and isolation problems are separable and solvable independently.
What we are building
A CI/CD engine in Go, built across three progressive stages. Each stage adds one foundational capability.
We build:
- A pipeline DSL:
Step,Pipeline, andPipelineResultstructs with JSON serialisation - A sequential
Executorusingexec.Commandwith per-step context timeouts - A DAG scheduler:
StagewithDependsOn, cycle detection via DFS, topological sort via Kahn’s algorithm - A
ParallelExecutorthat fans out each stage’s steps into goroutines and cancels siblings on failure - A content-addressed
ArtifactStorekeyed by SHA256 with deduplication - A
CachableExecutorthat computes a deterministicCacheKeyand skips steps on cache hits - A CLI entry point:
cicd run pipeline.json
We explicitly skip (and why):
- Container isolation per step: Linux namespaces and cgroups are covered in the container-runtime lab
- Distributed workers: a worker pool requires a message queue (see the message-queue lab) and service discovery
- Secret injection: Vault and sealed secrets are security primitives outside the engine scope
- Pipeline YAML with full GitHub Actions semantics: matrix builds, reusable workflows, and conditional expressions are a DSL layer on top of the core engine
Mental model
A CI/CD pipeline is three things layered on top of each other:
Layer 1 — Step execution (what each unit of work does)
Step: {name, command, env, timeout}
Result: {exitCode, stdout, stderr, duration, status}
Tool: exec.Command("sh", "-c", command) — a subprocess
Layer 2 — DAG scheduling (in what order stages run)
Stage: {name, steps[], dependsOn[]}
Order: topological sort of the dependency graph
Tool: Kahn's algorithm — queue stages with zero in-degree
Layer 3 — Artifact caching (whether to run at all)
Key: SHA256(step_name + sorted_env + input_file_hashes)
Hit: skip the step; restore cached output
Miss: run the step; store output by SHA256
Tool: map[string]*Artifact — content-addressed by hash
The caching layer is the most important for build performance. A typical monorepo PR changes 3 of 50 packages. The other 47 packages have unchanged inputs. If the cache key is correct — encoding all inputs that affect the output — those 47 packages produce cache hits and are skipped entirely.
The DAG layer is the most important for correctness. A pipeline that runs deploy before test is wrong regardless of how fast the cache is. Kahn’s algorithm gives us an order that respects all dependsOn edges in O(V+E) time.
The step execution layer is the most important for isolation. Running steps as subprocesses (exec.Command) gives each step its own process, environment, and signal handling. It does not give full isolation — that requires namespaces — but it is the correct building block.
Prerequisites
- Go 1.23 or later
- Familiarity with
os/execandcontext.WithTimeoutin Go - Basic graph theory: what a DAG is, what topological order means
- SHA256 as a content fingerprint — not a cryptographic tool here, but a deterministic hash of bytes
No prior CI/CD system knowledge assumed. We build from first principles.
Stage-by-stage build
Stage 0 Pipeline DSL + Sequential Executor
- Define Step, Pipeline, StepResult, PipelineResult structs
- Implement Executor.Run: exec.Command with context.WithTimeout per step
- Capture stdout and stderr into strings.Builder
- On step failure: mark remaining steps Skipped, return immediately
- 7 tests: sequential order, failure skips remaining, timeout kills step, env vars, exit code, stderr, empty pipeline
The pipeline DSL is three structs. Step is the unit of work. Pipeline is an ordered list of steps. StepResult is what comes back:
type Step struct {
Name string
Command string
Env map[string]string
Timeout time.Duration
}
type StepResult struct {
StepName string
ExitCode int
Stdout string
Stderr string
Duration time.Duration
Status StepStatus // Pending | Running | Passed | Failed | Skipped
}The Executor.Run method is the interesting part. It calls exec.CommandContext — not exec.Command — so the operating system kills the subprocess when the context is cancelled:
func (e *Executor) runStep(step Step) StepResult {
ctx := context.Background()
if step.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, step.Timeout)
defer cancel()
}
cmd := exec.CommandContext(ctx, "sh", "-c", step.Command)
// Merge step env on top of inherited environment.
if len(step.Env) > 0 {
for k, v := range step.Env {
cmd.Env = append(cmd.Environ(), k+"="+v)
}
}
var stdout, stderr strings.Builder
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
// ...
}The key detail is cmd.Environ() (inherits parent env) plus explicit overrides. This is how GitHub Actions step-level env: works — it inherits the workflow environment and overlays the step’s own keys.
Fail-fast behaviour: the outer loop checks a failed boolean. Once any step fails, subsequent steps get a StepResult{Status: StatusSkipped} without executing. This is deterministic — the pipeline result always has one entry per step, always in declaration order:
for _, step := range pipeline.Steps {
if failed {
result.Steps = append(result.Steps, StepResult{
StepName: step.Name, Status: StatusSkipped,
})
continue
}
sr := e.runStep(step)
result.Steps = append(result.Steps, sr)
if sr.Status == StatusFailed {
failed = true
result.Status = PipelineFailed
}
}What the 7 tests check: sequential steps run in order, a failing step at index 1 leaves index 2 as Skipped, a step with Timeout: 100ms and Command: "sleep 5" returns StatusFailed, env vars injected via step.Env are visible in the subprocess via $CICD_TEST_VAR, exit code 42 is captured exactly, echo error >&2 populates StepResult.Stderr, and a pipeline with no steps returns PipelinePassed immediately.
Stage 1 DAG Pipeline + Parallel Stages
- Define Stage{Name, Steps, DependsOn} and PipelineConfig{Name, Stages}
- Implement ValidateDAG: DFS with in-progress marks to detect cycles
- Implement TopoSort: Kahn's algorithm returning execution order
- Implement ParallelExecutor: goroutines + sync.WaitGroup per stage, context cancel on failure
- 5 tests: topo order, cycle detection error, parallel steps finish together, fail-fast cancels siblings, dependency respected
The DAG introduces Stage — a set of steps that run in parallel — and DependsOn — which stages must finish first:
type Stage struct {
Name string
Steps []Step
DependsOn []string
}
type PipelineConfig struct {
Name string
Stages []Stage
}Cycle detection uses DFS with three node states: 0 (unvisited), 1 (in-progress), 2 (done). If DFS reaches a node currently in-progress, there is a cycle:
func dfs(name string) error {
switch state[name] {
case 2:
return nil // already fully explored
case 1:
return fmt.Errorf("cycle detected: %q is in a dependency cycle", name)
}
state[name] = 1 // mark in-progress
for _, dep := range stageMap[name].DependsOn {
if err := dfs(dep); err != nil { return err }
}
state[name] = 2 // mark done
return nil
}Kahn’s algorithm for topological sort works by repeatedly removing stages with zero in-degree (no remaining dependencies):
// Count incoming edges (how many stages depend on each stage).
for _, s := range stages {
for _, dep := range s.DependsOn {
inDegree[s.Name]++
dependents[dep] = append(dependents[dep], s.Name)
}
}
// Start with all zero-in-degree stages.
queue := stagesWithZeroInDegree()
for len(queue) > 0 {
curr := queue[0]; queue = queue[1:]
order = append(order, curr)
for _, dep := range dependents[curr] {
inDegree[dep]--
if inDegree[dep] == 0 { queue = append(queue, dep) }
}
}The result is a linear order that respects all DependsOn edges. If the graph has N stages and E edges, Kahn’s algorithm runs in O(N+E) — fast even for large pipelines.
Parallel stage execution fans out each stage’s steps into goroutines. A shared context lets any failing step cancel its siblings:
func (pe *ParallelExecutor) runStage(stage Stage) StageResult {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ch := make(chan StepResult, len(stage.Steps))
var wg sync.WaitGroup
for _, step := range stage.Steps {
wg.Add(1)
go func(s Step) {
defer wg.Done()
sr := runStepWithContext(ctx, s)
if sr.Status == StatusFailed {
cancel() // signal siblings to stop
}
ch <- sr
}(step)
}
wg.Wait()
close(ch)
// collect results...
}The cancel() call propagates through ctx to all sibling goroutines. Goroutines that have not yet started their subprocess check ctx.Done() before calling exec.CommandContext. Goroutines already inside cmd.Run() are killed by the OS when their context is cancelled.
Why not run all stages in parallel? Because deploy after test is a correctness requirement, not just a preference. Kahn’s algorithm gives us the correct order for free. Stages at the same topological level (same depth in the DAG) genuinely can run in parallel — that is the next evolution, requiring a level-aware scheduler.
Stage 2 Content-Addressed Artifacts + Pipeline Cache
- Implement ArtifactStore: map[string]*Artifact keyed by SHA256, dedup on upload
- Implement ArtifactCache: map[string]*Artifact keyed by CacheKey
- Implement ComputeCacheKey: SHA256(step_name + sorted_env_pairs + input_hash)
- Implement CachableExecutor: check cache before running, store artifact on miss
- 4 tests: cache hit skips step, cache miss runs step, same content deduplicated, env var change invalidates key
The content-addressed artifact store deduplicates by content, not by name. Two steps that produce identical binary output store one artifact:
type ArtifactStore struct {
mu sync.RWMutex
artifacts map[string]*Artifact // keyed by SHA256 hex
}
func (s *ArtifactStore) Upload(name, path string, content []byte) (*Artifact, error) {
digest := sha256hex(content)
s.mu.Lock()
defer s.mu.Unlock()
if existing, ok := s.artifacts[digest]; ok {
return existing, nil // dedup: same content, return existing
}
a := &Artifact{Name: name, Path: path, SHA256: digest, ...}
s.artifacts[digest] = a
return a, nil
}This is exactly how Docker image layers work. A layer is not identified by its tag or position — it is identified by the SHA256 of its content. Two images that share a base layer share one blob on disk.
The cache key is the most important correctness decision in the entire system. It must encode every input that affects the step’s output. Add anything irrelevant and you get unnecessary cache misses. Miss anything relevant and you get stale outputs (a wrong cache hit):
func ComputeCacheKey(step Step, inputHash string) string {
var parts []string
parts = append(parts, "step:"+step.Name)
// Sort env keys — map iteration order is non-deterministic in Go.
envKeys := sortedKeys(step.Env)
for _, k := range envKeys {
parts = append(parts, "env:"+k+"="+step.Env[k])
}
parts = append(parts, "input:"+inputHash)
return sha256hex([]byte(strings.Join(parts, "\n")))
}The inputHash encodes the content of all files the step reads. Change a source file and its hash changes, the cache key changes, and the step re-runs. Leave source files unchanged and the key is identical across machines — because SHA256 of content is deterministic regardless of file timestamps.
The CachableExecutor checks the cache before running each step:
func (ce *CachableExecutor) Run(pipeline Pipeline, inputHashes []string) PipelineResult {
for i, step := range pipeline.Steps {
key := ComputeCacheKey(step, inputHashes[i])
if cached := ce.cache.Get(key); cached != nil {
ce.Stats.CachedSteps.Add(1)
// Skip the step; use cached artifact.
continue
}
// Cache miss: run the step and store the artifact.
sr := ce.executor.runStep(step)
if sr.Status == StatusPassed {
artifact, _ := ce.store.Upload(step.Name, "", []byte(sr.Stdout))
ce.cache.Put(key, artifact)
ce.Stats.BuiltSteps.Add(1)
}
}
}PipelineStats uses sync/atomic counters so the executor is safe for concurrent use across multiple pipeline runs.
The wrong-cache-hit failure mode: if inputHash omits a header file that the step’s compiler reads, the cache key will be identical before and after changing that header. The step returns stale cached output — a binary compiled against the old header. This is why Bazel’s cache keys include transitive dependency hashes, not just direct inputs. A wrong cache hit in a test step means silent test skips — the most dangerous failure mode in a CI system.
Run it yourself
labs/cicd-engine ↗
Go 1.23+ - 1 Clone and build
git clone https://github.com/pushkarkumar-dev/system-design-labs cd system-design-labs/labs/cicd-engine go build ./... - 2 Run all tests
go test ./pkg/cicd/ -v -count=1 - 3 Run benchmarks
go test -bench=. -benchtime=3s ./... - 4 Run a sample pipeline via the CLI
echo '{"name":"demo","steps":[{"name":"build","command":"echo building"},{"name":"test","command":"echo testing"}]}' > /tmp/demo.json go run ./cmd/runner run /tmp/demo.json - 5 Observe fail-fast: second step failure skips third
echo '{"name":"fail-demo","steps":[{"name":"ok","command":"echo ok"},{"name":"fail","command":"exit 1"},{"name":"skip","command":"echo skipped"}]}' > /tmp/fail.json go run ./cmd/runner run /tmp/fail.json
=== RUN TestSequentialOrder
--- PASS: TestSequentialOrder (0.01s)
=== RUN TestStepFailureSkipsRemaining
--- PASS: TestStepFailureSkipsRemaining (0.01s)
=== RUN TestTimeoutKillsStep
--- PASS: TestTimeoutKillsStep (0.11s)
=== RUN TestEnvVarsPassedToSubprocess
--- PASS: TestEnvVarsPassedToSubprocess (0.01s)
=== RUN TestExitCodeCaptured
--- PASS: TestExitCodeCaptured (0.01s)
=== RUN TestStderrCaptured
--- PASS: TestStderrCaptured (0.01s)
=== RUN TestEmptyPipelineSucceeds
--- PASS: TestEmptyPipelineSucceeds (0.00s)
=== RUN TestDAGTopologicalOrder
--- PASS: TestDAGTopologicalOrder (0.01s)
=== RUN TestCycleDetectionReturnsError
--- PASS: TestCycleDetectionReturnsError (0.00s)
=== RUN TestParallelStepsInSameStage
--- PASS: TestParallelStepsInSameStage (0.22s)
=== RUN TestFailFastCancelsSiblingSteps
--- PASS: TestFailFastCancelsSiblingSteps (0.01s)
=== RUN TestStageDependencyRespected
--- PASS: TestStageDependencyRespected (0.01s)
=== RUN TestCacheHitSkipsStep
--- PASS: TestCacheHitSkipsStep (0.00s)
=== RUN TestCacheMissRunsStep
--- PASS: TestCacheMissRunsStep (0.01s)
=== RUN TestSameContentDeduplicatedInStore
--- PASS: TestSameContentDeduplicatedInStore (0.00s)
=== RUN TestCacheKeyChangesWithEnvVarChange
--- PASS: TestCacheKeyChangesWithEnvVarChange (0.00s)
PASS
ok github.com/pushkarkumar-dev/system-design-labs/labs/cicd-engine/pkg/cicd Benchmarks
M2 MacBook Pro, 16GB RAM | Metric | Result | Notes |
|---|---|---|
| Pipeline with 5 echo steps | 85 ms total | exec.Command subprocess overhead dominates — 5 process forks + sh invocations |
| DAG topological sort (100 stages) | 8,500,000 sorts/sec | Kahn's algorithm O(V+E) — primarily map iteration and slice append |
| Cache hit (skip step) | 2,500,000 lookups/sec | SHA256 string key into a sync.RWMutex-protected map |
| Artifact dedup (same content) | 1,200,000 uploads/sec | SHA256 hash of content bytes + map lookup with write lock |
Stress tests
1,000 concurrent cache lookups on a warm cache: spawn 1,000 goroutines each calling ArtifactCache.Get with the same key. The sync.RWMutex allows all reads to proceed concurrently — total time under 2ms. Compare with sync.Mutex (no reader concurrency): total time under 5ms. For a read-heavy cache, RWMutex is the correct choice.
Cycle in a 10-stage graph: construct a graph where stage 5 depends on stage 7, which depends on stage 5. ValidateDAG returns an error within one DFS traversal — O(V+E). No infinite loop possible because the in-progress mark breaks the recursion on the first revisit.
Identical binary uploaded 10,000 times: call ArtifactStore.Upload 10,000 times with the same content. The SHA256 is computed once per call (the hash function is not cached), but the map write is skipped for all 9,999 duplicates. Store size stays at 1. This simulates 100 build jobs all uploading the same compiler binary as an artifact.
Cache key stability across env orderings: call ComputeCacheKey with the same step and env but in different map iteration orders. Because the implementation sorts env keys before hashing, the result is always identical. This is the correctness property that makes distributed caching work: two workers on different machines compute the same key for the same logical step.
What the toy misses
- Container sandboxing per step Real CI systems (GitHub Actions, CircleCI) run each step in an isolated container. Our exec.Command shares the host filesystem and process namespace — a malicious step could read secrets from other pipelines.
- Distributed workers A single executor is a throughput bottleneck. Production CI dispatches pipeline runs to a pool of workers via a job queue. Workers register with the server, poll for jobs, and return results.
- Secrets management CI pipelines need API keys and credentials. Storing secrets as env vars is insecure (logged, leaked in process lists). Production systems use sealed secrets, Vault injection, or OIDC token federation.
- Pipeline-as-code with schema validation Jenkinsfile and GitHub Actions YAML have rich schemas: matrix builds, conditional steps, timeout policies, retry counts, service containers. Our JSON is a minimal subset.
- Build result aggregation and reporting Real CI systems aggregate test results (JUnit XML), code coverage (Cobertura), and static analysis findings into a dashboard. Parsing and storing 10,000 test results from 100 parallel workers requires a dedicated result store.
- Cache poisoning prevention Our cache key includes input file hashes, but a malicious step could write to input files used by downstream steps, poisoning their cache. Production caches use signed artifacts and verify signatures before restoring.
Wiring it into a Java/Spring service
Wiring it into a Java/Spring service
org.springframework.boot:spring-boot-starter-web:3.3.0 org.springframework.boot:spring-boot-starter-actuator:3.3.0 labs/cicd-engine/java-integration Clone and run: cd labs/cicd-engine/java-integration && mvn spring-boot:run The CI/CD model in three syntaxes
Our Go pipeline, Jenkins Groovy DSL, and GitHub Actions YAML all describe the same concept: a named list of steps with dependency ordering and fail-fast semantics. Understanding the structural equivalence makes each system easier to reason about.
Our Go pipeline JSON:
{
"name": "build-and-test",
"steps": [
{"name": "build", "command": "go build ./..."},
{"name": "test", "command": "go test ./..."}
]
}Equivalent Jenkinsfile (Groovy DSL):
pipeline {
stages {
stage('Build') {
steps { sh 'go build ./...' }
}
stage('Test') {
steps { sh 'go test ./...' }
}
}
}Equivalent GitHub Actions YAML:
jobs:
build:
steps:
- run: go build ./...
test:
needs: build
steps:
- run: go test ./...The GitHub Actions needs: build field maps directly to our Stage.DependsOn: ["build"]. Jenkins stages are sequential by default; the parallel{} block enables concurrency. Our DAG scheduler generalises both: stages with no DependsOn run in parallel, stages with dependencies wait.
Part A — CicdClient (service-client pattern)
CicdClient.java submits pipeline definitions to the Go runner’s HTTP API and maps the result back to Java records:
@Component
public class CicdClient {
public record Step(String name, String command,
Map<String, String> env, long timeoutSeconds) {}
public record Pipeline(String name, List<Step> steps) {}
public record PipelineResult(String pipelineId, List<StepResult> steps,
String status) {}
private final RestClient restClient;
public CicdClient(CicdProperties props) {
this.restClient = RestClient.builder()
.baseUrl(props.getRunnerUrl())
.build();
}
public PipelineResult run(Pipeline pipeline) {
return restClient.post()
.uri("/pipelines")
.body(pipeline)
.retrieve()
.body(PipelineResult.class);
}
}Spring’s RestClient (Spring 6.1+) is the modern replacement for RestTemplate. It uses the same connection pooling as Go’s net/http.Transport with keep-alives.
Part B — CicdAutoConfiguration
CicdAutoConfiguration.java wires the client into the Spring context:
@Configuration
@EnableConfigurationProperties(CicdProperties.class)
public class CicdAutoConfiguration {
@Bean
public CicdClient cicdClient(CicdProperties properties) {
return new CicdClient(properties);
}
}The Spring @Configuration class mirrors Go’s init() pattern: it runs once at startup and wires dependencies together. @EnableConfigurationProperties binds application.yml values to the CicdProperties record automatically.
Part C — Comparing Go concurrency with Spring async
Our ParallelExecutor fans steps into goroutines via sync.WaitGroup. In Spring with Java 21 virtual threads, the equivalent is @Async with a virtual thread executor:
@Configuration
@EnableAsync
public class AsyncConfig {
@Bean
public Executor virtualThreadExecutor() {
return Executors.newVirtualThreadPerTaskExecutor();
}
}The structural pattern is identical: submit N tasks, collect results, cancel on first failure. Virtual threads eliminate the 1:1 OS thread overhead that made thread-per-request impractical in older Java. Goroutines and virtual threads solve the same problem from different directions: Go’s scheduler is in the runtime; Java’s virtual thread scheduler is in the JVM (JEP 444).
application.yml
cicd:
runner-url: http://localhost:8090
default-timeout-seconds: 300
max-parallel-steps: 8
management:
endpoint:
health:
show-details: alwaysRun it
# Terminal 1: build and start the Go runner
cd labs/cicd-engine
go build -o cicd-runner ./cmd/runner
# (The runner is a CLI tool; in practice you would add an HTTP server layer)
# Terminal 2: start the Spring Boot demo
cd labs/cicd-engine/java-integration
mvn spring-boot:run
# Check health
curl http://localhost:8080/actuator/healthExpected output:
{"status":"UP","components":{"diskSpace":{"status":"UP"},"ping":{"status":"UP"}}} What surprised me
Where to go deeper
- Bazel remote cache protocol: Bazel’s remote execution API (REAPI) defines how cache keys, action results, and artifacts are stored and retrieved across machines — the production version of our
ArtifactStoreandArtifactCache: bazel.build/remote/caching - BuildKit internals: Docker’s BuildKit rewrote the Docker builder as a DAG of LLB (Low-Level Builder) operations with content-addressed storage. The source is the clearest real-world example of the concepts in this lab: github.com/moby/buildkit
- Tekton pipeline CRDs: Tekton models pipelines as Kubernetes custom resources —
Task,TaskRun,Pipeline,PipelineRun. The CRD schemas map directly to our structs: tekton.dev/docs/pipelines - GitHub Actions runner source: the
actions/runnerrepository shows how GitHub dispatches jobs to runners, streams logs, and handles cancellation — the distributed worker problem this lab deliberately skips: github.com/actions/runner - Nx affected commands: Nx’s
--affectedflag computes which packages to build or test based on the Git diff — a practical cache key implementation built on top of the same content-hashing principle
Related labs and questions
- Previous in platform: Container Runtime — understanding Linux namespaces and cgroups explains why real CI runs steps in containers, not bare processes
- Next in platform: Service Discovery — distributing CI workers requires each worker to register itself and the coordinator to discover available workers
- Message Queue lab — the worker dispatch problem in distributed CI is solved with a queue: the coordinator enqueues jobs, workers dequeue and execute
- Distributed Lock lab — concurrent pipeline runs on the same repo branch need a lock to prevent two deploys from racing; the dist-lock lab builds that primitive