NestJS course Β· Module 9: Deployment and Infrastructure
CI/CD - the legions' automatic forge
In this lesson6
A manual deployment always looks the same: somebody logs on to the server, pulls the code, runs the build, restarts the application. It works - as long as that somebody remembers every step, performs them in the same order, and does not deploy at five o'clock on a Friday.
A legionary forge did not rely on the smith's memory. It had a settled order: the same steel, the same temperature, the same tempering, the same mark on every blade. That is why a sword forged in Gaul fitted a scabbard made in Rome. CI/CD is that order written down in a file.
What the abbreviation means
CI/CD stands for Continuous Integration / Continuous Deployment. Not "Container", not "Component", not "Code": both words refer to the process, not to what is being processed.
Each half solves a different problem. Continuous Integration is the automatic building and testing of code after every change - so that a fault comes to light within minutes, rather than a week later when nobody remembers whose change introduced it. Continuous Deployment is the automatic release of a change that has passed verification - so that the road from finished code to production does not depend on whether somebody has the time.
GitHub Actions
There are many tools; we shall stay with one. GitHub Actions automate build, test and deployment after each push and pull request (PR) - that is their whole role.
Three misunderstandings are worth clearing away at once. They are not used only for managing issues - that is a separate GitHub feature. They are not available only for private repositories; public ones use them free and without a minute limit. And they do not replace Docker - on the contrary, it is in a workflow step that you call docker build and docker push, so the one makes use of the other.
The configuration is a YAML file in the .github/workflows/ directory:
# .github/workflows/deploy.yml
name: Deploy
on:
push:
branches:
- mainThe trigger, or when this should run
The fragment above is the trigger - the statement of the event that starts the workflow. It is written in four nestings, always in this order: on: opens the events section, push: names the particular event, branches: narrows it to chosen branches, and - main is an item in the list of those branches.
In YAML the indentation carries meaning - each further level moves two spaces to the right, and that is what builds the structure. The dash before main marks a list item, so further branches are added on the lines below.
There is more than one event. pull_request: will run the checks on every PR - and that is usually more valuable than push, because it catches a fault before the change reaches the main branch.
The pipeline's four stages
The real work happens in a job, and its steps have a settled order:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm test
- run: npm run build
- run: docker push registry.imperium.rome/legion-api:latestThe order of the stages is not a matter of taste: install dependencies (npm ci) β run linter and tests (npm test) β build the application (npm run build) β deploy to production (docker push).
One principle governs it: the cheaper the stage, the earlier it goes. A linter finishes in seconds, tests in minutes, an image build in a quarter of an hour. Were the build to run before the tests, every typo would cost the full build time - and end in failure regardless.
The two steps at the start are preparation rather than pipeline stages. actions/checkout fetches the repository's code onto the machine the workflow runs on - without it the directory is empty. actions/setup-node installs the stated version of Node.js and thereby settles the "it works on my machine" argument: the version is written in the file, so every run gets the same one.
Note npm ci rather than npm install. The first installs exactly what is recorded in package-lock.json and stops if that disagrees with package.json. The second may quietly raise a dependency's version - and then CI is testing something other than what you have locally.
When a stage fails
The steps run one after another, and the first failure stops the rest. This is the default behaviour and the desirable one: if the tests do not pass, building an image and pushing it to a registry would merely waste time, and at worst deploy a broken version.
Hence a practical division. Run the checks - install, linter, tests - on every push and every PR, on every branch. Restrict deployment to the main branch, through the branches: - main of the earlier section. Work on a feature is then verified from its first commit, while nothing reaches production until the change is merged.
Summary
The forge tempers every blade alike:
- CI/CD stands for Continuous Integration / Continuous Deployment - not Container, not Component, not Code,
- CI is automatic building and testing after every change, CD the automatic release of whatever passed verification,
- GitHub Actions automate build, test and deployment after each push and PR - they are not for issues alone, not reserved for private repositories, and do not replace Docker,
- the configuration lives in
.github/workflows/and is a YAML file whose structure is built by indentation, - the trigger syntax:
on:βpush:βbranches:β- main;pull_request:checks a change before it is merged, - the order of stages:
npm ciβnpm testβnpm run buildβdocker push, cheapest first, actions/checkoutfetches the code,actions/setup-nodefixes a Node.js version identical for every run,npm ciinstalls exactly what is inpackage-lock.json;npm installmay change versions quietly,- the first failed step stops the others - which is why checks run everywhere and deployment only from the main branch.
We shall return to CI/CD in the infrastructure module, with builds across several versions at once, artifacts and dependency caching. For now remember: a pipeline is not there to deploy faster. It is there to deploy the same way - and the speed follows.
Code for this lesson: .github/workflows/ci.yml
# CI/CD Pipeline - Automated Cohortyard of the Empire
name: Roman Empire CI/CD
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
# Environment variables for the entire workflow
env:
NODE_VERSION: '18'
REGISTRY: ghcr.io
jobs:
# === JOB 1: Tests ===
test:
name: Run Tests
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '18'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run linter
run: npm run lint
- name: Run unit tests with coverage
run: npm run test:cov
- name: Run E2E tests
run: npm run test:e2e
# === JOB 2: Build ===
build:
name: Build Application
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build Docker image
run: docker build -t roman-api:latest .
- name: Run container health check
run: |
docker run -d --name test-api -p 3000:3000 roman-api:latest
sleep 10
curl -f http://localhost:3000/health || exit 1
docker stop test-api
# === JOB 3: Deploy ===
deploy:
name: Deploy to Production
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v4
- name: Deploy
run: echo "Deploying Roman Empire API..."
- name: Verify health
run: echo "Checking health endpoint..."
# CI = Continuous Integration (tests on every push)
# CD = Continuous Deployment (automatic deploy after merge)
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. CI/CD stands for:
2. GitHub Actions in the context of CI/CD:
Hands-on tasks in the game
- Code editor
Create .github/workflows/deploy.yml with steps: install, lint, test, build, deploy
- Vertical ordering
Arrange the CI/CD pipeline stages from first to last:
- Vertical ordering
Arrange the GitHub Actions workflow trigger syntax: