KAHIBARO
Discord Login Register

24.10. Deployment Pipelines

Understanding Deployment Pipelines

A deployment pipeline is the automated path that your code follows from a Git commit to running in a live environment. It connects building, testing, packaging, and deploying your backend into a repeatable process.

You can think of it as a factory line: code goes in at one end, a deployed application comes out at the other, with automatic checks at every station.

This chapter focuses on the structure and logic of deployment pipelines, not on specific CI/CD tools. Those are covered elsewhere in this course.

A deployment pipeline is a series of automated stages that transform and verify your code changes until they are safely deployed to an environment.

Typical stages include: build, test, package, deploy, and verify.


Core Ideas of a Deployment Pipeline

A deployment pipeline has a few key goals:

A pipeline usually runs on every push, merge, or tag in your Git repository. The behavior might differ per branch:


Branch / TagTypical Behavior
feature/*Build + unit tests only
developBuild + unit + integration tests + deploy to dev
stagingFull pipeline + deploy to staging
main / masterFull pipeline + deploy to production (maybe gated)
v1.2.3 tagProduction release for version 1.2.3

Pipeline Stages from Code to Production

Below is a common high level flow. Each step can be mapped to a CI/CD job or stage.

  1. Code checkout
  2. Build
  3. Static checks
  4. Tests
  5. Package / artifact build
  6. Deploy to non-production environments
  7. Automated checks in environment
  8. Production deployment
  9. Post-deploy verification

Not every project needs every step, but most real systems include at least a subset.


1. Code Checkout

The pipeline always starts with the source code.

Typical tasks:

Important points:

2. Build Stage

The build stage converts source code into something that can run or be deployed.

Examples for backend projects:

For Docker-based deployments, the build stage often:

  1. Logs into a container registry (if private).
  2. Builds a Docker image with a tag, for example:
    • my-api:commit-sha
    • my-api:v1.2.3
  3. Pushes the image to the registry.

Always tie a build artifact (like a Docker image) to a specific commit hash so you can reproduce and roll back deployments.


3. Static Checks

Static checks run without executing the application. They catch problems early.

Common static checks:

In a deployment pipeline:

Example grouping of static check jobs:


Job namePurpose
lintStyle and basic errors
type-checkType-related issues
security-scanKnown vulnerabilities in deps

4. Test Stages

Tests are usually split into stages according to speed and scope:

A common pattern:

  1. Run unit tests after build and static checks.
  2. Only if those pass, build deployment artifacts (Docker image).
  3. After deployment to a test environment, run integration or E2E tests.

You might also:

5. Packaging and Artifacts

A deployment pipeline often produces artifacts that are then reused in later stages.

For example:

Key rule:

Build an artifact once and promote it through environments.
Do not rebuild from source separately for staging and production.

This ensures that staging and production are running exactly the same artifact.


6. Deploying to Environments

The same artifact usually moves through multiple environments:

Typical sequence:

  1. Development environment
  2. Staging or QA environment
  3. Production environment

Each deployment step should be:

Configuration per Environment

You should not change the code when you deploy to production. Instead, you swap configuration values, usually through environment variables.

Examples:

SettingDev valueStaging valueProd value
DATABASE_URLlocal PostgreSQLstaging PostgreSQL clusterproduction PostgreSQL cluster
REDIS_URLlocal Redisstaging Redisproduction Redis
DEBUGtruefalsefalse
ALLOWED_ORIGINShttp://localhost:3000https://staging.example.comhttps://app.example.com

The pipeline passes these values through Docker, Kubernetes manifests, or systemd service files, depending on your platform.


7. Automated Checks in Each Environment

After deployment to a non-production environment, you usually want automatic validation.

Common checks:

These checks can be part of the pipeline and must pass before you:

Pipelines for Different Environments

You usually define pipelines or pipeline branches that map to environments in a consistent way.

Dev / Feature Branch Pipelines

For feature branches, your goal is fast feedback, not full deployment.

Common behavior:

Ephemeral environments are temporary environments created for a branch, then destroyed when the branch is merged.

Staging Pipelines

Staging pipelines are closer to production:

Some teams also run manual QA or user acceptance testing on staging.

Production Pipelines

Production pipelines are the most cautious:

Possible triggers:

Common production deployment strategies (concept only, implementation is tool specific):

StrategyDescription
Direct (all at once)Update all instances at the same time
RollingUpdate instances one by one or in batches
Blue-greenDeploy to a new set of servers, switch traffic over
CanaryDeploy to a small subset first, then expand if healthy

After production deploy, the pipeline often:

Manual Approvals and Gates

For critical environments like production, many teams use manual approval steps.

A gate might:

Typical flow:

  1. Code merges into main.
  2. Pipeline builds and tests.
  3. Successfully built artifact is deployed to staging.
  4. Tests pass in staging.
  5. Pipeline pauses at a manual approval step: "Deploy to production?"
  6. Engineer reviews changes and production status.
  7. Engineer approves, pipeline continues to production deploy.

Rollbacks and Versioning in Pipelines

A good deployment pipeline not only deploys forward but also helps you roll back when something goes wrong.

Common rollback approaches:

You need a clear versioning scheme to do this reliably.

Examples:

Always make it easy and fast to deploy a previous known good version from your pipeline, without manual rebuilding.

This often means:

Pipeline Configuration as Code

Deployment pipelines are usually described in files inside your repository, for example:

Benefits of storing pipeline configuration in version control:

Core ideas you often express in these files:

Secrets in Deployment Pipelines

Deployment usually needs secrets, for example:

You must:

The pipeline should:

Example Logical Pipeline Flow

Here is a simplified, tool-agnostic flow for a backend with Docker deployment:

  1. Trigger
    • On push to any branch.
  2. Build & Unit Test Stage
    • Checkout code at commit X.
    • Install dependencies.
    • Run static checks (lint, type).
    • Run unit tests.
    • If any step fails, stop here.
  3. Build Artifact Stage
    • Build Docker image my-api:X.
    • Push image to registry.
    • Store commit hash and image tag mapping.
  4. Staging Deployment Stage
    • Only for staging and main branches.
    • Deploy my-api:X to staging.
    • Run smoke tests against staging.
    • If tests fail, mark pipeline as failed.
  5. Production Deployment Stage
    • Only for main branch, maybe after manual approval.
    • Deploy my-api:X to production using rolling updates.
    • Run basic health checks.
    • Notify team of deployment status.
  6. Rollback Option
    • A separate job or pipeline can redeploy my-api:Y where Y is the last known good tag.

Metrics and Feedback from Pipelines

A deployment pipeline is also a source of process metrics:

Even as a beginner, you can start simple:

Starting Simple with Deployment Pipelines

You do not have to build a complex pipeline on day one. A minimal useful pipeline might be:

  1. On push:
    • Run lint and unit tests.
  2. On push to main:
    • Build Docker image and push to registry.
    • Deploy container to a test server via script.
  3. Later:
    • Add staging environment.
    • Add manual approval for production.
    • Add smoke tests and rollback strategies.

Focus on these principles:

These ideas will apply regardless of the specific CI/CD tool or hosting platform you use.

Views: 7

Comments

Please login to add a comment.

Don't have an account? Register now!