End-to-End Parameterized Build Pipeline in Jenkins

Task Overview

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.

Technology Stack

Pipeline Flow

  1. Developer Pushes Code → GitHub
  2. Jenkins Triggers Build (Based on Parameter)
  3. Code Quality Check → SonarQube
  4. Build & Package Application → Maven/Docker
  5. Push Artifacts → Nexus/Docker Hub
  6. Deploy to Kubernetes (EKS) → Terraform & Ansible
  7. Notify Team → Slack

Step-by-Step Implementation

Step 1: Set Up Jenkins with Parameterized Build

Install necessary Jenkins plugins:

Create a New Jenkins Pipeline Job and add the following parameter:

ENVIRONMENT (Choice Parameter: dev, staging, prod)

Step 2: Define the Jenkins Pipeline

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"
        }
    }
}

Step 3: Set Up Kubernetes Manifests

Create different deployment files for dev, staging, and prod in the k8s/ directory.

Deployment for Dev (k8s/deployment-dev.yaml)

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:

Step 4: Provision Infrastructure with Terraform

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"

Step 5: Automate Configuration with Ansible

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

Step 6: Configure Slack Notifications

Final Project Structure

├── 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

Deployment Process

  1. Trigger a Jenkins Build, selecting the environment (dev, staging, or prod).
  2. Jenkins runs the pipeline, building the app and pushing Docker images.
  3. Terraform provisions infrastructure if not already deployed.
  4. Ansible automates deployment using Kubernetes.
  5. Slack Notifies the team of deployment status.

Conclusion

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.