> For the complete documentation index, see [llms.txt](https://bhushans-devops-organization.gitbook.io/advanced-projects/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bhushans-devops-organization.gitbook.io/advanced-projects/deploying-a-youtube-clone-app-with-devsecops-and-jenkins-shared-library.md).

# Deploying a YouTube Clone App with DevSecOps and Jenkins Shared Library

<figure><img src="/files/xCkqJrl2rayMepd7oBzY" alt=""><figcaption></figcaption></figure>

## Step 1: Launch an Ubuntu 22.04 instance for Jenkins <a href="#heading-step-1-launch-an-ubuntu-2204-instance-for-jenkins" id="heading-step-1-launch-an-ubuntu-2204-instance-for-jenkins"></a>

**Log into AWS Console:** Sign in to your AWS account.

**Launch an Instance:**

Choose **"EC2"** from services. Click "Launch Instance."

**Choose an AMI:** Select an Ubuntu image.

**Choose an Instance Type:** Pick "t2.large."

**Key Pair:** Choose an existing key pair or create a new one.

**Configure Security Group:**

Create a new security group. Add rules for HTTP, and HTTPS, and open all ports for learning purposes. Add Storage: Allocate at least 20 GB of storage.

\[ opening all ports is not recommended for production environments; it's just for educational purposes. ]

**Launch Instance:** Review and launch the instance.

Access Your Instance: Use SSH to connect to your instance with the private key.

<figure><img src="/files/zmChegDCzDM7QbLEbljI" alt=""><figcaption></figcaption></figure>

For the installation of the Jenkins follow the official Document.

{% embed url="<https://www.jenkins.io/doc/book/installing/linux/>" %}

You will need to go to your AWS EC2 Security Group and open Inbound Port 8080 since Jenkins works on Port 8080.

```
<EC2 Public IP Address:8080>
```

For unlocking the Jenkins use this command

```
sudo cat /var/lib/jenkins/secrets/initialAdminPassword

```

<figure><img src="/files/fnL0MfZhoWfKVYF3ilHY" alt=""><figcaption></figcaption></figure>

Jenkins Dashboard

## Step2A: Install Docker on the Jenkins machine <a href="#heading-step2a-install-docker-on-the-jenkins-machine" id="heading-step2a-install-docker-on-the-jenkins-machine"></a>

Run the below commands to install the 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, we will create a Sonarqube container (Remember to add 9000 ports in the security group).

Run this command on your EC2 instance to create a SonarQube container:

```
docker run -d --name sonar -p 9000:9000 sonarqube:lts-community

```

<figure><img src="/files/Qto3FmI8mXrfjuZGLrdr" alt=""><figcaption></figcaption></figure>

Now copy the IP address of the ec2 instance

```
<ec2-public-ip:9000>
```

<figure><img src="/files/m9jROgSzygB3RkEqSDqn" alt=""><figcaption></figcaption></figure>

Enter username and password, click on login and change password

```
username admin
password admin
```

#### SonarQube Dashboard

<figure><img src="/files/0OKAWMq8nAQtg4RXFurB" alt=""><figcaption></figcaption></figure>

#### Step2B: Install Trivy on Jenkins machine <a href="#heading-step2b-install-trivy-on-jenkins-machine" id="heading-step2b-install-trivy-on-jenkins-machine"></a>

```
sudo vi trivy.sh

```

```
sudo apt-get install wget apt-transport-https gnupg lsb-release -y
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor | sudo tee /usr/share/keyrings/trivy.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] 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 -y

```

Provide executable permissions and run the shell script

```
// Some codesudo chmod +x trivy.sh

./trivy.sh

```

<figure><img src="/files/tyE7he6xpCHyWPzBy2cc" alt=""><figcaption></figcaption></figure>

This will install Trivy on our Jenkins machine.

<figure><img src="/files/IRSHTWiKdHSWQ7xyXOGd" alt="" width="469"><figcaption></figcaption></figure>

## Step3A: Launch an Ubuntu instance for Splunk <a href="#heading-step3a-launch-an-ubuntu-instance-for-splunk" id="heading-step3a-launch-an-ubuntu-instance-for-splunk"></a>

**Step 1: Launch Instances**

1. Log in to your AWS console or your chosen cloud provider.
2. Navigate to the EC2 service and launch Ubuntu 22.04 instances. Ensure you select T2.medium as the instance type and allocate 24GB of storage to each instance.

**Step 2: Install Splunk**

At this point, the first machine is set up with Jenkins. You can now move to the second machine and proceed with the installation of Splunk.

* Connect to your second instance using Putty or Mobaxtreme.

  To download and install Splunk on your Ubuntu instance use the `wget` command, use the following command.

```
wget -O splunk-9.1.1-64e843ea36b1-linux-2.6-amd64.deb "https://download.splunk.com/products/splunk/releases/9.1.1/linux/splunk-9.1.1-64e843ea36b1-linux-2.6-amd64.deb"
```

<figure><img src="/files/Hprj4V8WWr3UG4v08Os7" alt=""><figcaption></figcaption></figure>

To Depackage the Splunk use the below command

```
sudo dpkg -i splunk-9.1.1-64e843ea36b1-linux-2.6-amd64.deb

```

```
sudo /opt/splunk/bin/splunk enable boot-start

```

By running this command, you ensure that Splunk Enterprise is configured to start automatically when your Ubuntu system boots, allowing you to seamlessly integrate it into your workflow.

Please note that after running this command, you should follow the on-screen prompts to accept the terms and complete the setup to 100%.

After completing the initial setup and accepting the terms, you'll be prompted to create an admin user.

**Administrator Username:** Choose a username for the admin account. This should be a unique and secure username.

**Administrator Password:** Set a strong and secure password for the admin account. It's important to choose a password that combines upper and lower-case letters, numbers, and special characters for enhanced security.

Confirm your password to ensure it matches the one you initially entered.

By creating an administrator username and password, you'll have full access to your Splunk instance, allowing you to configure and manage it effectively.

<figure><img src="/files/iW2dLk5MElafKa7ZCTq9" alt=""><figcaption></figcaption></figure>

```
sudo ufw allow openSSH
sudo ufw allow 8000
sudo ufw status
sudo ufw enable
```

<figure><img src="/files/QHDDB8wlt5hU58qrPquA" alt="" width="563"><figcaption></figcaption></figure>

```
sudo /opt/splunk/bin/splunk start

```

<figure><img src="/files/7iGGCnJgwRvdQQG5ecin" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/X0GeoUjhvbSHq4ecLM4m" alt=""><figcaption></figcaption></figure>

After Installing set up the Splunk Account- For reference please check official document

## Step3B: Install the Splunk app for Jenkins <a href="#heading-step3b-install-the-splunk-app-for-jenkins" id="heading-step3b-install-the-splunk-app-for-jenkins"></a>

In Splunk Dashboard

Click on Apps --> Find more apps

<figure><img src="/files/6LAjcuElORG6KvTsStLg" alt=""><figcaption></figcaption></figure>

Search for Jenkins in the Search bar

### You will get the Splunk app for Jenkins and click on install

<figure><img src="/files/1wDrPk4cyluSgEs3g7wC" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/aJxnCniUNyzWLgQp2pP8" alt=""><figcaption></figcaption></figure>

On the homepage of Splunk, you will see Jenkins has been added

<figure><img src="/files/jywymZWxRTZ3J90Tdv9m" alt="" width="563"><figcaption></figcaption></figure>

In the Splunk web interface, go to Settings > Data Inputs.

<figure><img src="/files/oXvvbGI8AvZKtIqaQNC2" alt="" width="563"><figcaption></figcaption></figure>

Click on HTTP Event Collector. ->  Click on Global Settings

\
&#x20;Set All tokens to enabled

Uncheck SSL enable

Use 8088 port and click on save

<figure><img src="/files/58NPCyBcefwhsAhV6y3e" alt=""><figcaption></figcaption></figure>

Now click on New token -> Provide a Name and click on the next

<figure><img src="/files/zoE4pTTGWQfXPKJM527r" alt=""><figcaption></figcaption></figure>

Click Review & Submit&#x20;

<figure><img src="/files/8HMKe42d5WsixXNYHCKa" alt=""><figcaption></figcaption></figure>

Click Start searching

<figure><img src="/files/ddjApzyGZU7hMxJgiJHh" alt=""><figcaption></figcaption></figure>

Now let's copy our token again

In the Splunk web interface, go to **Settings** > **Data Inputs**. -> Click on the HTTP event collector

Now copy your token and keep it safe

<figure><img src="/files/FnlrJTUByNNYV0obGJGg" alt=""><figcaption></figcaption></figure>

## Add Splunk Plugin in Jenkins <a href="#heading-add-splunk-plugin-in-jenkins" id="heading-add-splunk-plugin-in-jenkins"></a>

Go to Jenkins dashboard

Click on Manage Jenkins --> Plugins --> Available plugins

Search for Splunk and install it.

<figure><img src="/files/CMN6Hdwsi6dnAJL7bmzW" alt=""><figcaption></figcaption></figure>

Again Click on Manage Jenkins --> System

Search for Splunk

Check to enable

HTTP input host as SPLUNK PUBLIC IP

HTTP token that you generated in Splunk

Jenkins IP and apply.

<figure><img src="/files/XAHE8WcDby5YoMHlyfgg" alt=""><figcaption></figcaption></figure>

Now go to Putty or Mobaxtreme and In Splunk machine run this command

```
sudo ufw allow 8088

```

<figure><img src="/files/gCzJ8y7BUmKzrpazxl4s" alt="" width="486"><figcaption></figcaption></figure>

### Restart Both Splunk and Jenkins <a href="#heading-restart-both-splunk-and-jenkins" id="heading-restart-both-splunk-and-jenkins"></a>

Let's Restart our Splunk machine

Click on Settings --> Server controls

<figure><img src="/files/y2v7CPXFXyoO685LtjCv" alt="" width="563"><figcaption></figcaption></figure>

Now restart Jenkins and log in again.

```
<jenkins-ip:8080/restart> #this will restart jenkins

```

<figure><img src="/files/Mt2pnW9wq9Wz7y5k7Rr4" alt=""><figcaption></figcaption></figure>

Now go to Splunk and click on the Jenkins app and you will get this output monitoring

<figure><img src="/files/tls4zYMHH7YcZSAmvG7g" alt=""><figcaption></figcaption></figure>

<br>

## Step4A: Integrate Slack for Notifications <a href="#heading-step4a-integrate-slack-for-notifications" id="heading-step4a-integrate-slack-for-notifications"></a>

<figure><img src="/files/prec5n2V4RbyUXGmP3DC" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/CoLn0nCJddh2YcUB8YEL" alt=""><figcaption></figcaption></figure>

### Create a Slack account <a href="#heading-create-a-slack-account" id="heading-create-a-slack-account"></a>

Open your preferred web browser (e.g., Google Chrome, Mozilla Firefox, Safari, or Microsoft Edge).

In the address bar, type or click on the following URL to access Slack's official website: [https://slack.com](https://slack.com/)

On the Slack homepage, you'll find a prominent "SIGN UP WITH GOOGLE" or "Try for Free" button. Click on it

<figure><img src="/files/EjLwb3litoJABMdhZFA1" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/phRCRPzbP7zE3JY1dZKB" alt=""><figcaption></figcaption></figure>

### Add the Channel -

<figure><img src="/files/C8e4lDVjCmf8aYAcFYtO" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/BRKtMJFqMLMBh6rq5LOX" alt=""><figcaption></figcaption></figure>

## **Step4B: Install the Jenkins CI app on Slack** <a href="#heading-step4b-install-the-jenkins-ci-app-on-slack" id="heading-step4b-install-the-jenkins-ci-app-on-slack"></a>

Go to Slack and click on your name

Select Settings and Administration

Click on Manage apps

<figure><img src="/files/ASuX6wkawwV6qxdRYoTa" alt=""><figcaption></figcaption></figure>

It will open a new tab

Search for Jenkins CI and click on it

<figure><img src="/files/mzKIWcMLP5WoLa8XBLEQ" alt=""><figcaption></figcaption></figure>

Now choose your Slack channel

Click on Add Jenkins CI integration.

<figure><img src="/files/R2ZJ2yQZxYi9YmDxpekb" alt=""><figcaption></figcaption></figure>

You will be redirected to this page

<figure><img src="/files/C9WpCUtPpZYRSmdmrEn3" alt=""><figcaption></figcaption></figure>

Copy the team subdomain and integration token credential ID for later use.

<figure><img src="/files/pQrMkGSZ907da2tynnNB" alt=""><figcaption></figcaption></figure>

* **Team subdomain:** `mrbhushan`
* **Integration token credential ID:** Create a secret text credential using `LpaDDc2n96KFOLtQ4mDqkxEQ` as the value

### Install Slack Notification Plugin in Jenkins <a href="#heading-install-slack-notification-plugin-in-jenkins" id="heading-install-slack-notification-plugin-in-jenkins"></a>

Go to Jenkins Dashboard

Click on manage Jenkins --> Plugins --> Available plugins

Search for Slack Notification and install

<figure><img src="/files/PBE3vOLdmUqvkJjiAC8Z" alt=""><figcaption></figcaption></figure>

Click on Manage Jenkins --> Credentials --> Global

Select kind as Secret Text

At Secret Section Provide Your Slack integration token credential ID

Id and description are optional and create

<figure><img src="/files/M64WK7lMpNGAVVysU1Up" alt=""><figcaption></figcaption></figure>

Click on Apply and save

manage Jenkins -> add secrete credentials of slack & workspace is slack subdomain

<figure><img src="/files/qQynrUtUJIRzOQp0ki2N" alt=""><figcaption></figcaption></figure>

Apply & save.

## **Step5A: Start Job** <a href="#heading-step5a-start-job" id="heading-step5a-start-job"></a>

Go to Jenkins dashboard and click on New Item.

Provide a name for the Job & click on Pipeline and click on OK.

## Step5B: Create a Jenkins shared library in GitHub  <a href="#heading-step5b-create-a-jenkins-shared-library-in-github" id="heading-step5b-create-a-jenkins-shared-library-in-github"></a>

Create a new repository in GitHub named Jenkins\_shared\_library.

<figure><img src="/files/1D3MRtw81ctyPM43k6JQ" alt=""><figcaption></figcaption></figure>

Connect to your VS Code

Create a directory named Jenkins-shared-library

Create a Vars directory inside it

<figure><img src="/files/jLzm7vfEwx2Nc3D1narX" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/hdraykOdnyqxS7I2eS33" alt=""><figcaption></figcaption></figure>

```
echo "# Jenkins_shared_library" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
# make sure to change your repo Url here
git remote add origin https://github.com/Bhushan0151/Jenkins-shared-library.git
git push -u origin main

```

Now, Let's Write a Groovy script for our Pipeline

Create a **cleanWorkspace.groovy** file and add the below code

```
#cleanWorkspace.groovy //cleans workspace
def call() {
    cleanWs()
}

```

Create **checkoutGit.groovy** file and add the below code

```
def call(String gitUrl, String gitBranch) {
    checkout([
        $class: 'GitSCM',
        branches: [[name: gitBranch]],
        userRemoteConfigs: [[url: gitUrl]]
    ])
}

```

Now push them to GitHub using the below commands from vs code

```
git add .
git commit -m "message"
git push origin main

```

<figure><img src="/files/lfvTdEAUSGTMNkhBWozw" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/iqRruM45RveSQ7J4cRbj" alt=""><figcaption></figcaption></figure>

## Step5C: Add Jenkins shared library to Jenkins system <a href="#heading-step5c-add-jenkins-shared-library-to-jenkins-system" id="heading-step5c-add-jenkins-shared-library-to-jenkins-system"></a>

Go to Jenkins Dashboard

Click on Manage Jenkins --> system

Search for Global Pipeline Libraries and click on Add

<figure><img src="/files/lJ1WnZNcACEVp0SIeLyQ" alt=""><figcaption></figcaption></figure>

Now Provide a name that we have to call in our pipeline<br>

<figure><img src="/files/511kQ0Qgxx1gFhwsCDIN" alt=""><figcaption></figcaption></figure>

## Step5D: Run Pipeline <a href="#heading-step5d-run-pipeline" id="heading-step5d-run-pipeline"></a>

Go to Jenkins Dashboard again & select the job and add the below pipeline

```
@Library('Jenkins-shared-library')_     // name used in jenkins library

def COLOR_MAP = [
    'FAILURE' : 'danger',
    'SUCCESS' : 'good'
]


pipeline{
    agent any
    parameters {
        choice(name: 'action', choices: 'create\ndelete', description: 'Select create or destroy.')
    }
    stages{
        stage('clean workspace'){
            steps{
                cleanWorkspace()
            }
        }
        stage('checkout from Git'){
            steps{
                checkoutGit('https://github.com/Bhushan0151/Youtube-clone-app.git', 'main')
            }
        }
     }
     post {
         always {
             echo 'Slack Notifications'
             slackSend (
                 channel: '#jenkins',   // change your channel name
                 color: COLOR_MAP[currentBuild.currentResult],
                 message: "*${currentBuild.currentResult}:* Job ${env.JOB_NAME} \n build ${env.BUILD_NUMBER} \n More info at: ${env.BUILD_URL}"
               )
           }
       }
   }
```

<figure><img src="/files/3ccqrZZaJiVUPaJhizTn" alt=""><figcaption></figcaption></figure>

### **Slack Notifications-** <a href="#heading-step6-install-plugins-like-jdk-sonarqube-scanner-nodejs" id="heading-step6-install-plugins-like-jdk-sonarqube-scanner-nodejs"></a>

<figure><img src="/files/y4KW5DADsxfrsfMFDv98" alt=""><figcaption></figcaption></figure>

## **Step6**: **Install Plugins like JDK, Sonarqube Scanner, NodeJs** <a href="#heading-step6-install-plugins-like-jdk-sonarqube-scanner-nodejs" id="heading-step6-install-plugins-like-jdk-sonarqube-scanner-nodejs"></a>

### **Step6A: Install Plugin** <a href="#heading-step6a-install-plugin" id="heading-step6a-install-plugin"></a>

Goto Manage Jenkins →Plugins → Available Plugins →

Install below plugins

1 → Eclipse Temurin Installer (Install without restart)

2 → SonarQube Scanner (Install without restart)

3 → NodeJs Plugin (Install Without restart)

<figure><img src="/files/wZUL2cW9Oq01vkOk86Ab" alt=""><figcaption></figcaption></figure>

### **Step6B: Configure Java and Nodejs in Global Tool Configuration** <a href="#heading-step6b-configure-java-and-nodejs-in-global-tool-configuration" id="heading-step6b-configure-java-and-nodejs-in-global-tool-configuration"></a>

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

<figure><img src="/files/YveXggRO3rANTsQugO7Z" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/QYP6qGzA6JxUdv5yP3vu" alt=""><figcaption></figcaption></figure>

### **Step6C: Configure Sonar Server in Manage Jenkins** <a href="#heading-step6c-configure-sonar-server-in-manage-jenkins" id="heading-step6c-configure-sonar-server-in-manage-jenkins"></a>

Grab the Public IP Address of your EC2 Instance, Sonarqube works on Port 9000, so \<Public IP>:9000.

&#x20;Goto your Sonarqube Server. Click on Administration → Security → Users → Click on Tokens and Update Token → Give it a name → and click on Generate Token ->click on update Token

<figure><img src="/files/zVpExRMXWGev415CBWuq" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/EsizdpOWqDGbQ5fs3Zko" alt=""><figcaption></figcaption></figure>

copy Token

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

<figure><img src="/files/m0rUkMO0MXs7twPceaxW" alt=""><figcaption></figcaption></figure>

Now, go to Dashboard → Manage Jenkins → System and Add like the below image.

Click on Apply and Save.

<figure><img src="/files/XmlOfl1014Fc7vQkBUJs" alt=""><figcaption></figcaption></figure>

**The Configure System option** is used in Jenkins to configure different server

**Global Tool Configuration** is used to configure different tools that we install using Plugins

We will install a sonar scanner in the tools.

<figure><img src="/files/j6bjlo3stFPhNplMIyls" alt=""><figcaption></figcaption></figure>

In the Sonarqube Dashboard add a quality gate also

Administration--> Configuration-->Webhooks

<figure><img src="/files/vIzje7pubEihhXihRj03" alt=""><figcaption></figcaption></figure>

Click on Create

<figure><img src="/files/DiBJOwsDwcNeBhl7vwCh" alt=""><figcaption></figcaption></figure>

Add details

```
#in url section of quality gate
```

```
<http://jenkins-public-ip:8080>/sonarqube-webhook/

```

<figure><img src="/files/Cf1xX9g7Bbr28WaTkPvc" alt=""><figcaption></figcaption></figure>

### Step6D: Add New stages to the pipeline <a href="#heading-step6d-add-new-stages-to-the-pipeline" id="heading-step6d-add-new-stages-to-the-pipeline"></a>

Go to vs code and create a file **sonarqubeAnalysis.groovy** & add the below code and push to Jenkins shared library GitHub Repo.

```

def call() {
    withSonarQubeEnv('sonar-server') {
        sh ''' $SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=Youtube1 -Dsonar.projectKey=Youtube1'''
    }
}

```

<figure><img src="/files/YmyG5TJfcXTRoNXEvHJK" alt=""><figcaption></figcaption></figure>

Create another file for **qualityGate.groovy**

```

def call(credentialsId) {
    waitForQualityGate abortPipeline: false, credentialsId: credentialsId   
}
```

<figure><img src="/files/RYTCgYY6CTJ2Pt9NdClh" alt=""><figcaption></figcaption></figure>

Create another file for **npmInstall.groovy**

```

def call() {
    sh 'npm install'
}

```

<figure><img src="/files/zFG29jLp0nQ7Hq5e1pBO" alt=""><figcaption></figcaption></figure>

Push them to the GitHub Jenkins shared library

```
git add .
git commit -m "message"
git push origin main

```

Add these stages to the pipeline now

```
// under parameters

tools{
        jdk 'jdk17'
        nodejs 'node16'
    }
    environment {
        SCANNER_HOME=tool 'sonar-scanner'
    }

// add in stages

stage('sonarqube Analysis'){
        when { expression { params.action == 'create'}}    
            steps{
                sonarqubeAnalysis()
            }
        }
        stage('sonarqube QualitGate'){
        when { expression { params.action == 'create'}}    
            steps{
                script{
                    def credentialsId = 'Sonar-token'
                    qualityGate(credentialsId)
                }
            }
        }
        stage('Npm'){
        when { expression { params.action == 'create'}}    
            steps{
                npmInstall()
            }
        }

```

#### Build now.

<figure><img src="/files/RYJMtBCTXbb1cDYyhRw4" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/g53YdDvKkF5kH2folAfB" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/B1msKfb4DTy8WkAOGYev" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/arbjbhk4YsdzMFra70a6" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/XHBsvAM2zDW0MrsgF9Ws" alt=""><figcaption></figcaption></figure>

To see the report, you can go to Sonarqube Server and go to Projects.

<figure><img src="/files/vpPaIyvvSRR97hjIfbNs" alt=""><figcaption></figcaption></figure>

## **Step7: Install OWASP Dependency Check Plugins** <a href="#heading-step7-install-owasp-dependency-check-plugins" id="heading-step7-install-owasp-dependency-check-plugins"></a>

GotoDashboard → Manage Jenkins → Plugins → OWASP Dependency-Check. Click on it and install it without restart.

<figure><img src="/files/XUTn6u9eg2J2FwXNXXjn" alt=""><figcaption></figcaption></figure>

First, we configured the Plugin and next, we had to configure the Tool

Goto Dashboard → Manage Jenkins → Tools →

<figure><img src="/files/SttBkrP77wAoMNaRwEWE" alt=""><figcaption></figcaption></figure>

Click on Apply and Save here.

Create a file for **trivyFs.groovy**

```
def call() {
    sh 'trivy fs . > trivyfs.txt'
}

```

<figure><img src="/files/txwyfKjvQkSPIyUUUybk" alt=""><figcaption></figcaption></figure>

Push to GitHub

```
git add .
git commit -m "message"
git push origin main

```

Add the below stages to the Jenkins pipeline

```
stage('Trivy file scan'){
        when { expression { params.action == 'create'}}    
            steps{
                trivyFs()
            }
        }
        stage('OWASP FS SCAN') {
        when { expression { params.action == 'create'}}
            steps {
                dependencyCheck additionalArguments: '--scan ./ --disableYarnAudit --disableNodeAudit', odcInstallation: 'DP-Check'
                dependencyCheckPublisher pattern: '**/dependency-check-report.xml'
            }
        }

```

<figure><img src="/files/pIiCMXLahoHLsLvkvmL5" alt=""><figcaption></figcaption></figure>

Stage with the Dependency Check steps cannot be directly used inside a shared library.

The main reason is that pipelines loaded from shared libraries have more restrictive script security by default. So the `dependencyCheck` and `dependencyCheckPublisher` steps would fail with rejected signature errors.

Build now

<figure><img src="/files/i0Zu8XTtS3Vg6PHIsNeS" alt=""><figcaption></figcaption></figure>

You will see that in status, a graph will also be generated and Vulnerabilities.

<figure><img src="/files/Hi4KgjKK6GmBYoSCVC1T" alt=""><figcaption></figcaption></figure>

## **Step8A: Docker Image Build and Push** <a href="#heading-step8a-docker-image-build-and-push" id="heading-step8a-docker-image-build-and-push"></a>

We need to install the Docker tool in our system, Goto Dashboard → Manage Plugins → Available plugins → Search for Docker and install these plugins

`Docker`

`Docker Commons`

`Docker Pipeline`

`Docker API`

`docker-build-step`

and click on install without restart

<figure><img src="/files/9UrBtWn3Fi1THEq8S1yF" alt=""><figcaption></figcaption></figure>

Now, goto Dashboard → Manage Jenkins → Tools →

<figure><img src="/files/FLZMqW2R3N6eUDbAgSux" alt=""><figcaption></figcaption></figure>

Add DockerHub Username and Password under Global Credentials

<figure><img src="/files/kCarOTDxmCjCF8ISzrnT" alt=""><figcaption></figcaption></figure>

## **Step8B: Create an API key from Rapid API** <a href="#heading-step8b-create-an-api-key-from-rapid-api" id="heading-step8b-create-an-api-key-from-rapid-api"></a>

Open a new tab in the browser and search for [rapidapi.com](http://rapidapi.com/)

It will automatically provide your mail and select a mail to create an account

<figure><img src="/files/wIYMsdFfGOe34y3wKpWC" alt=""><figcaption></figcaption></figure>

Account is created

Now in the search bar search for YouTube and select YouTube v3

<figure><img src="/files/vBQRuPQ9GmYxTZw8SMJJ" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/GAeoRKvAE9IauUEyQMN3" alt=""><figcaption></figcaption></figure>

Copy API and use it in the groovy file

docker build --build-arg REACT\_APP\_RAPID\_API\_KEY=\<API-KEY> -t ${imageName} .

```
3fe354f5eamsh37c7e31ef2f57c2p129f7fjsnbfdb052f67f5
```

Create a shared library file for **dockerBuild.groovy**

```
def call(String dockerHubUsername, String imageName) {
    // Build the Docker image
    sh "docker build --build-arg REACT_APP_RAPID_API_KEY=3fe354f5eamsh37c7e31ef2f57c2p129f7fjsnbfdb052f67f5 -t ${imageName} ."
     // Tag the Docker image
    sh "docker tag ${imageName} ${dockerHubUsername}/${imageName}:latest"
    // Push the Docker image
    withDockerRegistry([url: 'https://index.docker.io/v1/', credentialsId: 'docker']) {
        sh "docker push ${dockerHubUsername}/${imageName}:latest"
    }
}

```

<figure><img src="/files/pCTx4T8GXSB5BZTO0ptD" alt=""><figcaption></figcaption></figure>

Create another file for **trivyImage.groovy**

```
def call() {
    sh 'trivy image sevenajay/youtube:latest > trivyimage.txt'
}

```

<figure><img src="/files/Zt8yGAQVxdhLwX6wP1tr" alt=""><figcaption></figcaption></figure>

Push the above files to the GitHub shared library.

```
git add .
git commit -m "message"
git push origin main

```

Add this stage to your pipeline with parameters

```
#add inside parameter

 string(name: 'DOCKER_HUB_USERNAME', defaultValue: 'bhushann11', description: 'Docker Hub Username')
 string(name: 'IMAGE_NAME', defaultValue: 'youtube', description: 'Docker Image Name')
#stage
stage('Docker Build'){
        when { expression { params.action == 'create'}}    
            steps{
                script{
                   def dockerHubUsername = params.DOCKER_HUB_USERNAME
                   def imageName = params.IMAGE_NAME

                   dockerBuild(dockerHubUsername, imageName)
                }
            }
        }
        stage('Trivy iamge'){
        when { expression { params.action == 'create'}}    
            steps{
                trivyImage()
            }
        }

```

Build now with parameters

<figure><img src="/files/5oDzzFL13ED5rpf03T42" alt=""><figcaption></figcaption></figure>

### **Step8C: Run the Docker container** <a href="#heading-step8c-run-the-docker-container" id="heading-step8c-run-the-docker-container"></a>

Create a new file **runContainer.groovy**

```
def call(){
    sh "docker run -d --name youtube1 -p 3000:3000 sevenajay/youtube:latest"
}

```

<figure><img src="/files/dY6uh2YiY88SOk0eQ24e" alt=""><figcaption></figcaption></figure>

Create Another file to remove container **removeContainer.groovy**

```
def call(){
    sh 'docker stop youtube1'
    sh 'docker rm youtube1'
}

```

<figure><img src="/files/QYAz1XIqDMwLNO0IsV2j" alt=""><figcaption></figcaption></figure>

Push them to the Shared library GitHub repo

```
git add .
git commit -m "message"
git push origin main

```

Add the below stages to the Pipeline

```
stage('Run container'){
        when { expression { params.action == 'create'}}    
            steps{
                runContainer()
            }
        }
        stage('Remove container'){
        when { expression { params.action == 'delete'}}    
            steps{
                removeContainer()
            }
        }

```

<figure><img src="/files/0v4ooEtAX73HpVuYDnOF" alt=""><figcaption></figcaption></figure>

Build with parameters 'create'

<figure><img src="/files/R2DoKT43Wn0Yg7sIQzjP" alt=""><figcaption></figcaption></figure>

It will start the container

```
<public-ip-jenkins:3000>

```

Output-

<figure><img src="/files/YKa1VTLW0RAHc47qJWGe" alt=""><figcaption></figcaption></figure>

Build with parameters 'delete'<br>

<figure><img src="/files/vc8sW448ywuPpFsZP1xl" alt=""><figcaption></figcaption></figure>

It will stop and remove the Container

<figure><img src="/files/UhIxpLH9igMVqC9UUGUm" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/JIs5vaYJyBnAQWLW15JI" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/GMWfrNbuQGA3XIAGQ0pF" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/bqWHhmFWgsp0LNRMZ6Bn" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/H9mLJYzg6l3jNhCcXzAG" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/d1WBi1yhrwMb7jih1D5c" alt=""><figcaption></figcaption></figure>

## **Step9A: Kubernetes Setup** <a href="#heading-step9a-kubernetes-setup" id="heading-step9a-kubernetes-setup"></a>

Connect your machines to Putty or Mobaxtreme

**Take-Two Ubuntu 20.04(t2.medium) instances one for k8s master and the other one for worker.**

Install Kubectl on Jenkins machine also.

### **Step9B: Kubectl is to be installed on Jenkins** <a href="#heading-step9b-kubectl-is-to-be-installed-on-jenkins" id="heading-step9b-kubectl-is-to-be-installed-on-jenkins"></a>

Connect your Jenkins machine

Create a shell script file [kube.sh](http://kube.sh/)

```
sudo vi kube.sh

```

Paste the below commands

```
sudo apt update
sudo apt install curl -y
curl -LO https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
kubectl version --client

```

### Step9C: K8S Master-Slave setup <a href="#heading-step9c-k8s-master-slave-setup" id="heading-step9c-k8s-master-slave-setup"></a>

**Part 1 ----------Master Node------------**

```
sudo hostnamectl set-hostname K8s-Master
exec bash
```

**----------Worker Node------------**

```
sudo hostnamectl set-hostname K8s-Worker
exec bash
```

**Part 2 ------------Both Master & Node ------------**

```
sudo apt-get update 

sudo apt-get install -y docker.io
sudo usermod –aG docker Ubuntu
newgrp docker
sudo chmod 777 /var/run/docker.sock

sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

sudo tee /etc/apt/sources.list.d/kubernetes.list <<EOF
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF

sudo apt-get update

sudo apt-get install -y kubelet kubeadm kubectl

sudo snap install kube-apiserver

```

**Part 3 --------------- Master ---------------**

```
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
# in case your in root exit from it and run below commands
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

```

**----------Worker Node------------**

```
sudo kubeadm join <master-node-ip>:<master-node-port> --token <token> --discovery-token-ca-cert-hash <hash>

```

<figure><img src="/files/lyht3tcgzkAKlTr7umr2" alt=""><figcaption></figcaption></figure>

Copy the config file to Jenkins master or the local file manager and save it

<figure><img src="/files/HppX2GGUcSVPtuRoU2oH" alt=""><figcaption></figcaption></figure>

copy it and save it in documents or another folder save it as secret-file.txt

Note: create a secret-file.txt in your file explorer save the config in it and use this at the kubernetes credential section.

Install Kubernetes Plugin, Once it's installed successfully.

<figure><img src="/files/D3aTyYgtQVFPn9IqQB63" alt=""><figcaption></figcaption></figure>

goto manage Jenkins --> manage credentials --> Click on Jenkins global --> add credentials

<figure><img src="/files/0dpSAhDxN2M2oUipIJHA" alt=""><figcaption></figcaption></figure>

#### **Step9D: Install Helm & Monitoring K8S using Prometheus and Grafana** <a href="#heading-step9d-install-helm-andamp-monitoring-k8s-using-prometheus-and-grafana" id="heading-step9d-install-helm-andamp-monitoring-k8s-using-prometheus-and-grafana"></a>

On Kubernetes Master install the helm

```
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
chmod 700 get_helm.sh
./get_helm.sh

```

<figure><img src="/files/ZAYwifqdLj6EcMFQdWja" alt=""><figcaption></figcaption></figure>

See the Helm version

```
helm version --client

```

<figure><img src="/files/cHkj0jCwdSbc06HcLarA" alt=""><figcaption></figcaption></figure>

Add Prometheus Helm repo

```
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts

```

Create Prometheus namespace

```
kubectl create namespace prometheus

```

**Install kube-Prometheus-stack**

**Below is the command to install kube-Prometheus-stack. The helm repo kube-stack-Prometheus (formerly Prometheus-operator) comes with a Grafana deployment embedded.**

```
helm install stable prometheus-community/kube-prometheus-stack -n prometheus

```

Let's check if the Prometheus and Grafana pods are running or not

```
kubectl get pods -n prometheus

```

Now See the services

```
kubectl get svc -n prometheus

```

<figure><img src="/files/oUdCf5gVDz3SfPexJjRE" alt=""><figcaption></figcaption></figure>

This confirms that Prometheus and grafana have been installed successfully using Helm.

To make Prometheus and grafana available outside the cluster, use LoadBalancer or NodePort instead of ClusterIP.

**Edit Prometheus Service**

kubectl edit svc stable-kube-prometheus-sta-prometheus -n prometheus

**Edit Grafana Service**

```
kubectl edit svc stable-grafana -n prometheus
```

<figure><img src="/files/wAo9rOLqPjQMsWSQJNSS" alt=""><figcaption></figcaption></figure>

Verify if the service is changed to LoadBalancer and also get the Load BalancerPorts.

```
kubectl get svc -n prometheus
```

<figure><img src="/files/OyIiInAePCdUisFtU4Fq" alt=""><figcaption></figcaption></figure>

**Access Grafana UI in the browser**

Get the external IP from the above screenshot and put it in the browser

<figure><img src="/files/n2PgA98iELBKBpnafYbH" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/iZVAQRq23nlsTqCyb3dD" alt=""><figcaption></figcaption></figure>

**Create a Dashboard in Grafana**

In Grafana, we can create various kinds of dashboards as per our needs.

**How to Create Kubernetes Monitoring Dashboard?**

For creating a dashboard to monitor the cluster:

Click the '+' button on the left panel and select ‘Import’.

Enter the 15661 dashboard id under [Grafana.com](http://grafana.com/) Dashboard.

Click ‘Load’.

<figure><img src="/files/QJiewq8Quup5UvELVphu" alt=""><figcaption></figcaption></figure>

Select ‘Prometheus’ as the endpoint under the Prometheus data sources drop-down.

<figure><img src="/files/G1Jg2fOHMdTwctNi1tEK" alt=""><figcaption></figcaption></figure>

Click ‘Import’.

This will show the monitoring dashboard for all cluster nodes

<figure><img src="/files/7EDcqVqVis2SZ5gKoVs4" alt=""><figcaption></figcaption></figure>

**How to Create Kubernetes Cluster Monitoring Dashboard?**

For creating a dashboard to monitor the cluster:

Click the '+' button on the left panel and select ‘Import’.

Enter 3119 dashboard ID under [Grafana.com](http://grafana.com/) Dashboard.

Click ‘Load’.

Select ‘Prometheus’ as the endpoint under the Prometheus data sources drop-down.

Click ‘Import’.

This will show the monitoring dashboard for all cluster nodes

<figure><img src="/files/Ks2Q4DoEuwOHUX82vaEy" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/p2BCPppXXbeQrf6UhNvZ" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/kph9NtlGisT4idtNDuZK" alt=""><figcaption></figcaption></figure>

**Create a POD Monitoring Dashboard**

For creating a dashboard to monitor the cluster:

Click the '+' button on the left panel and select ‘Import’.

Enter 6417 dashboard ID under [Grafana.com](http://grafana.com/) Dashboard.

Click ‘Load’.

Select ‘Prometheus’ as the endpoint under the Prometheus data sources drop-down.

Click ‘Import’.

<figure><img src="/files/tm2fT1EC20iKz1CtnrCX" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/DBvOqvchcMTpv0v1xjJg" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/lp6Jng3SKk1yAi0Ng459" alt=""><figcaption></figcaption></figure>

### **Step9E: K8S Deployment** <a href="#heading-step9e-k8s-deployment" id="heading-step9e-k8s-deployment"></a>

Let's Create a Shared Jenkins library file for K8s deploy and delete

Name **kubeDeploy.groovy**

```
def call() {
    withKubeConfig(caCertificate: '', clusterName: '', contextName: '', credentialsId: 'k8s', namespace: '', restrictKubeConfigAccess: false, serverUrl: '') {
        sh "kubectl apply -f deployment.yml"
    }
}
```

<figure><img src="/files/MqMS2AHjevhTjBuhVDHo" alt=""><figcaption></figcaption></figure>

To delete deployment

Name **kubeDelete.groovy**

```
def call() {
    withKubeConfig(caCertificate: '', clusterName: '', contextName: '', credentialsId: 'k8s', namespace: '', restrictKubeConfigAccess: false, serverUrl: '') {
        sh "kubectl delete -f deployment.yml"
    }
}
```

<figure><img src="/files/gQDgXPKbg4ed3zAYWefL" alt=""><figcaption></figcaption></figure>

Let's push them to GitHub

```
git add .
git commit -m "message"
git push origin main

```

### The final stage of the Pipeline

```
stage('Kube deploy'){
        when { expression { params.action == 'create'}}    
            steps{
                kubeDeploy()
            }
        }
        stage('kube deleter'){
        when { expression { params.action == 'delete'}}    
            steps{
                kubeDelete()
            }
        }

```

<figure><img src="/files/qd7m1UO2yxplPROwZXGt" alt=""><figcaption></figcaption></figure>

Build Now with parameters 'create'

It will apply the deployment

stage view

<figure><img src="/files/vWNyxKCsVQ2GJUdbEZSB" alt=""><figcaption></figcaption></figure>

```
kubectl get all (or)
kubectl get svc

```

```
<kubernetes-worker-ip:svc port>
```

### Output -

<figure><img src="/files/6v0i5TtdLJsLKFRjP6Wo" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/OfCphO82CseSDiABJRYn" alt=""><figcaption></figcaption></figure>

Build with parameter 'delete'

It will destroy Container and Kubernetes deployment.

<figure><img src="/files/uu6mUfslHSSNHzhQefyR" alt=""><figcaption></figcaption></figure>

Dependacy Check Result -

<figure><img src="/files/5k4KceAnIULOwBZ9fmbe" alt=""><figcaption></figcaption></figure>

Slack Notifications

<figure><img src="/files/Ou9PBLDdLu06U3sTUJzd" alt=""><figcaption></figcaption></figure>

**Splunk**

<figure><img src="/files/4Vk04NUleQbuZf5tTBnZ" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/8WXKYwxlhOPEh9MmLx8H" alt=""><figcaption></figcaption></figure>

**Grafhana -**

<figure><img src="/files/qd9ZyV9zPsTwwVM0oEkq" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/j4jDwfVXGlfvDL8m1kgS" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/PyDwo1deQjo0fXhSITsi" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/SmuKMcwuGvrmpjOVnX0H" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/nJQAbHNWna74j1tiSchM" alt=""><figcaption></figcaption></figure>

### **Pipeline**

```
@Library('Jenkins-shared-library')_

def COLOR_MAP = [
    'FAILURE' : 'danger',
    'SUCCESS' : 'good'
]


pipeline{
    agent any
    parameters {
        choice(name: 'action', choices: 'create\ndelete', description: 'Select create or destroy.')
        string(name: 'DOCKER_HUB_USERNAME', defaultValue: 'bhushann11', description: 'Docker Hub Username')
        string(name: 'IMAGE_NAME', defaultValue: 'youtube', description: 'Docker Image Name')    
         
    }
    
    // under parameters

    tools{
            jdk 'jdk17'
            nodejs 'node16'
        }
        environment {
            SCANNER_HOME=tool 'sonar-scanner'
        }
    
    stages{
        stage('clean workspace'){
            steps{
                cleanWorkspace()
            }
        }
        stage('checkout from Git'){
            steps{
                checkoutGit('https://github.com/Bhushan0151/Youtube-clone-app.git', 'main')
            }
        }
        
        stage('sonarqube Analysis'){
        when { expression { params.action == 'create'}}    
            steps{
                sonarqubeAnalysis()
            }
        }
        stage('sonarqube QualitGate'){
        when { expression { params.action == 'create'}}    
            steps{
                script{
                    def credentialsId = 'Sonar-token'
                    qualityGate(credentialsId)
                }
            }
        }
        stage('Npm'){
        when { expression { params.action == 'create'}}    
            steps{
                npmInstall()
            }
        }
        
        stage('Trivy file scan'){
        when { expression { params.action == 'create'}}    
            steps{
                trivyFs()
            }
        }
        
        stage('OWASP FS SCAN') {
        when { expression { params.action == 'create'}}
            steps {
                dependencyCheck additionalArguments: '--scan ./ --disableYarnAudit --disableNodeAudit', odcInstallation: 'DP-Check'
                dependencyCheckPublisher pattern: '**/dependency-check-report.xml'
            }
        }
        
        stage('Docker Build'){
        when { expression { params.action == 'create'}}    
            steps{
                script{
                   def dockerHubUsername = params.DOCKER_HUB_USERNAME
                   def imageName = params.IMAGE_NAME

                   dockerBuild(dockerHubUsername, imageName)
                }
            }
        }
        stage('Trivy iamge'){
        when { expression { params.action == 'create'}}    
            steps{
                trivyImage()
            }
        }
        
        stage('Run container'){
        when { expression { params.action == 'create'}}    
            steps{
                runContainer()
            }
        }
        stage('Remove container'){
        when { expression { params.action == 'delete'}}    
            steps{
                removeContainer()
            }
        }
        
        stage('Kube deploy'){
        when { expression { params.action == 'create'}}    
            steps{
                kubeDeploy()
            }
        }
        stage('kube deleter'){
        when { expression { params.action == 'delete'}}    
            steps{
                kubeDelete()
            }
        }
    }
     post {
         always {
             echo 'Slack Notifications'
             slackSend (
                 channel: '#jenkins',   // change your channel name
                 color: COLOR_MAP[currentBuild.currentResult],
                 message: "*${currentBuild.currentResult}:* Job ${env.JOB_NAME} \n build ${env.BUILD_NUMBER} \n More info at: ${env.BUILD_URL}"
               )
           }
       }
   }
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://bhushans-devops-organization.gitbook.io/advanced-projects/deploying-a-youtube-clone-app-with-devsecops-and-jenkins-shared-library.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
