Chapter 4 · Lesson 8 of 18
Deploy and operate

What is CI/CD?

CI/CD is an automated pipeline where continuous integration builds and tests every change and continuous delivery or deployment ships it to your servers.

By the end you will

Explain CI and CD and add a small pipeline that runs your tests on every push.

Time
18 min
Level
Beginner
Check
4 questions
You need
  • A code repository on a host that offers pipelines, such as GitHub

From a push to a running app

CI builds and tests every change when you push, CD publishes the result and releases it, so shipping is the same repeatable steps each time.
  1. You push a change to the repository.
  2. The push triggers the CI pipeline automatically.
  3. CI builds the code and runs the tests, and stops if any fail.
  4. When the tests pass, the build is published as an image.
  5. The delivery step pulls the new image onto your server.
  6. The change is running, with no manual copying.
Static diagram
  1. YoupushRepository
  2. RepositorytriggerCI
  3. CItests passRepository
  4. CIpublishRegistry
  5. RegistrydeployYour server
  6. Your serverliveYou

You fix a typo, copy files to the server by hand, and forget a build step. The site breaks, and nobody remembers exactly what was copied. CI/CD replaces manual, error-prone release steps with a repeatable script that runs on every change.

How it works#

A pipeline is a script that your code host runs for you, on a machine called a runner, every time something happens (usually a push or a pull request). The runner starts clean, so it cannot depend on anything that only exists on your laptop. That is the point: if the build works there, it works for everyone.

Continuous integration (CI) is the first half. Each time someone pushes code, the runner checks it out, installs dependencies, builds it and runs the tests. If something fails, you hear about it within minutes, while the change is still small.

Continuous delivery and deployment (CD) is the second half. After CI passes, the pipeline can deliver the result:

  • Continuous delivery: the build is ready to release, and a person approves the deploy.
  • Continuous deployment: every passing change goes live with no manual step.

A pipeline is a list of stages that run in order, and a failure stops the rest:

  1. Checkout the code.
  2. Build.
  3. Test.
  4. Package, for example a Docker image pushed to a registry.
  5. Deploy, for example by logging in over SSH and running docker compose pull && docker compose up -d.

A tiny example#

Many hosts use a YAML file. This one, in the GitHub Actions style, runs tests on every push:

yaml
name: ci
on: push
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm ci
      - run: npm test

on: push is the trigger. runs-on picks the runner image. Each steps entry runs in order, and a non-zero exit code from any step fails the job. npm ci installs exactly what the lockfile says. Other tools use different file names and keys, but the idea is the same.

To limit overlapping deploys, add a concurrency group to the job that deploys:

yaml
concurrency:
  group: deploy-production
  cancel-in-progress: false

With cancel-in-progress: false, a second run waits instead of cancelling the first.

Check it worked#

Push a small change. In your code host you should see the pipeline start and finish with a green status. Then push a deliberately failing test and confirm the pipeline turns red and stops before any deploy stage. Revert the failing test afterwards.

You can also run the same commands locally to see what the runner will do:

bash
npm ci && npm test; echo "exit code: $?"

The last line prints exit code: 0 when the pipeline’s test stage would pass.

Edge cases and gotchas#

  • It works on my machine, fails in CI. The runner starts clean. Missing environment variables, files ignored by git, or a different Node or Go version are the usual causes. Pin versions in the workflow.
  • Secrets do not reach pull requests from forks. Most hosts withhold secrets from fork builds on purpose, so a deploy or integration test needing them is skipped or fails there.
  • Secrets can leak through logs. A set -x or an echo of an environment variable can print a secret. Hosts mask known secrets, but not transformed values, so never print them.
  • Deploy races. Two quick pushes can run two deploys at once, and the older one may finish last. Use a concurrency setting so only one runs.
  • Architecture mismatch. An image built on an amd64 runner will not run on an ARM server. Build for the architecture your server uses.
  • The deploy key is a risk. If the pipeline can SSH into your server, anyone who can change the workflow can run commands there. Protect the branch and use a key limited to the deploy user.
  • Green does not mean healthy. Tests can pass while production config is wrong. End the pipeline with a check against your real health URL.

Troubleshooting#

SymptomLikely causeFix
Pipeline never startsWorkflow file in the wrong folder or invalid YAMLCheck the path your host requires and validate the YAML
Permission denied (publickey) in the deploy stepWrong or missing SSH key in the secret storeAdd the private key as a secret and the public key to the server
Tests pass locally, fail in CIDifferent versions or missing env varsPin versions, set variables in the workflow
exec format error when the container startsImage built for another CPU architectureBuild for the server’s architecture
Deploy step succeeds but old version is liveServer did not pull the new image or tagUse a unique tag per build and pull it explicitly
Secret value shows as *** in logsThe host masked itExpected; do not try to print it
Two deploys overlapNo concurrency controlAdd a concurrency group
Pipeline is slowDependencies are downloaded each runCache dependencies using your host’s cache feature
Deploy runs on every branchTrigger is too broadLimit the deploy job to the main branch

Real-world scenario#

Two people work on a small API. Before CI, a release meant SSH, git pull, and hoping the build still worked. One Friday a missing dependency in the lockfile took the site down for an hour.

They add the workflow above and a deploy job that only runs on the main branch after tests pass. The next time someone forgets to commit the lockfile, npm ci fails on the runner within a minute, the job turns red, and nothing reaches the server. The fix is a one-line commit.

The deploy job then logs in over SSH and runs:

bash
docker compose pull && docker compose up -d
curl -fsS https://example.com/health

The final curl fails the pipeline if the app does not answer, so a broken release is noticed by the pipeline and not by customers.

Quick reference#

TermMeaning
RunnerThe machine that runs the pipeline
TriggerEvent that starts it, such as on: push
Stage or jobOne step group, run in order
ArtifactOutput kept between jobs, such as a built image
SecretValue stored by the host, read at run time
GateA manual approval or a passing check before deploy

Security and safety notes#

  • Never commit tokens or keys into the workflow file. Use the secret store.
  • Do not give the deploy key more rights than needed. A full-access root key on the server is a large target.
  • Keep a way to go back: see Rollbacks.

Next#

Decide how new versions replace old ones: Blue-green vs rolling deploys.

Check what you learned4 questions
1What is the difference between continuous delivery and continuous deployment?
2A pipeline stage fails. What happens to the later stages?
3Where should pipeline secrets live?
4Two pushes arrive a minute apart and both start a deploy. What can go wrong?

Frequently asked questions

What is the difference between CI and CD?

CI builds and tests each change as it is pushed. CD takes a change that passed and delivers it to an environment, either after a manual approval or automatically.

Do I need CI/CD for a small project?

No, but even a single automated test run on each push catches mistakes early and costs little to set up.

Why does the pipeline pass but my deploy still breaks?

A pipeline only checks what you told it to check. Tests may skip real config, secrets, or the production database, so add a health check after deploy.

Can the pipeline log in to my server?

Yes, usually with an SSH key stored in the pipeline's secret store. Give that key only the access the deploy needs.

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios