DevSecOps Pipeline Project:YouTube App Deployment with GitLab CI/CD

Step 1: Create an API key from Rapid API


Step 2: Create a Repository and push it to GitLab


Step 3: Launch an Ec2 instance and run Sonarqube on it
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.medium."
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 10 GB of storage.
Launch Instance: Review and launch the instance.
Access Your Instance: Use SSH to connect to your instance with the private key.
Keep in mind, that opening all ports is not recommended for production environments; it's just for educational purposes.


Connect to Your EC2 Instance and install docker:
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
Now copy the IP address of the ec2 instance
<ec2-public-ip:9000>

Enter username and password, click on login and change password
username admin
password admin

Step 4A: Create a .gitlab-ci.yml File.
Now go to GitLab click on '+' and click on Newfile

Commit the changes and it will automatically start the build
Now click on Build and Pipelines

Click on Install dependency

You will build output

Now add the Sonarqube stage to the pipeline
Go to the Sonarqube dashboard and click on Manually.

Select the CI tool as GitLab CI

Select Other because we are using the JS App
It will provide code and we need to create a file inside our repo

Go to Gitlab and click on + and Newfile

Step 4B: Add the required variables for the project.
Variables Generated

Now go to GitLab
Click on settings and CI/CD

Click on Expand in variables

Sonar token is added

Now go back to the Sonarqube Dashboard
Click on continue

It will provide and CI configuration file copy it and use it inside our .gitlab-ci.yml file

stages:
- npm
- sonar
Install dependecy:
stage: npm
image:
name: node:16
script:
- npm install
sonarqube-check:
stage: sonar
image:
name: sonarsource/sonar-scanner-cli:latest
entrypoint: [""]
variables:
SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar" # Defines the location of the analysis task cache
GIT_DEPTH: "0" # Tells git to fetch all the branches of the project, required by the analysis task
cache:
key: "${CI_JOB_NAME}"
paths:
- .sonar/cache
script:
- sonar-scanner
allow_failure: true
only:
- main



Now add the next stage of the Trivy file scan
Update the .gitlab-ci.yml file
stages:
- npm
- sonar
Install dependecy:
stage: npm
image:
name: node:16
script:
- npm install
sonarqube-check:
stage: sonar
image:
name: sonarsource/sonar-scanner-cli:latest
entrypoint: [""]
variables:
SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar" # Defines the location of the analysis task cache
GIT_DEPTH: "0" # Tells git to fetch all the branches of the project, required by the analysis task
cache:
key: "${CI_JOB_NAME}"
paths:
- .sonar/cache
script:
- sonar-scanner
allow_failure: true
only:
- main
Trivy file scan:
stage: trivy file scan
image:
name: aquasec/trivy:latest
entrypoint: [""]
script:
- trivy fs .



Add the Docker build and push stage
Before that Add docker credentials to GitLab Variables as secrets.
Go to the docker hub and create a Personal Access token
Click on your profile name and Account Settings

Now add the below stage to the Configuration .gitlab-ci.yml file
Added Docker and Trivy image scan stages
stages:
- npm
- sonar
Install dependecy:
stage: npm
image:
name: node:16
script:
- npm install
sonarqube-check:
stage: sonar
image:
name: sonarsource/sonar-scanner-cli:latest
entrypoint: [""]
variables:
SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar" # Defines the location of the analysis task cache
GIT_DEPTH: "0" # Tells git to fetch all the branches of the project, required by the analysis task
cache:
key: "${CI_JOB_NAME}"
paths:
- .sonar/cache
script:
- sonar-scanner
allow_failure: true
only:
- main
Trivy file scan:
stage: trivy file scan
image:
name: aquasec/trivy:latest
entrypoint: [""]
script:
- trivy fs .
Docker build and push:
stage: docker
image:
name: docker:latest
services:
- docker:dind
script:
- docker build --build-arg REACT_APP_RAPID_API_KEY= -t youtubev1 .
- docker tag youtubev1 sevenajay/youtubev1:latest
- docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
- docker push sevenajay/youtubev1:latest
Scan image:
stage: trivy image scan
image:
name: aquasec/trivy:latest
entrypoint: [""]
script:
- trivy image shreyashbhise16/youtubev1:latest



Step 5: Install Gitlab Runner on Ec2


sudo gitlab-runner start
sudo gitlab-runner run

sudo su
sudo vi /home/gitlab-runner/.bash_logout
Comment them

Save and exit from that file and restart GitLab runner
sudo gitlab-runner restart
exit #from root
Now start and Run the GitLab runner
sudo gitlab-runner start
sudo gitlab-runner run

Step 7: Access the Application on Browser
Copy the Public IP of the ec2 instance and paste it into the Browser.
Don't forget to open the 3000 port in the Security Group
<Public-ip:3000>

About the author
*Shreyash Bhise | Aspiring Mern Stack Developer and DevOps enthusiast,
Comments
Join the discussionNo comments yet. Be the first to comment.



