Mark Pitt
claude-skills
  1. claude-skills
  2. Engineering practices
  3. twelve-factor-app
  4. resources/build-release-run.md

A file in twelve-factor-app

resources/build-release-run.md

Markdown 7.3 KB
build-release-run.mdMarkdown, rendered

Build, Release, Run

Covers Factor V: Build, Release, Run — strict separation of the three deployment stages to ensure reproducible, auditable, and rollback-capable deployments.

---

Factor V: Build, Release, Run

Principle

Strictly separate build and run stages.

A codebase is transformed into a deploy through exactly three stages, and they must never be mixed.

The Three Stages

Codebase (Git repo)
   │
   ▼ BUILD STAGE
Build Artifact (compiled binaries, assets, vendored dependencies)
   │
   ▼ RELEASE STAGE (Build + Config)
Release (unique, immutable, timestamped)
   │
   ▼ RUN STAGE
Running Process(es) in execution environment
StageInputOutputWho Triggers
BuildSpecific commit + dependenciesExecutable bundle / artifactDeveloper (on deploy)
ReleaseBuild artifact + environment configImmutable, timestamped releaseAutomated pipeline
RunA specific releaseRunning processesProcess manager / platform

Build Stage

The build stage converts a code repository into an executable bundle (the "build"):

  • Fetches every dependency declared in the manifest (Factor II)
  • Compiles binaries, transpiles code (TypeScript → JavaScript, SCSS → CSS)
  • Packages assets (bundling, minification)
  • Runs code generation (protobuf, OpenAPI, etc.)

The build is tied to a specific commit. It should be deterministic: the same commit always produces the same build.

Build artifacts by platform:

PlatformBuild Artifact
DockerContainer image
Heroku / BuildpacksSlug
AWS LambdaZIP or container image
Node.jsBundled dist/ directory
Java*.jar / *.war file
.NETpublish/ output directory
GoCompiled binary

Release Stage

The release stage combines the build artifact with the deploy's config (Factor III):

Build v42 + Production config → Release v42-prod
Build v42 + Staging config   → Release v42-staging

Release immutability rules:

  • Every release has a unique release ID (timestamp or incrementing number)
  • Releases are an append-only ledger — a release is never mutated
  • Any change (code or config) requires creating a new release
  • Releases can be rolled back by re-activating a previous release ID
# Example: Heroku-style release tracking
heroku releases
# v47   Deploy a3f1c4b  2025-11-15 14:32:11
# v46   Deploy 9c2b8e1  2025-11-14 09:15:42
# v45   Set FEATURE_FLAG_X=true  2025-11-13 16:40:10

heroku rollback v46   # re-activate a previous release

Run Stage

The run stage executes one or more of the app's processes against a selected release. It should be as simple as possible because:

  • It can be triggered automatically (server reboot, crash restart)
  • There may be no developer available when a failure occurs
  • Complexity here increases mean time to recovery

Run stage characteristics:

  • Reads config from the environment (Factor III)
  • Starts processes as declared in the Procfile or equivalent
  • No compilation, asset pipelining, or dependency fetching happens here
  • Manages process lifecycle via the host OS process manager

Why Strict Separation Matters

Without SeparationWith Separation
Runtime code changes are possibleRuntime code changes are impossible
No rollback capabilityEasy rollback to previous release
Build failures surface at runtimeBuild failures surface at build time
Non-deterministic deploymentsReproducible, auditable deployments
Mixing config and codeConfig is cleanly injected at release time

Critical Rule: No Runtime Code Changes

It is impossible to make changes to the code at runtime, since there is no way to propagate those changes back to the build stage.

# Wrong: editing code on a running server
ssh prod-server
vim /app/handlers/payment.py   # direct edit in production
# This bypasses build stage entirely, is not tracked, not reproducible

All code changes must go through the build pipeline.

CI/CD Pipeline Implementation

A twelve-factor compliant pipeline looks like:

# GitHub Actions example
jobs:
  build:
    steps:
      - uses: actions/checkout@v4
      - name: Install dependencies
        run: npm ci           # reproducible install from lockfile
      - name: Build
        run: npm run build    # compile, bundle, generate assets
      - name: Push image
        run: docker push myapp:${{ github.sha }}

  release:
    needs: build
    steps:
      - name: Create release
        run: |
          # Combine build artifact with environment config
          # Tag the release with unique ID
          kubectl set image deployment/myapp app=myapp:${{ github.sha }}
          # Release ID = github.sha or a derived version

  # "run" stage is managed by Kubernetes, systemd, or the platform

Procfile Pattern

Many platforms (Heroku, Railway, Render, Dokku) use a Procfile to declare process types at run time:

# Procfile
web: gunicorn myapp.wsgi:application --bind 0.0.0.0:$PORT
worker: celery -A myapp worker --loglevel=info
beat: celery -A myapp beat --loglevel=info

The Procfile lives in the codebase (built into the artifact), and the platform decides which process types to run and how many of each (see Factor VIII: Concurrency).

Rollback Strategy

Because releases are immutable and identified by unique IDs, rollback is simple:

# Heroku
heroku rollback v45

# Kubernetes (image tag = commit SHA)
kubectl set image deployment/myapp app=myapp:9c2b8e1

# AWS ECS (task definition revision)
aws ecs update-service --service myapp --task-definition myapp:41

Rollback is a config change (pointing to an existing earlier build), not a new build.

Common Violations

ViolationDescriptionFix
SSH to prod and run git pullBypasses build and release stages entirelyUse a CI/CD pipeline
Compile assets at startupBuild work happening in the run stageMove to build stage
pip install at container startInstalling dependencies at runtimeInstall in Dockerfile/build
Config baked into buildEnvironment-specific values in the artifactInject at release stage via env vars
No release IDsCannot identify which version is runningTag every artifact with commit SHA or version
Mutable releasesPatching running code in placeBuild a new release for every change

Containerised Build/Release/Run

Docker naturally maps to this model:

# BUILD stage: compile the application
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production   # dependency install = part of build
COPY . .
RUN npm run build              # compile TypeScript, bundle assets

# RUN stage: minimal production image
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
CMD ["node", "dist/server.js"]

The release is the container image tagged with a specific version. Config (Factor III) is injected at container startup via environment variables — never baked into the image.

# Release: image + config = running container
docker run \
  -e DATABASE_URL=$DATABASE_URL \
  -e SECRET_KEY=$SECRET_KEY \
  myapp:v1.2.3       # ← specific, immutable build artifact