The goal of this task is to implement a Jenkins CI/CD pipeline that allows users to specify environment parameters before triggering a build. The pipeline will dynamically deploy an application to the specified environment (dev, staging, or prod) using Docker, Kubernetes (EKS), Terraform, and Ansible for automation.
Install necessary Jenkins plugins:
Create a New Jenkins Pipeline Job and add the following parameter:
ENVIRONMENT (Choice Parameter: dev, staging, prod)
Create a Jenkinsfile in the root of your project:
pipeline {
agent any
parameters {
choice(name: 'ENVIRONMENT', choices: ['dev', 'staging', 'prod'], description: 'Choose Deployment Environment')
}
environment {
DOCKER_IMAGE = "your-dockerhub-repo/app:${BUILD_NUMBER}"
KUBE_CONFIG = "/path/to/kube/config"
}
stages {
stage('Checkout Code') {
steps {
git branch: 'main', url: 'https://github.com/your-repo.git'
}
}
stage('Code Quality Check') {
steps {
script {
sh 'sonar-scanner -Dsonar.projectKey=your-project -Dsonar.host.url=http://sonarqube:9000'
}
}
}
stage('Build Application') {
steps {
sh 'mvn clean package'
}
}
stage('Build & Push Docker Image') {
steps {
script {
sh """
docker build -t $DOCKER_IMAGE .
docker login -u $DOCKER_USER -p $DOCKER_PASS
docker push $DOCKER_IMAGE
"""
}
}
}
stage('Deploy to Kubernetes') {
steps {
script {
sh """
kubectl --kubeconfig=$KUBE_CONFIG apply -f k8s/deployment-${params.ENVIRONMENT}.yaml
"""
}
}
}
}
post {
success {
slackSend channel: '#deployments', message: "Deployment to ${params.ENVIRONMENT} successful"
}
failure {
slackSend channel: '#deployments', message: "Deployment to ${params.ENVIRONMENT} failed"
}
}
}
Create different deployment files for dev, staging, and prod in the k8s/
directory.
apiVersion: apps/v1 kind: Deployment metadata: name: app-dev spec: replicas: 2 selector: matchLabels: app: myapp template: metadata: labels: app: myapp spec: containers: - name: myapp image: your-dockerhub-repo/app:latest ports: - containerPort: 8080
Repeat the same for staging and prod, but change:
Create an eks-cluster.tf
for setting up an AWS EKS cluster.
provider "aws" {
region = "us-east-1"
}
resource "aws_eks_cluster" "eks_cluster" {
name = "eks-${var.environment}"
role_arn = aws_iam_role.eks_role.arn
vpc_config {
subnet_ids = var.subnet_ids
}
}
Deploy EKS based on environment:
terraform init terraform apply -var="environment=dev"
Use Ansible to configure Kubernetes and deploy applications.
Create deploy-app.yaml:
- hosts: localhost tasks: - name: Apply Kubernetes Deployment command: kubectl apply -f k8s/deployment-{{ ENVIRONMENT }}.yaml
Run Ansible:
ansible-playbook -e "ENVIRONMENT=dev" deploy-app.yaml
├── Jenkinsfile
├── terraform/
│ ├── eks-cluster.tf
├── ansible/
│ ├── deploy-app.yaml
├── k8s/
│ ├── deployment-dev.yaml
│ ├── deployment-staging.yaml
│ ├── deployment-prod.yaml
├── src/
├── Dockerfile
├── pom.xml
├── README.md
This project demonstrates end-to-end automation for deploying an application across multiple environments using a parameterized Jenkins pipeline. By leveraging Terraform, Ansible, Docker, and Kubernetes, we ensure a scalable, automated, and efficient CI/CD pipeline.