# What a pipeline is for

CI, continuous delivery and continuous deployment, the stages every pipeline shares, why you build once and deploy many, and how environments keep production safe.

Course: [CI/CD Pipelines](https://devops.rajeev.pro/courses/cicd), lesson 1. About 20 minutes, beginner. Source: https://devops.rajeev.pro/courses/cicd/01-what-a-pipeline-is-for

## What you will learn

- Tell continuous integration, continuous delivery and continuous deployment apart
- Put a pipeline's stages in the right order, and explain why the cheapest checks run first
- Explain what an artifact is, and why you build it once and deploy that same one everywhere
- Describe how code moves through development, staging and production

## The problem a pipeline solves

Without a pipeline, a release looks like this: someone pulls the latest code on their laptop,
runs the tests they remember, builds it, copies it to the server and restarts the app. It works
until the day they forget a step, build from the wrong branch, or are on leave.

A **pipeline** is that same list of steps, written down as code and run by a machine on every
change. It never forgets a step, it runs the same way every time, and it tells you within
minutes when a change breaks something, while the change is still fresh in your head.

The machine checks out the commit, runs each step, and reports a pass or failure before the change moves on.
In this lesson, you'll build that path and learn how CI, delivery and deployment differ.

## CI, continuous delivery and continuous deployment

"CI/CD" is three ideas, and people mix them up all the time. Think of them as three steps a team
takes, each removing one more manual job:

1. **Continuous integration.** Everyone merges small changes into `main` at least daily, and every
   push is built and tested automatically. You find out within minutes that a change broke
   something, while you still remember writing it.
2. **Continuous delivery.** On top of that, every change that passes is packaged and deployed to a
   test environment automatically. Releasing to production becomes one button press, and the
   release itself becomes boring.
3. **Continuous deployment.** That last button goes away too: every change that passes all the
   checks goes to production on its own, often many times a day.

The table puts them side by side:

| Term                   | What happens automatically                                | Who sends it to production |
| ---------------------- | --------------------------------------------------------- | -------------------------- |
| Continuous integration | Every push is merged into the main line, built and tested | Not covered                |
| Continuous delivery    | Every passing change is packaged and ready to release     | A person, with one click   |
| Continuous deployment  | Every passing change goes all the way to production       | Nobody: the pipeline does  |

**CI** is the habit: small changes, merged often, each one checked. **Continuous delivery**
adds packaging and deploying to test environments, so releasing is a non-event. **Continuous
deployment** removes the last manual click. Most teams start with delivery and move to
deployment once they trust their tests.

> **Video.** [DevOps CI/CD Explained in 100 Seconds](https://www.youtube.com/watch?v=scEDHsr3APg) (Fireship)

## The stages of a pipeline

Almost every pipeline you'll meet has the same shape, whatever tool runs it:

> **Figure.** A pipeline: a push triggers lint, test and build, which produce one artifact. The same artifact is deployed to staging, then after an approval to production. The first stages are CI, the deploys are CD.
>
> Checks first, then one artifact, promoted from staging to production.

1. **Trigger.** A push, a pull request, a tag or a schedule starts the run.
2. **Lint and static checks.** Formatting, style, type checks. Seconds.
3. **Test.** Unit tests, then the slower integration tests.
4. **Build.** Turn the code into something deployable: the artifact.
5. **Publish.** Store the artifact in a registry, tagged with a version.
6. **Deploy.** To staging first, then to production.

Each stage only runs if the one before it passed. That's the whole idea, and you can see it
with two commands in any shell:

```sh
$ false && echo deployed      # false fails, so the second command never runs
$ true && echo deployed       # true succeeds, so it does
deployed
```

**Fail fast**: put the cheapest check that can catch a mistake first. A missing semicolon
should fail in 10 seconds of linting, not after an 8-minute build.

Here's what that looks like in GitHub Actions, the tool the next lessons use. Don't worry
about every line yet; notice the trigger, the job, and the steps running in order:

```yaml
# .github/workflows/ci.yml
name: ci
on:
  push:
    branches: [main]
  pull_request:

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7          # get the code
      - uses: actions/setup-node@v7
        with:
          node-version: 22
      - run: npm ci                        # install exact versions from the lock file
      - run: npm run lint                  # cheapest check first
      - run: npm test
      - run: npm run build
```

## Artifacts: build once, deploy many

An **artifact** is what the build produces and what you actually deploy: a Docker image, a
`.jar`, a binary, or a folder of static files. It gets a version (a tag, a commit SHA, or for
images a digest) and lives in a registry or artifact store.

The rule that follows: **build it once, then deploy that same artifact everywhere.** If you
rebuild for production, you might pick up a new base image or a new dependency version, and
production ends up running something that never went through staging.

| Do                                                 | Don't                                        |
| -------------------------------------------------- | -------------------------------------------- |
| Build once, tag it `app:3f9c2a1`, promote that tag | Rebuild from the same commit per environment |
| Put environment differences in config and secrets  | Bake the database URL into the artifact      |

## Environments

An **environment** is a place your app runs, with its own config, data and access rules:

- **Development**: where you try things. Breaking it is fine.
- **Staging**: as close to production as you can afford. The last check with real infrastructure.
- **Production**: real users and real data. Changes arrive only through the pipeline.

The artifact moves up this ladder, and only the config changes. Moving it from one environment
to the next is called **promotion**. Production usually has a gate: a required approval, a
deploy window, or both.

> **Figure.** Animated. A change starts on a developer's laptop, is pushed as a pull request, and CI runs lint, unit and integration checks. When they pass it is merged to main, which builds the artifact once and deploys it to staging automatically. Production waits at an approval gate; after approval the same artifact is deployed to production and health-checked. If any gate fails, the change stops there and never reaches users.
>
> Watch one change travel: laptop, pull request, CI, main, staging, approval, production. The same artifact all the way.

> **Nobody deploys from a laptop.** Once you have a pipeline, it must be the **only** way into production. One "quick fix" copied by hand means production no longer matches any commit, and the next pipeline run will quietly undo it.

> **In production.** Keep pipelines fast. Teams that get feedback in under ten minutes merge small changes often; teams that wait an hour batch changes up, and big batches are what make releases scary.

## Mission: Design the pipeline for a small web app

Your team has a Node.js API with ESLint, unit tests and a Dockerfile. Releases are manual and someone broke production last week by deploying the wrong branch. Write ~/pipeline-plan.md: the trigger, each stage in order with what it checks, the artifact and its tag, and how a change reaches production.

- You need: Node.js API: ESLint, unit tests, Dockerfile
- You'll have: ~/pipeline-plan.md

```md
# Pipeline: payments-api

Trigger: every pull request (checks only) and every push to main (full pipeline).

1. Lint: `npm run lint`, 15 s. Fails on style or obvious bugs.
2. Unit tests: `npm test`, 2 min.
3. Build: `docker build`, tag the image with the commit SHA, e.g. `payments-api:3f9c2a1`.
4. Publish: push that image to the registry.
5. Deploy to staging: automatic, then a smoke test hits /health.
6. Deploy to production: the same image tag, after one approval in the pipeline.

Only the pipeline can deploy. Config and secrets come from each environment, never the image.
```

The wrong-branch problem goes away because production only accepts what main built, and the
staging-tested image is the one that ships.

## Try it

- Open `.github/workflows/` in an open-source project you use. Name its triggers, its jobs and the steps of one job.
- In that workflow, find the step that produces an artifact, or explain why there isn't one.
- Write the stages for one of your own projects, cheapest check first.
- Run `false && echo deployed` and `true && echo deployed` in a terminal, and explain the difference in one line.
- Do the mini mission and keep `~/pipeline-plan.md`; you'll build it for real in the next lesson.

## Quiz

1. Every change that passes the pipeline is ready to release, but a person clicks "deploy" to send it to production. What is that called?
   - Continuous integration
   - **Continuous delivery** (answer)
   - Continuous deployment
   Why: Continuous delivery keeps every change releasable and leaves the final step to a person. Continuous deployment removes that click, so every passing change goes live on its own.

2. Linting takes 10 seconds, unit tests 2 minutes and the image build 6 minutes. In what order should they run?
   - Build, tests, lint, so the slow part starts early
   - **Lint, unit tests, build, so a mistake fails in seconds, not minutes** (answer)
   - The order doesn't matter
   Why: Fail fast. The cheapest check that can catch a mistake runs first, so a typo costs 10 seconds of feedback instead of 8 minutes.

3. Staging passed. For production, the pipeline rebuilds the image from the same commit. What's the risk?
   - None, it's the same commit
   - **The new build can differ (a new base image or dependency), so production runs something that was never tested** (answer)
   - Production deploys get slower but nothing else changes
   Why: Build once, deploy many. Promote the exact artifact that passed staging, identified by its tag or digest, so production runs precisely what you tested.

4. What is an artifact?
   - The source code in the repository
   - **The output of a build that you can deploy, like a Docker image, a .jar or a zipped site** (answer)
   - A log file from a failed pipeline
   Why: An artifact is the built, versioned thing you ship. It's stored in a registry or artifact store and deployed unchanged from environment to environment.

## Resources

- [Continuous Integration](https://martinfowler.com/articles/continuousIntegration.html) (Martin Fowler)
- [Continuous Delivery](https://martinfowler.com/bliki/ContinuousDelivery.html) (Martin Fowler)
- [The Twelve-Factor App: build, release, run](https://12factor.net/build-release-run) (12factor.net)
- [Understanding GitHub Actions](https://docs.github.com/en/actions/get-started/understand-github-actions) (GitHub Docs)
- [Workflow syntax for GitHub Actions](https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-syntax) (GitHub Docs)
- [Managing environments for deployment](https://docs.github.com/en/actions/how-tos/deploy/configure-and-manage-deployments/manage-environments) (GitHub Docs)
- [DORA research on software delivery performance](https://dora.dev/) (DORA)
