Nivas DevSecOps
5 min readSep 7, 2024

CICD — Devops simple structure.

Now, let’s get started and dig deeper into each of these steps:-

Launch an Ubuntu(22.04) T2 MediumInstance

Launch an AWS T2 MediumInstance. Use the image as Ubuntu. You can create a new key pair or use an existing one. Enable HTTP and HTTPS settings in the Security Group and open all ports (not best case to open all ports but just for learning purposes it’s okay).

Install Jenkins, Docker and Trivy

To Install Jenkins

Connect to your console, and enter these commands to Install Jenkins

nano jenkins.sh
#!/bin/bash
sudo apt update -y
wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public | tee /etc/apt/keyrings/adoptium.asc
echo "deb [signed-by=/etc/apt/keyrings/adoptium.asc] https://packages.adoptium.net/artifactory/deb $(awk -F= '/^VERSION_CODENAME/{print$2}' /etc/os-release) main" | tee /etc/apt/sources.list.d/adoptium.list
sudo apt update -y
sudo apt install temurin-17-jdk -y
/usr/bin/java --version
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update -y
sudo apt-get install jenkins -y
sudo systemctl start jenkins
sudo chmod 777 jenkins.sh
sudo su #move into root and run
./jenkins.sh # this will installl jenkins

Once Jenkins is installed, you will need to go to your AWS EC2 Security Group and open Inbound Port 8080, since Jenkins works on Port 8080.

Now, grab your Public IP Address

<EC2 Public IP Address:8080>
sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Unlock Jenkins using an administrative password and install the suggested plugins.

Jenkins will now get installed and install all the libraries.

Create a user click on save and continue.

Jenkins Getting Started Screen.

Install Docker

sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker $USER #my case is ubuntu
newgrp docker
sudo chmod 777 /var/run/docker.sock

After the docker installation.

Next, we will log in to Jenkins and start to configure our Pipeline in Jenkins

Install Plugins like JDK, Maven, Docker

Install Plugin

Goto Manage Jenkins →Plugins → Available Plugins →

Install below plugins

Blue ocean

1 → Eclipse Temurin Installer

2 → Maven

3 → Blueocean

4 → Docker

5 → Docker commons

6 → Docker pipeline

7 → Docker API

8 → Docker Build step

Configure Java and Nodejs in Global Tool Configuration

Goto Manage Jenkins → Tools → Install JDK(17) and NodeJs(19)→ Click on Apply and Save

Go to Jenkins Dashboard → Manage Jenkins → Credentials → Add Secret Text. It should look like this

You will this page once you click on create.

Now, goto Dashboard → Manage Jenkins → Tools →

Go to manage Jenkins –> Credentials

Add DockerHub Username and Password under Global Credentials

Let’s add a pipeline

pipeline {
agent any

tools {
maven 'maven3'
jdk 'jdk17'
}

stages {
stage('Git Checkout') {
steps {
git 'https://github.com/nivas-22/task-master-pro.git'
}
}

stage('Compile the code') {
steps {
sh 'mvn compile'
}
}

stage('Build the Application') {
steps {
sh 'mvn package'
}
}

stage('Build and Tag Docker Image') {
steps {
script {
withDockerRegistry(credentialsId: 'docker', toolName: 'docker') {
sh 'docker build -t maven .'
sh 'docker tag maven nivasheart22/taskmasterpro:latest'
}
}
}
}

stage('Push Image to DockerHub') {
steps {
script {
withDockerRegistry(credentialsId: 'docker', toolName: 'docker') {
sh 'docker push nivasheart22/taskmasterpro:latest'
}
}
}
}

stage('Remove Container') {
steps {
sh 'docker stop maven|| true'
sh 'docker rm maven || true'
}
}

stage('Deploy to Container') {
steps {
sh 'docker run -d --name maven -p 3000:8080 nivasheart22/taskmasterpro:latest'
}
}
}
}

Run the Pipeline using Build Now

Docker Hub registry :

<Jenkins-public-ip:3000>

Output:

ERROR :

If you find error in docker login execute the below steps

sudo usermod -aG docker $USER
groups $USER
docker ps
sudo systemctl start docker
sudo systemctl enable docker
ls -l /var/run/docker.sock
sudo chown root:docker /var/run/docker.sock
sudo usermod -aG docker jenkins
sudo systemctl restart jenkins

Hope it helps..!

Thank you for reading so far! Before you go:

Nivas DevSecOps
Nivas DevSecOps

Written by Nivas DevSecOps

Cloud | DevSecOps| AWS ⭐Passionate Cloud and DevOps . 🎯 Like to stay up-to-date with the latest trends and insights.

No responses yet