Project3_DevSecOps_Jenkins_CICD_Pipeline_for-a_Node.js_Application : A Full Guide ๐
In this project we will create our Node.js application using Jenkins, security using SonarQube,OWASP, image scanning using Trivy and Email notifications.
Tool we will need in this project is :-
1.) AWS EC2
2.) Docker, Docker-compose and DockerHub
3.) Github
4.) Jenkins
5.) SonarQube
6.) OWASP DC
7.) Trivy
8.) Email
Prerequisites
Before starting the project you should have these things in your system :-
Account on AWS
Account on GitHub
Code (we will use code from this repository) : click here for code
Part 1 : Initial Setup and Deployment
STEP 1: Launch Instance
Create AWS EC2 instance (t2.large)
Connect to instance through SSH client to my local.
After successfully connecting to the EC2 instance, it will look like this
Part 2 : Setup Jenkins
Now to install Jenkins, first we need Java install because Jenkins need Java so for to install Java use command :-
sudo apt update
sudo apt install fontconfig openjdk-17-jre
To check Java version use command :-
java --version
After installing Java, we will install Jenkins so for to install Jenkins use command :-
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
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
sudo apt-get install jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins
To check Jenkins status, use command :-
sudo service jenkins status
Now copy Public IPv4 address:8080 and we will be on Unlock Jenkins page.
To unlock jenkins, use command :-
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
And we will get our password. Copy and paste it to unlock Jenkins โ Now click Install suggested plugins โ Fill details โ Welcome to Jenkins
Part 3 : Setup Docker and Docker-Compose
Now to install docker and docker-compose use command as follow :-
sudo apt-get update
sudo apt-get install docker.io docker-compose -y
sudo usermod -aG docker $USER
Also add jenkins to docker group for this use command as follow :-
sudo usermod -aG docker jenkins
sudo reboot
Check docker version use command :-
docker version
Now enable docker for this use command :-
sudo systemctl enable docker
Part 4 : Setup SonarQube
Now we will build SonarQube container for this use command :-
docker run -itd --name sonarqube -p 9000:9000 sonarqube:lts-community
Now if we will do docker ps so we will see our SonarQube container is running.
Now open port no. 9000 and copy Public IPv4 address and paste in new tab Public IPv4 address:9000 and we will be on Unlock SonarQube page.
Now enter login and password=admin and it will give us screen to reset our password and we will be on SonarQube page.
Now to make devsecops pipeline, we have create user on sonarqube and this user will have acces given to jenkins. To add user in sonar, Go to SonarQube โ Administrator โ Security โ Users โ Tokens โ Update Tokens โ name "jenkins" โ Generate.
Our sonarqube setup done. Now to put sonar into jenkins we have to Install Plugins. To install sonarqube plugins Go to Manage Jenkins โ Plugins โ Available Plugins โ Search SonarQube Scanner โ install this plugin.
Now SonarQube token put in Jenkins โ Go to Manage Jenkins โ click on Credentials โ System โ Global credentials โ Add Credentials โ Secret text โ in Secret put the token that we copied from SonarQube โ ID "Sonar" โ Description "Sonar" โ Create.
Same as Sonar add Docker Credentials in Jenkins โ Go to Manage Jenkins โ click on Credentials โ System โ Global credentials โ Add Credentials โ Username with password โ in Secret put your username and password of dockerhub โ ID "DockerHub" โ Description "DockerHub" โ Create.
Now we will link our SonarQube with Jenkins, for this Go to Manage Jenkins โ System โ Find SonarQube servers โ Add SonarQube โ Name "Sonar" โ Server URL "http://52.205.89.152:9000" โ Server authentication token "Sonar" โ click on Apply and Save.
We added our SonarQube Server to Jenkins.
Now to enable Sonar Scanner Go to Manage Jenkins โ Tools โ Find SonarQube Scanner installations โ Add SonarQube Scanner โ name "Sonar" โ Version "latest" โ click on Apply and Save.
Now we will create sonar webhooks. Go to Sonar โ Administrator โ Webhooks โ Create โ Name "jenkins" โ URL "http://52.205.89.152:8080/sonarqube-webhook/" โ Create.
Part 5 : Setup Trivy
Now to install trivy use command :-
sudo apt-get install wget apt-transport-https gnupg lsb-release
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo apt-key add -
echo deb https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main | sudo tee -a /etc/apt/sources.list.d/trivy.list
sudo apt-get update
sudo apt-get install trivy
Part 6 : Setup OWASP DC
Now same as SonarQube we have to install OWASP plugins Go to Manage Jenkins โ Plugins โ Available Plugins โ Search OWASP Dependency-Check โ install this plugin.
Now to enable Dependency-Check Go to Manage Jenkins โ Tools โ Find Dependency-Check installations โ Add Dependency-Check โ name "OWASP" โ Install automatically โ Install from github.com โ click on Apply and Save.
Now build a pipeline click on Create a job โ give name "node-app" โ select "Pipeline" โ click OK.
Now add the script in Pipeline Script.
pipeline {
agent any
environment{
SONAR_HOME = tool "Sonar"
}
stages {
stage("Code"){
steps{
git url: "https://github.com/sudhajobs0107/DevSecOps_ToDo_App.git" , branch: "main"
echo "Code Cloned Successfully"
}
}
stage("SonarQube Analysis"){
steps{
withSonarQubeEnv("Sonar"){
sh "${SONAR_HOME}/bin/sonar-scanner -Dsonar.projectName=node-todo-app -Dsonar.projectKey=node-todo-app -X"
}
}
}
stage("SonarQube Quality Gates"){
steps{
timeout(time: 5, unit: "MINUTES"){
waitForQualityGate abortPipeline: false
}
}
}
stage("OWASP"){
steps{
dependencyCheck additionalArguments: '--scan ./', odcInstallation: 'OWASP'
dependencyCheckPublisher pattern: '**/dependency-check-report.xml'
}
}
stage("Build & Test"){
steps{
sh 'docker build -t devsecops-todo-app:latest .'
echo "Code Built Successfully"
}
}
stage("Trivy"){
steps{
sh "trivy image devsecops-todo-app"
}
}
stage("Push to Private Docker Hub Repo"){
steps{
withCredentials([usernamePassword(credentialsId:"DockerHubCreds",passwordVariable:"dockerPass",usernameVariable:"dockerUser")]){
sh "docker login -u ${env.dockerUser} -p ${env.dockerPass}"
sh "docker tag devsecops-todo-app:latest ${env.dockerUser}/devsecops-todo-app:latest"
sh "docker push ${env.dockerUser}/devsecops-todo-app:latest"
}
}
}
stage("Deploy"){
steps{
sh "docker-compose up -d"
echo "App Deployed Successfully"
}
}
}
}
Now click Apply and Save โ Build Now and our pipeline will build successfully.
Our project on SonarQube.
Our run will run perfectly.
Part 7 : Setup Email Integration With Jenkins
First we have to install email plugin.
Go to Manage Jenkins โ Plugins โ Available Plugins โ Search Email Extension Template โ install this plugin.
Now go to your Gmail โ click on your profile โ click on Manage Your Google Account โ> click on the Security tab on the left side panel โ search App Passwords โ Create a password โ you will get page like image given below :-
Now same as Sonar and Docker we will add Email Credentials in Jenkins โ Go to Manage Jenkins โ click on Credentials โ System โ Global credentials โ Add Credentials โ Username with password โ in Secret put sudhajobs0107@gmail.com and password that we created earlier โ ID "email" โ Description "email" โ Create.
Now Go to Manage Jenkins โ System โ Find E-mail notification โ Add STMP=stmp.gmail.com โ click Avanced โ UserName=sudhajobs0107@gmail โ Password=put that we created โ Tick Use SSL โ SMTP Port=465 โ click on Apply.
We have to add one more thing so in System โ Find Extended E-mail Notification โ Add STMP=stmp.gmail.com โ SMTP Port=465 โ click Avanced โ Cedentials=select email that we creted we earlier โ Tick Use SSL โ Default Content Type=HTML โ go down and find Default Triggers โ Tick Always โ click on Apply and Save.
Now in pipeline in the down add code that given below :-
post {
always {
emailext attachLog: true,
subject: "'${currentBuild.result}'",
body: "Project: ${env.JOB_NAME}<br/>" +
"Build Number: ${env.BUILD_NUMBER}<br/>" +
"URL: ${env.BUILD_URL}<br/>",
to: 'postbox.aj99@gmail.com', #change Your mail
attachmentsPattern: 'trivyfs.txt,trivyimage.txt'
}
}
Now build the pipeline and pipeline "SUCCES" or "FAILURE" we will get email like image given below :-
Our DevSecOps for Node.js Application Project is completed ๐.
๐I've put a lot of effort into making the app user-friendly and efficient. Take a look at the project on GitHub and let me know what you think! Your feedback means a lot. Do Repost on Linkedin to share this valuable Project to your Connections.๐
๐ GitHub Repository :https://github.com/sudhajobs0107/DevSecOps_ToDo_App
Let's connect and grow on Linkedin :Click Here
Let's connect and grow on Hashnode :Click Here
Let's connect and grow on Twitter :Click Here
Happy Reading!!!!!
HappyLearning!!!!!
Sudha Yadav
ย