Automating Kubernetes Deployments with GitLab CI/CD
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
- Build & Test: Run unit and integration tests
- Build Image: Create and tag Docker images
- Push Image: Push to container registry
- Deploy to Dev: Automatic deployment to development environment
- Deploy to Staging: Manual approval then deploy to staging
- 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.