CI/CD Pipeline with Jenkins, Nexus, SonarQube & AWS Services

🔹 Project Overview

The goal is to set up a complete CI/CD pipeline that:

🔹 Tech Stack

🔹 Project Architecture

  1. Developers push code to GitHub.
  2. Jenkins Pipeline triggers on code commit.
  3. SonarQube scans for code quality.
  4. Maven/Gradle builds the application.
  5. Artifacts are stored in Nexus Repository or S3.
  6. Terraform provisions AWS infrastructure.
  7. Ansible configures EC2 instances.
  8. AWS CodeDeploy deploys artifacts to EC2.
  9. Slack notifications inform about pipeline progress.

🔹 Step-by-Step Implementation

Step 1: Setup Jenkins on AWS EC2

  1. Launch an AWS EC2 instance for Jenkins.
  2. Install Java, Jenkins, and required plugins:
  3. sudo apt update
    sudo apt install openjdk-11-jdk -y
    wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
    sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
    sudo apt update
    sudo apt install jenkins -y
    sudo systemctl start jenkins
    sudo systemctl enable jenkins
    
  4. Configure Jenkins with required plugins:

Step 2: Install & Configure Nexus Repository

  1. Launch another EC2 instance for Nexus Repository.
  2. Install Nexus:
  3. sudo yum install -y java-1.8.0-openjdk
    wget https://download.sonatype.com/nexus/3/latest-unix.tar.gz
    tar -xvzf latest-unix.tar.gz
    mv nexus-3* nexus
    sudo mv nexus /opt/
    sudo useradd nexus
    sudo chown -R nexus:nexus /opt/nexus
    sudo su - nexus
    /opt/nexus/bin/nexus start
  4. Access http://:8081 and create a Maven Repository.

Step 3: Install & Configure SonarQube

  1. Launch an EC2 instance for SonarQube.
  2. Install Java & SonarQube:
  3. sudo yum install -y java-11-openjdk
    sudo wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.0.0.45539.zip
    sudo unzip sonarqube-9.0.0.45539.zip -d /opt/
    sudo mv /opt/sonarqube-9.0.0.45539 /opt/sonarqube
    sudo useradd sonar
    sudo chown -R sonar:sonar /opt/sonarqube
    sudo su - sonar
    /opt/sonarqube/bin/linux-x86-64/sonar.sh start
  4. Access http://:9000 and configure SonarQube.

Step 4: Write the Jenkins Pipeline

Jenkinsfile:

pipeline {
    agent any
    environment {
        SONARQUBE_URL = 'http://<SonarQube-IP>:9000'
        NEXUS_REPO = 'http://<Nexus-IP>:8081/repository/maven-releases/'
    }
    stages {
        stage('Checkout Code') {
            steps {
                git branch: 'main', url: 'https://github.com/your-repo.git'
            }
        }
        stage('Code Quality Analysis') {
            steps {
                withSonarQubeEnv('SonarQube') {
                    sh 'mvn sonar:sonar'
                }
            }
        }
        stage('Build and Test') {
            steps {
                sh 'mvn clean package'
            }
        }
        stage('Publish Artifact to Nexus') {
            steps {
                sh 'mvn deploy -DaltDeploymentRepository=nexus::default::${NEXUS_REPO}'
            }
        }
        stage('Deploy to AWS using CodeDeploy') {
            steps {
                sh 'aws deploy create-deployment --application-name MyApp --deployment-group-name MyDeploymentGroup --s3-location bucket=my-s3-bucket,key=my-app.zip,bundleType=zip'
            }
        }
        stage('Slack Notification') {
            steps {
                slackSend(channel: '#devops-alerts', message: 'Deployment Completed!')
            }
        }
    }
}

Step 5: Automate Infrastructure with Terraform

Terraform script for AWS EC2 & S3 bucket:

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "artifact_bucket" {
  bucket = "my-ci-cd-artifacts"
}

resource "aws_instance" "jenkins_server" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.medium"
  key_name      = "my-key"
  security_groups = ["jenkins-sg"]

  tags = {
    Name = "Jenkins-Server"
  }
}

Deploy with Terraform:

terraform init
terraform apply -auto-approve

Step 6: Configure Ansible for EC2 Setup

Ansible playbook to install Apache on EC2:

- name: Install Apache
  hosts: all
  become: true
  tasks:
    - name: Install Apache
      yum:
        name: httpd
        state: present
    - name: Start Apache
      service:
        name: httpd
        state: started

Run playbook:

ansible-playbook -i inventory.ini deploy.yml

Step 7: Deploy with AWS CodeDeploy

  1. Create AppSpec.yml for deployment:
  2. version: 0.0
    os: linux
    files:
      - source: /
        destination: /var/www/html/
    hooks:
      AfterInstall:
        - location: scripts/restart-apache.sh
          timeout: 300
          runas: root
    
  3. Upload AppSpec.yml & App to S3.
  4. Step 8: Add Slack Notifications

    1. Install Slack Plugin in Jenkins.
    2. Configure Slack Webhook.
    3. Modify Jenkinsfile to send messages.

    🚀 Conclusion

    This project helps you master CI/CD automation using AWS DevOps tools. You’ve now: