AWS Builder Center
Build a CI/CD Pipeline with Jenkins, SonarQube, Docker and Kubernetes on AWS EC2

Build a CI/CD Pipeline with Jenkins, SonarQube, Docker and Kubernetes on AWS EC2

Your screenshot shows a pipeline with nine stages, and build #28 went green. Here is the article, ready to paste into AWS Builder Center.

Aspiring Cloud & DevOps Engineer | AWS | Linux | Cloud Automation

Introduction

Shipping code by hand is slow and easy to get wrong. In this project I built a complete CI/CD pipeline on AWS. Every push to GitHub is compiled, scanned for code quality, packaged, containerized, pushed to Docker Hub, and deployed to Kubernetes automatically.

Architecture

GitHub → Jenkins (EC2) → Maven → SonarQube → Docker → Docker Hub → Kubernetes
Pipeline stages:
  1. Declarative: Tool Install
  1. Git Checkout
  1. Code Compile
  1. SonarQube Analysis
  1. Maven Build
  1. Docker Build
  1. Docker Image Push
  1. Deploy to Kubernetes
  1. Declarative: Post Actions
Tools used: AWS EC2, Jenkins, Maven, SonarQube, Docker, Docker Hub, Kubernetes (kubectl), GitHub.

Step 1: Launch EC2 instances

  1. Launch an Ubuntu EC2 instance (t2.medium or larger) for Jenkins and SonarQube.
  1. Launch instances for the Kubernetes cluster (or use EKS).
  1. Open these ports in the Security Group:
  • 22 (SSH)
  • 8080 (Jenkins)
  • 9000 (SonarQube)
  • 6443 (Kubernetes API, restricted to the Jenkins server’s IP)
  • 30000-32767 (NodePort range for your app)
Tip: Never open ports to  0.0.0.0/0  unless you have to. Restrict them to your own IP wherever possible.

Step 2: Install Jenkins

bash
sudo apt update
sudo apt install openjdk-17-jdk -y
# Add the Jenkins repository, then:
sudo apt install jenkins -y
sudo systemctl enable --now jenkins
Open  http://<EC2-Public-IP>:8080 , unlock Jenkins with the initial admin password, and install the suggested plugins.

Step 3: Install Docker, Maven and kubectl

bash
sudo apt install docker.io maven -y
sudo usermod -aG docker jenkins
sudo systemctl restart jenkins
# install kubectl as per the official Kubernetes docs
Adding  jenkins  to the  docker  group lets pipeline builds run Docker commands.

Step 4: Set up SonarQube

Run SonarQube as a container:
bash
docker run -d --name sonarqube -p 9000:9000 sonarqube:lts-community
  1. Log in at  http://<EC2-IP>:9000 .
  1. Generate a token under My Account → Security.
  1. Create a project and note its project key.

Step 5: Configure Jenkins

Install plugins: SonarQube Scanner, Docker Pipeline, Kubernetes CLI, Pipeline Stage View, Maven Integration.
Add credentials (Manage Jenkins → Credentials):
  •  dockerhub-cred : Docker Hub username and password
  •  sonar-token : the SonarQube token
  •  k8s-config : your kubeconfig file, as a Secret file
Configure tools (Manage Jenkins → Tools): add Maven and JDK.
Connect SonarQube (Manage Jenkins → System): add the SonarQube server URL and token.

Step 6: Write the Jenkinsfile

groovy
pipeline {
agent any
tools { maven 'maven3' }

environment {
IMAGE = "your-dockerhub-user/your-app"
TAG = "${BUILD_NUMBER}"
}

stages {
stage('Git Checkout') {
steps { git branch: 'main', url: 'https://github.com/<you>/<repo>.git' }
}
stage('Code Compile') {
steps { sh 'mvn compile' }
}
stage('SonarQube Analysis') {
steps {
withSonarQubeEnv('sonar') {
sh 'mvn sonar:sonar'
}
}
}
stage('Maven Build') {
steps { sh 'mvn clean package -DskipTests' }
}
stage('Docker Build') {
steps { sh "docker build -t $IMAGE:$TAG ." }
}
stage('Docker Image Push') {
steps {
withCredentials([usernamePassword(credentialsId: 'dockerhub-cred', usernameVariable: 'U', passwordVariable: 'P')]) {
sh 'echo $P | docker login -u $U --password-stdin'
sh "docker push $IMAGE:$TAG"
}
}
}
stage('Deploy to Kubernetes') {
steps {
withKubeConfig(credentialsId: 'k8s-config') {
sh "sed -i 's|IMAGE_TAG|$TAG|g' k8s/deployment.yaml"
sh 'kubectl apply -f k8s/deployment.yaml'
sh 'kubectl apply -f k8s/service.yaml'
}
}
}
}
}

Step 7: Kubernetes manifests

Keep these in a  k8s/  folder in your repo.
deployment.yaml
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 2
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: your-dockerhub-user/your-app:IMAGE_TAG
ports:
- containerPort: 8080
service.yaml
yaml
apiVersion: v1
kind: Service
metadata:
name: my-app-svc
spec:
type: NodePort
selector:
app: my-app
ports:
- port: 80
targetPort: 8080
nodePort: 30080

Step 8: Run the pipeline

Create a Pipeline job, point it at your repo’s Jenkinsfile, and click Build Now. Jenkins’s Stage View shows each stage turning green. The SonarQube Quality Gate shows Passed when your code meets the quality rules.

Troubleshooting: when “Deploy to Kubernetes” fails

My first builds (#25, #26, #27) passed every stage until Deploy to Kubernetes, then failed. Build #25 failed in under a second, while #26 and #27 ran for about 3 minutes before failing. Build #28 passed. Everything before the deploy stage worked, so the problem was between Jenkins and the cluster.
A failure that takes about 3 minutes usually points to a connection timeout. A failure in under a second usually points to a credential or config error. Here is what to check:
  • Kubeconfig credential: is it uploaded correctly in Jenkins, and does the  withKubeConfig  ID match?
  • Network access: can the Jenkins server reach the cluster API on port 6443? Check the Security Group and the server IP in the kubeconfig.
  • Test manually: SSH into the Jenkins server and run  kubectl get nodes  as the  jenkins  user.
  • Manifest errors: check the YAML and the image name and tag.
  • Pod state: run  kubectl get pods  and  kubectl describe pod  to look for image pull errors.
After fixing the connectivity and config, build #28 deployed in about 38 seconds.

Key learnings

  • Automating the whole path from commit to deployment removes manual mistakes.
  • SonarQube’s Quality Gate catches bad code before it ships.
  • Tagging images with the build number makes every deployment traceable and easy to roll back.
  • Most pipeline failures come from credentials, permissions and networking, so check those first.

What’s next

  • Add a GitHub webhook so builds trigger on every push.
  • Move to Amazon EKS and Amazon ECR.
  • Add Slack or SNS notifications for build status.

Connect with me

#vishalcloudjourney #AWS #DevOps #Jenkins #Kubernetes #Docker #SonarQube #CICD
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article