Automating Kubernetes Deployments with GitLab CI/CD

April 10, 2025
Kubernetes
AWS EKS
GitLab CI
CI/CD
Docker

In my current role, I architected a fully automated GitLab CI/CD pipeline deploying Next.js applications to AWS EKS, replacing manual deployments with repeatable, auditable releases. This article shares the general approach and lessons learned.

The Challenge

Before automation, deployments were manual, which made them hard to repeat and hard to audit. Developers had to build images locally, push them to a registry, and manually update Kubernetes manifests.

The Solution: GitLab CI/CD Pipeline

We created a multi-stage pipeline that handles everything from testing to production deployment:

Pipeline Stages

  1. Build & Test: Run unit and integration tests
  2. Build Image: Create and tag images with multi-stage Docker builds
  3. Push Image: Push to container registry
  4. Deploy to Dev: Automatic deployment to development environment
  5. Deploy to Staging: Manual approval then deploy to staging
  6. Deploy to Production: Manual approval then deploy to production

GitLab CI Configuration

Here's a simplified version of our .gitlab-ci.yml file:

stages:
  - test
  - build
  - deploy-dev
  - deploy-staging
  - deploy-prod

test:
  stage: test
  script:
    - npm ci
    - npm test

build:
  stage: build
  script:
    - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA

deploy-dev:
  stage: deploy-dev
  script:
    - kubectl set image deployment/app app=$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
  environment:
    name: development

# Similar configurations for staging and production

Multi-Stage Docker Builds

The image is built in stages, so build tooling and dev dependencies never reach the image that runs in the cluster:

FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:22-alpine
WORKDIR /app
COPY --from=build /app/.next/standalone ./
COPY --from=build /app/.next/static ./.next/static
EXPOSE 3000
CMD ["node", "server.js"]

Kubernetes Manifests Management

Helm charts manage the Kubernetes manifests, with environment-specific values files.

Secrets Management

Secrets belong in a dedicated store, not in the repo or in hand-maintained pipeline config. I integrated Azure Key Vault (Government Cloud) for this.

TLS Behind a Corporate Proxy

In restricted environments, a corporate proxy can break TLS for package installs, image pulls and calls to cloud APIs. Disabling certificate verification is the tempting fix and the wrong one. The right fix is to establish trust explicitly by adding the required CA certificates to build images and tooling.

Results

  • Manual deployments replaced with an automated pipeline
  • Releases that are repeatable and auditable
  • Better visibility into what is deployed and how it got there

Conclusion

A pipeline like this is the paved road: product teams get to production by merging code, without filing a ticket or running a deployment by hand.