Torna al blog
Sviluppo software

GitHub Actions: how I automated CI/CD for my APIs without touching a server

GitHub Actions made continuous integration accessible to everyone. In this article I share how I build complete CI/CD pipelines for Node.js APIs: from automated tests with JEST to Docker Hub deployment, all inside the repository with no additional infrastructure.

The moment I stopped deploying manually

For years I had a ritual that now makes me smile: finish a feature, run tests locally, build Docker, push to Docker Hub, SSH into the server, pull the image, restart the container. Every time. Manually. The problem wasn't the time — it took ten minutes. The problem was that I was executing a mechanical procedure that a computer can do better than me: without forgetting a step, without getting the tag version wrong, at 3am if necessary. When I started using GitHub Actions seriously, I asked myself why I'd waited so long.

The GitHub Actions model: events, workflows, jobs, steps

Before writing YAML, understanding the model is essential. An Event triggers the workflow (a push, PR, tag, schedule). A Workflow is the YAML file in .github/workflows/. A Job runs on a virtual machine (the runner) and contains multiple steps. A Step is a single command or action. Jobs can run in parallel or sequence, and this composability is the power: lint in one job, tests in another, Docker build in a third, deploy only if all pass.

CI workflow for a Node.js API

The starting point is always a CI workflow triggered on every push. In 30 lines of YAML you have a pipeline that clones the repo, configures Node.js with dependency caching, installs packages and runs tests — on a fresh Ubuntu VM every time, guaranteeing a clean environment.

Testing with JEST and Supertest

For Node.js APIs I use JEST as the test runner and Supertest to make real HTTP requests to my application without starting it on an actual port. Supertest starts the app in memory, makes the HTTP request, and stops it. No port configuration needed, no manual cleanup. It's the approach I teach in the course because it's the most realistic: it tests the real API, not a mock.

Multi-stage Dockerfile for production

The first stage installs dependencies, the second stage copies only what's needed for production. The final image contains no npm, no devDependencies. The USER node instruction runs the process as a non-root user — a basic security measure many forget.

CD workflow: automatic push to Docker Hub

The CD workflow triggers only when I create a tag starting with v (e.g. v1.2.3). The tag automatically becomes the Docker tag, so myapi:v1.2.3 and myapi:latest are updated together. Credentials go in GitHub Secrets — never hardcoded in YAML. GitHub masks them automatically in logs.

Job dependencies: the CI→CD chain

With the needs keyword, the CD job depends on the test job. If tests fail, the build doesn't start. If the build fails, the deploy doesn't start. This creates a quality chain: each stage must be green before the next begins.

What I learned building production pipelines

The most important lesson is cultural: CI/CD automation doesn't just save time — it changes how the team relates to code. When deployment is automatic and safe, developers deploy more often. Small, frequent changes are much easier to debug than large monolithic releases. Bugs are found earlier. Feedback is faster. GitHub Actions is the tool I've found most accessible for starting this journey — no separate infrastructure, already integrated where code lives.

Condividi