Automating Kubernetes Deployments with GitLab CI/CD

April 10, 2025
Kubernetes
GitLab
CI/CD
DevOps
Automation

In my current role, I significantly improved software delivery by architecting and implementing a fully automated GitLab CI/CD pipeline deploying Next.js applications on AWS EKS. This article shares the approach and lessons learned.

The Challenge

Before automation, our deployment process was manual, error-prone, and time-consuming. 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 Docker images
  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 install
          - 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
      

Kubernetes Manifests Management

We use Helm charts to manage our Kubernetes manifests, with environment-specific values files.

Secrets Management

Sensitive information is stored in GitLab CI/CD variables and Kubernetes secrets.

Results

After implementing this pipeline, we saw:

  • Deployment time reduced from hours to minutes
  • Elimination of manual errors
  • Improved developer productivity
  • Better visibility into the deployment process

Conclusion

Automating Kubernetes deployments with GitLab CI/CD has transformed our development workflow, making it faster, more reliable, and more transparent.