LESSON 77 · ADVANCED4 min read

CI/CD & Deployment

Automate verification, build an immutable container, and release it with health checks and a rollback path.

ES2024 / Node.js 20+Free to readModule 17 of 19
YOUR NEXT SMALL WINS

By the end, you’ll be able to…

  • Describe the stages of a continuous integration and delivery pipeline.
  • Write a GitHub Actions workflow that installs, tests, and builds a Node.js service.
  • Deploy a container safely using secrets, health checks, and graceful shutdown.

01CI turns habits into a gate

Continuous integration means every push is merged with the main line and verified automatically. A pipeline checks out the commit, installs from the lockfile, runs linting and tests, and builds the artifact. The point is not ceremony; it is fast, repeatable feedback before a bug reaches users.

Keep the pipeline ordered from cheap to expensive: lint, unit tests, build, integration tests, image build. Fail once, loudly, at the first broken step. Cache dependency downloads, but never cache the result of a test. A flaky pipeline teaches the team to ignore red builds, which is worse than no pipeline.

02A GitHub Actions workflow

GitHub Actions reads YAML from .github/workflows. The on block selects events, jobs run on fresh runners, and steps run in order. actions/checkout fetches the repository, and actions/setup-node installs Node and can cache the npm download directory. Pin major versions of actions and review them like dependencies.

Run npm ci rather than npm install so the lockfile decides versions. Set a read-only GITHUB_TOKEN unless a step needs more. Use a matrix when you genuinely support several Node versions; otherwise one well-chosen version keeps feedback fast. Tag the built image with the commit SHA so the deployed artifact is traceable.

.github/workflows/ci.ymlYAML
name: cion:  push:    branches: [main]  pull_request:permissions:  contents: readjobs:  verify:    runs-on: ubuntu-latest    steps:      - uses: actions/checkout@v4      - uses: actions/setup-node@v4        with:          node-version: 22          cache: npm      - run: npm ci      - run: npm run lint      - run: npm test      - run: docker build -t tasks-api:${{ github.sha }} .

03Secrets are injected, not committed

Secrets belong in the platform's secret store, not in YAML or a .env file in git. GitHub exposes repository or environment secrets to a workflow through the secrets context. Masking hides values in logs, but it is not a licence to print them: pass a token through standard input, and scope it to the step that needs it.

Environment variables configure behaviour, while secrets authorize access. Keep runtime configuration separate from build-time arguments, because a Docker build argument can be read from image history. Rotate credentials, use short-lived tokens where possible, and give the pipeline the smallest permission set that still works.

workflow step (excerpt)YAML
- name: Push image  run: |    echo "${{ secrets.REGISTRY_TOKEN }}" | docker login registry.example.com -u "${{ secrets.REGISTRY_USER }}" --password-stdin    docker push registry.example.com/tasks-api:${{ github.sha }}

04Docker gives you one artifact

A Docker image packages the application with its runtime, so the thing you tested is the thing you deploy. Start from a small official base such as node:22-alpine. Copy package files first and install dependencies in a separate layer, so source edits do not reinstall packages. Then copy source.

Run as a non-root user, expose only the port the app listens on, and add a health check. Use a .dockerignore to keep node_modules, .git, tests, and local secrets out of the build context. Tag images with the commit SHA and never rely on latest for production; latest is a moving label, not a version.

DockerfileDockerfile
FROM node:22-alpine AS depsWORKDIR /appCOPY package*.json ./RUN npm ci --omit=dev && mkdir -p node_modulesFROM node:22-alpine AS runtimeENV NODE_ENV=productionWORKDIR /appCOPY --from=deps /app/node_modules ./node_modulesCOPY package.json ./COPY src ./srcUSER nodeEXPOSE 3000HEALTHCHECK --interval=30s --timeout=3s CMD wget -qO- http://127.0.0.1:3000/health || exit 1CMD ["node", "src/server.js"]

05Health checks, graceful shutdown, and rollback

A health endpoint answers whether the process can serve traffic. Separate liveness from readiness when your platform supports both: liveness asks whether the process is stuck, while readiness asks whether it can accept requests, including database connectivity. A shallow /health is a useful start but will not catch a broken dependency.

On SIGTERM, stop accepting new connections, finish in-flight requests, and exit. Platforms send that signal before replacing an instance, and a process that ignores it causes failed requests during deploys. Deploy immutably tagged images, keep the previous version available, and roll back by redeploying it. After deploy, check /health and one real endpoint, then watch error rate and latency.

server.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
LET IT CLICK

The ideas worth keeping.

  • A CI pipeline should verify changes in a deterministic order and fail at the first broken step.
  • Secrets are injected at run time and scoped narrowly; they never belong in the repository.
  • Immutable images, health checks, and graceful shutdown make deploys observable and reversible.
TURN UNDERSTANDING INTO SOMETHING YOU CAN DO

Make it yours.

Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.

01

Exercise 1: Write a post-deploy smoke test

Write verifyDeploy(baseUrl) that requests /health and /tasks, throws when either response is not ok, and returns the health payload. Assume fetch is available.

your_turn_1.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
Explore a worked solution
one_possible_solution.jsJavaScript
export async function verifyDeploy(baseUrl) {  const health = await fetch(`${baseUrl}/health`);  if (!health.ok) throw new Error(`Health check failed: ${health.status}`);  const tasks = await fetch(`${baseUrl}/tasks`);  if (!tasks.ok) throw new Error(`Tasks check failed: ${tasks.status}`);  return health.json();}

The function checks both a shallow health route and a route that touches application data. Throwing on a non-2xx status turns a bad deploy into a failed release step instead of a silent problem.

02

Exercise 2: Harden a container image

Starting from a Node 22 Alpine base, write a runtime stage that copies the app, switches to the built-in node user, exposes port 3000, adds a health check for /health, and starts src/server.js.

your_turn_2.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
Explore a worked solution
one_possible_solution.jsDockerfile
FROM node:22-alpineENV NODE_ENV=productionWORKDIR /appCOPY package*.json ./RUN npm ci --omit=devCOPY src ./srcUSER nodeEXPOSE 3000HEALTHCHECK --interval=30s --timeout=3s CMD wget -qO- http://127.0.0.1:3000/health || exit 1CMD ["node", "src/server.js"]

The node user limits the damage of a compromised process, and the health check gives the platform evidence that the container is ready. Installing before copying source preserves the dependency layer cache.

A SMALL QUESTION. A USEFUL NEXT STEP.

What stayed with you?

Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.

A LITTLE ACTIVE RECALL0 / 3 answered
01What is the main purpose of continuous integration?
02Where should a container registry token be stored for a GitHub Actions deploy?
03What should a service do when it receives SIGTERM during a rolling deploy?
⚡CODING CHALLENGE
🔒Required to Mark Lesson Complete

Exercise 1: Write a post-deploy smoke test

Write verifyDeploy(baseUrl) that requests /health and /tasks, throws when either response is not ok, and returns the health payload. Assume fetch is available.

main.jsJavaScript
Ctrl + ↵
Execution Console
Click "Run & Verify" (or press Ctrl + Enter) to test your code against the challenge.

Keep exploring.

The official references behind this lesson, when you want a second angle or the full details.

One lesson closer.

Reading is a start. Trying it yourself makes the connection.

Solve the hands-on coding challenge in the knowledge section to verify your code and unlock lesson completion.