Pipelines

Stages, caching, artifacts, secrets, and the gate between a green build and production — in Jenkins and GitHub Actions.

11 min read🔁 CI/CD, Linux and Nginx

A pipeline is the build, run by a machine, on every change, with the results where everyone can see them. That sentence contains the whole value: not that the tests run — you could run them — but that they run the same way every time, on a clean machine, before a change can be merged, and that "it passes on my laptop" stops being an argument. This lesson is the stages a backend pipeline has, the three things that make it fast, and the two tools you will meet.

Stages, and the order that fails fastest

plaintext
lint → unit tests → build the artifact → integration tests → publish → deploy

The order is by cost and by how much a failure tells you. Lint and compile fail in seconds and catch most typos; unit tests in a minute; the build produces the jar that every later stage uses; integration tests — Testcontainers, the contract suite — take minutes and Docker; publishing puts the artifact somewhere; deployment is its own lesson. A stage runs only if the one before passed, and a stage that fails should say what failed in its own output, not in a log somebody has to scroll.

This repository's npm run quality is that pipeline on one machine, and its report is the shape a pipeline's summary should have: each stage named, PASS or FAIL or NOT CONFIGURED, never a green that was not earned. The GitHub workflow runs the same guards on two Node versions and the Java tests on a third job, in parallel, because they do not depend on each other.

GitHub Actions: a workflow file

.github/workflows/ci.ymlyaml
name: ci
on:
  pull_request:
  push: { branches: [main] }
jobs:
  api:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-java@v6
        with: { distribution: temurin, java-version: "25", cache: maven }
      - run: ./mvnw -B -ntp verify
        working-directory: backend/api
      - uses: actions/upload-artifact@v4
        with: { name: api-jar, path: backend/api/target/*.jar, retention-days: 7 }
  contract:
    needs: api
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:16-alpine
        env: { POSTGRES_PASSWORD: contract }
        options: --health-cmd pg_isready --health-interval 5s
    steps:
      - uses: actions/checkout@v7
      - uses: actions/download-artifact@v4
        with: { name: api-jar, path: backend/api/target }
      - run: npm run contract:test

The parts to know: on is the trigger (pull requests, pushes to main, a schedule, a manual workflow_dispatch); a job is a fresh virtual machine, so nothing carries between jobs except what you upload as an artifact or cache; needs orders jobs and lets independent ones run in parallel; services starts containers beside the job — a Postgres for the contract suite — the way Testcontainers does inside a test; and a matrix (strategy: { matrix: { node: [20, 22] } }) runs the same job per value, which is how this repository builds on two Node versions with one job definition. Pin actions to a major version and let Dependabot move them; a @main reference is a supply chain you do not control.

Caching dependencies: the difference between two minutes and twelve

A fresh machine has no ~/.m2 and no node_modules. Without a cache, every run downloads every dependency — for a Spring Boot service, hundreds of megabytes — before it compiles a line. The setup actions cache by lockfile: setup-java with cache: maven keys the cache on pom.xml's hash, setup-node with cache: npm on package-lock.json, so the cache is reused until the dependencies change and rebuilt when they do. The rule that keeps it correct: the cache key is the file that declares the dependencies, never a branch name or a date. A Docker build in CI caches the same way — docker/build-push-action with cache-from: type=gha stores layers between runs, and the Docker course's layer ordering is what makes that cache hit.

Artifacts: build once, promote everywhere

The jar that passed the tests is the jar that should be deployed — not a second build from the same commit, which is probably identical and cannot be proven to be. So the build stage publishes an artifact and every later stage consumes it: a workflow artifact for the next job, a container image pushed to a registry (GHCR, ECR, Artifactory) tagged with the commit SHA for the deploy, a Maven artifact to a repository manager (Nexus, Artifactory) for a library other services depend on. Tags are immutable and traceable — orders-api:3f2a9c1 says which commit — and :latest is for humans typing docker run, never for a deployment.

The repository manager also does the other half of dependency hygiene: it proxies Maven Central so the build does not depend on the internet, and it is where a dependency audit (npm audit, OWASP Dependency-Check, the mvn dependency:tree diff) reports.

Credentials in the pipeline

A pipeline needs credentials — a registry token, a deploy key, a database password for the integration tests — and it is the most-attacked place to keep them, because a pull request from a fork runs code you did not write. The rules: they live in the CI system's secret store, injected as environment variables into the steps that need them and no others; a pull_request from a fork gets none (GitHub does this by default; keep it); prefer short-lived tokens through OIDC federation (Actions to AWS, to GCP, to Vault) over long-lived keys in the store; and mask them in logs, which the store does for exact matches and does not for a value you printed base64-encoded. This repository's hooks scan every diff for credential patterns before a commit lands, which is the layer before the pipeline.

Jenkins, and the other one

Jenkins is a server you run, with a Jenkinsfile in the repository describing stages in a Groovy DSL, agents you provision, and a plugin for everything. GitHub Actions (GitLab CI, and the cloud providers' equivalents) is hosted, YAML, and integrated with the pull request. The trade is control against maintenance: Jenkins can run on your hardware, behind your firewall, with build agents shaped exactly for your workload, and it is a service with upgrades, plugins that break, and a security surface your team owns. A team that has one is not wrong to keep it; a team choosing today usually starts hosted and moves only when a specific need — air-gapped builds, custom hardware, a thousand jobs a day — makes the server worth owning. The stages, the caching and the artifact rules are the same in both.

Under the hood: what "a fresh machine" actually means, and why a cache is not memory

"A job is a fresh machine" is not a metaphor. A GitHub-hosted runner is a short-lived virtual machine, provisioned from a clean image and destroyed after the job finishes — nothing a previous job wrote to its disk, installed as a package, or left running as a process survives into the next one. A self-hosted runner that instead executes each job inside a container gets the same property a different way: the container runtime gives each job its own PID namespace (it cannot see or signal another job's processes), its own mount namespace (its own filesystem view, layered from the image), and its own network namespace, with cgroups capping the CPU and memory it may use. Neither mechanism is about speed — a persistent VM or bare-metal box would run the steps just as fast. It is about not letting one job's state leak into another's: a leftover environment variable, a stray background process, a half-written file, or (the sharper risk) a secret injected into one job's environment that a later, unrelated job could otherwise read off the same disk.

That isolation is also why a cache is a separate mechanism, not the runner remembering anything. actions/cache (and its equivalents) is a key-value store outside the runner: at the start of a job it downloads and extracts an archive matching a key, and at the end, on a cache miss, it uploads a new one under that key. The key in this lesson's workflow is derived from the lockfile's hash, which is the whole design: the cache is addressed by what it contains, not by when it ran or which branch built it. A key that changed dependencies collide the exact way a stale cache would be wrong — reusing yesterday's node_modules for today's package-lock.json — so the cache system refuses that reuse by construction; a job whose lockfile hash does not match anything gets a clean install and writes a new entry.

That combination — a fresh, isolated environment every run, restored only from an addressed cache — is exactly why a pipeline step has to be idempotent: safe to run again with the same inputs and get the same result, because a retry (a flaky network call, a runner that was terminated mid-job, a human clicking "re-run failed jobs") is a fresh machine restoring the same cache and running the same steps again, not a continuation of the run that failed. A step that assumes it is the only one that will ever touch a resource — appending to a file instead of writing it, incrementing a counter instead of setting it, publishing an artifact tag that a second run would collide with — breaks the moment it runs twice, and in CI it will run twice.

Try it yourself

Why doesn't the dependency cache need a lock, but a shared deployment target does?

Two pull requests are opened at nearly the same moment, both changing package-lock.json to add the same new dependency. Both workflow runs compute the same cache key from the new lockfile's hash, and both are cache misses (nothing has written that key yet), so both jobs do a full npm ci and both try to save a new cache entry under the same key when they finish.

Now compare that to two deploys running at the same moment against the same production server's current symlink. Reason about what could actually go wrong in each case, using what you know about how the cache store and the isolated runner behave.

Why one is safe to race and the other is not

The cache race is harmless: both runners are isolated from each other (neither can see the other's filesystem or processes), each one independently produces a correct, complete node_modules from the same lockfile, and whichever one's upload lands second either succeeds (there was no entry yet) or is rejected as a duplicate key — either way, the key still points at a correct node_modules for that lockfile, because both runs computed the same result. There is no shared mutable state whose order of writes matters; the two runs are computing the same idempotent answer independently.

The deploy race is not harmless, because deploy/deploy.sh's current symlink is exactly the kind of shared mutable state the cache never has: two deploys flipping it at nearly the same time can interleave (whichever finishes its health check last wins, regardless of which one was actually meant to ship), and there is no key that makes a second deploy a no-op the way a matching cache key does. That is why a deploy pipeline needs an explicit safeguard a dependency cache does not — a concurrency lock on the deploy job (GitHub Actions' concurrency: key, or a lock file on the server) so only one deploy to a given target runs at a time.

Misconceptions

  • "The cache remembers what the runner did." The runner remembers nothing — it is destroyed after the job. The cache is a separate, addressed store the job explicitly restores from and saves to; a job that never calls the cache action gets no benefit from a previous run's work, however recent.
  • "A pipeline that passed once will pass the same way on a retry." Only if every step is idempotent and hermetic. A step that resolves dependency versions without a lockfile, reaches an external service whose state has changed, or depends on file timestamps can pass on one run and fail — or silently produce something different — on the next, even with no code change.

Sources

  • GitHub Actions documentation, "About GitHub-hosted runners" — the VM lifecycle, one job per instance.
  • GitHub Actions documentation, "Caching dependencies to speed up workflows" — cache key matching and restore behaviour.
  • The Twelve-Factor App, "V. Build, release, run" — strictly separating and never mutating a build once it exists, the same principle that makes an artifact promotable.
Progress is saved on this device and to your account when signed in.