CI/CD Basics

Continuous integration and delivery turn a change into a tested, traceable artifact. A good pipeline shortens feedback while preserving a clear boundary between code that is merely proposed, code that is releasable, and code that is serving users.

The Delivery Model

Continuous integration (CI) repeatedly integrates small changes and runs automated checks against them. Continuous delivery keeps validated changes ready to release, while a person or policy decides when production deployment occurs. Continuous deployment automatically releases every change that passes the required safeguards. These terms describe different release decisions, not different brands of pipeline software.

Model the Stages

A typical path is source change, validation, artifact creation, release approval, and deployment. A failed required check stops promotion; it must not be converted into a green result by an unconditional step. The diagram shows promotion of the same build output rather than rebuilding separately for each environment.

A CI/CD path from a source change through automated checks and a versioned artifact to approval and production deployment
Promote one verified artifact through controlled release stages.

Keep Feedback Fast and Useful

Run cheap, deterministic checks early: formatting, static analysis, unit tests, and a build. Add slower integration or browser tests where they cover meaningful risk. Each check should identify the commit, report a useful failure, and be reproducible locally or in a documented environment.

Defining a Workflow

A workflow-as-code file makes automation reviewable beside the application. This GitHub Actions example runs on proposed changes and on updates to the main branch. It assumes the repository declares its Node runtime and package scripts.

# Run the same required checks for proposed changes and main-branch updates.
name: Validate

on:
  pull_request:
  push:
    branches: [main]

jobs:
  test:
    # Use a clean hosted runner so results do not depend on a maintainer's machine.
    runs-on: ubuntu-latest
    steps:
      # Review action updates; full commit SHAs provide immutable version pinning.
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm
      # npm ci installs exactly the dependency graph committed in the lockfile.
      - run: npm ci
      # Keep required checks explicit so a missing script fails visibly.
      - run: npm test
      - run: npm run build

Choose Triggers and Jobs

Use pull-request events for pre-merge validation and branch pushes for integration feedback. Split independent checks into jobs when parallel execution saves meaningful time, but avoid duplicating expensive setup without reason. A job dependency expresses a real gate: a release job should depend on the validation job rather than merely appearing later in the file.

Make Required Checks Enforceable

Configure branch protection or repository rules to require the intended status checks before merging. A workflow that runs successfully but is not required can be bypassed by merging while pending or failing. Give each check a stable name, and update the rule when job names change.

Artifacts and Reproducible Builds

An artifact is the output that is tested, stored, and ultimately delivered: for example, a package, container image, static site, or signed release archive. Record its source revision and build metadata. For stronger supply-chain integrity, preserve checksums or attestations and verify them at promotion time.

Build Once, Promote Deliberately

Build once from a known revision and promote that same artifact between environments. Rebuilding separately for staging and production can produce different bytes due to dependency drift, timestamps, or changing external inputs. Use immutable version tags or digests instead of mutable names such as latest when selecting a release.

Manage Dependencies and Caches

Commit lockfiles and use the package manager's reproducible install mode in CI. Caches are performance hints, not trusted sources of correctness: a cache miss must still produce a valid build, and cache keys should include relevant lockfiles and runtime versions. Avoid caching credentials, private build outputs across trust boundaries, or data that could allow one untrusted workflow to influence another.

Release and Deployment Safety

Separate the permission to validate a pull request from the permission to change production. Use a protected environment for approval or deployment restrictions, and grant each job only the permissions it needs. Do not expose deployment credentials to code from an untrusted fork.

Gate a Production Release

This workflow demonstrates a deployable static-site path for repositories configured to use GitHub Pages. The production job runs only after tests and build succeed on the protected default branch, and it deploys the artifact created by the build job.

# Publish the already-built site only after validation succeeds on main.
name: Publish site

on:
  push:
    branches: [main]

permissions:
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      # Build from the pushed revision using the committed dependency lockfile.
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm
      - run: npm ci
      - run: npm test
      - run: npm run build
      # Upload the generated static site as the immutable handoff to deployment.
      - uses: actions/upload-pages-artifact@v3
        with:
          path: public

  deploy:
    # The dependency is the release gate; no deployment occurs after a failed build.
    needs: build
    runs-on: ubuntu-latest
    permissions:
      pages: write
      id-token: write
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      # Deploy the artifact created above instead of building a second copy.
      - id: deployment
        uses: actions/deploy-pages@v4

Plan for Rollback and Verification

A deployment is not complete when a command exits successfully. Verify that the new version is healthy using a smoke test, health signal, or a small canary before expanding traffic. Define how to restore the previous known-good artifact or roll forward with a fix. Keep deployment logs, release identifiers, and operator actions available for incident review.

Pipeline Security and Reliability

CI runs code, installs dependencies, and may hold credentials, so its trust boundaries matter. Treat workflow files and third-party actions as executable code that requires review. The GitHub Actions security hardening guide explains platform-specific controls; apply equivalent least-privilege and isolation principles to other CI systems.

Limit Credentials and Permissions

Grant read-only permissions to validation jobs unless a specific step needs more. Store deployment secrets in the platform's secret manager, scope them to protected environments, and rotate them when exposure is suspected. Prefer short-lived identity federation where supported. Never print tokens in logs or run privileged deployment steps for untrusted pull-request code.

Make Failures Actionable

Set explicit timeouts for jobs that can hang, report test output as an artifact when useful, and distinguish flaky tests from deterministic failures. Do not routinely retry every failing test until it passes: retries can hide races and infrastructure problems. Track failure rate and queue time, then fix the slow or unreliable stage rather than weakening the gate.

Practice Lab

Use a repository with a package lockfile and a build command, or adapt the sequence to the project's actual toolchain. Make the validation workflow required before trying a release workflow.

Create and Verify CI

  1. Add a workflow that runs the repository's documented install, test, and build commands on pull requests.
  2. Open a branch with a deliberate failing test; confirm the check fails and blocks the protected merge.
  3. Fix the test, rerun the workflow, and compare the reported commit with the revision under review.
  4. Inspect logs and permissions to confirm no deployment secret is available to the pull-request job.

Promote and Recover an Artifact

  1. Publish a versioned build artifact only after validation succeeds on the protected branch.
  2. Deploy it to a staging environment and run a smoke test against a stable health endpoint.
  3. Require an authorized production approval, then deploy the same artifact rather than rebuilding it.
  4. Practice restoring the previous artifact in a non-production environment and record the exact recovery steps.