# GitLab CI/CD YouTube App Deployment

In today’s software development landscape, speed and reliability are paramount. Continuous Integration and Continuous Deployment (CI/CD) pipelines offer a way to automate and streamline your development and deployment processes. In this guide, we’ll show you how to set up a complete CI/CD pipeline for a React YouTube app clone using GitLab.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719231489521/73be2531-4039-4cb6-9b90-75ea9dfee3ff.png align="center")

Picture this: we are creating a YouTube-like platform where users can explore videos and channels. It’s a fantastic project, but managing the development workflow can be complex. That’s where GitLab CI/CD comes in, offering a straightforward and powerful solution to automate your work.

This step-by-step tutorial is for developers and devops enthusiasts looking to make their projects more efficient with CI/CD. By the end, you’ll know how to set up a robust CI/CD pipeline, including automated testing, code quality checks, and secure containerization. Let’s embark on this journey together and see how CI/CD can transform your development experience while building a React YouTube app clone.

Ready? Let’s simplify CI/CD and make your development life easier!

Step 1: Create an API key for Youtube.

Step 2: Create a Repository and push it to GitLab.

Step 3: Launch an Ec2 instance and run Sonarqube on it.

Step 4A: Create a `.gitlab-ci.yml` File.

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

Step 5: Install Gitlab Runner on Ec2.

Step 6: Run the Application on the Docker container.

Step 7: Access the Application on Browser.

### Step 1: Create an API key from Rapid API

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

You will see the page like this and click on signup

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719231878729/35b4c962-2bd2-4114-8be5-ced4817a5942.avif align="center")

Now click on Sign Up with Google

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719232032371/0be4adf0-c127-40aa-83cb-bbb8bd6f1fc0.avif align="center")

Select your mail here

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719232428620/d77a4eb5-210c-4ce9-b629-a5f9c9b71ddc.png align="center")

It will automatically create your account now

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719232837737/de3b346a-4509-45fe-b427-bad9ef70157c.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719232919513/e808fd7a-c24a-4a4c-b2aa-b5253b5c3075.avif align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719233656659/e49a6f70-b693-41f4-bd90-4a80da54a678.png align="center")

Copy API Key as shown in screenshot and save it for further use at the docker stage.

docker build –build-arg REACT\_APP\_RAPID\_API\_KEY=<mark>&lt;API-KEY&gt;</mark> -t ${imageName} .

### Step 2: Create a Repository and push it to GitLab

Go to [GitLab.com](http://gitlab.com/) and login to your account

Click on New Project

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719234100945/74e47604-2b44-4d63-8e79-62772ca0a13b.avif align="center")

Click on Create Blank Project

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719234152767/d36594e2-71cf-4f2d-9def-7d2aedeb35e1.avif align="center")

Provide a name for the Project

Keep Visibility to the public

Uncheck the Readme and create the Project

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719234223172/30cfaf8a-10b9-44dc-84d6-e03e58b65580.avif align="center")

Use the below commands to push code to GitLab

```plaintext
cd existing_folder
git init --initial-branch=main
git remote add origin https://gitlab.com/sgawali/youtubev1.git
git add .
git commit -m "Initial commit"
git push --set-upstream origin main
```

Files pushed to GitLab

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719236845153/561c7fa4-850c-4383-8d0a-13e1b4562244.png align="center")

### 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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719238375690/360def43-ade4-4dac-bf7a-aaf7fba37206.avif align="center")

**Connect to Your EC2 Instance and install docker:**

Run the below commands to install the docker

```bash
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:

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719238825684/f982604f-59c8-4f2b-bff0-245c1f717e1a.avif align="center")

Now copy the IP address of the ec2 instance

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719238995076/b67b7180-ef3c-481a-9150-ed1092482845.avif align="center")

Enter username and password, click on login and change password

```plaintext
username admin
password admin
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719239085490/352d5259-8135-4509-a908-cfb8df52027d.avif align="center")

Update New password, This is Sonar Dashboard.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694159887297/6e055b5c-13ea-405f-bc13-1234b05bf2ff.png?auto=compress,format&format=webp&auto=compress,format&format=webp&auto=compress,format&format=webp&auto=compress,format&format=webp&auto=compress,format&format=webp align="left")

### Step 4A: Create a `.gitlab-ci.yml` File.

Now go to GitLab click on ‘+’ and click on Newfile

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719239341779/4243b79c-7fb3-4af2-a424-7100964bf195.png align="center")

File name `.gitlab-ci.yml`

Content

```yaml
stages:
    - npm

Install dependecy:
    stage: npm    
    image:
        name: node:16
    script:
        - npm install
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699452609575/a9a0d794-a5c7-4501-8d9d-93f093441e8d.png?auto=compress,format&format=webp align="left")

Commit the changes and it will automatically start the build

Now click on Build and Pipelines

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719240918577/de678a26-ed71-4f31-b4cc-3873164e9f75.png align="center")

Now click on Running.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719242090972/bc88238d-bc9c-479e-970f-cf0968622149.png align="center")

Click on Install dependency

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719243135897/747451d1-723e-472e-9336-16cde2c8c556.png align="center")

You will see build output

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719243637198/ed29467c-8e03-43a4-8e3d-22d6f0442e93.png align="center")

Now add the Sonarqube stage to the pipeline

Go to the Sonarqube dashboard and click on Manually.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699453113737/703cb437-6072-4c62-b4f5-b3182244e91c.png?auto=compress,format&format=webp align="left")

Provide the name of the Project and click on Setup

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699453190201/9e10f50d-a125-41a8-b400-4b24b4df6944.png?auto=compress,format&format=webp align="left")

Select the CI tool as GitLab CI

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699453238357/cf6ae97a-a6fd-4d28-8a7e-0505cf3dc032.png?auto=compress,format&format=webp align="left")

Select Other because we are using the JS App

It will provide code and we need to create a file inside our repo

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699453308067/e9219457-2bba-4ca3-84c9-3933ec1092cf.png?auto=compress,format&format=webp align="left")

Go to Gitlab and click on + and Newfile

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719244406839/b1a3f886-15a5-45bf-87a2-56b6a14a2816.png align="center")

Filename is [`sonar-project.properties`](http://sonar-project.properties)

Paste the content that you got from Sonarqube

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699453515944/6a8cd5e1-80b4-4b72-9958-8c704182f741.png?auto=compress,format&format=webp align="left")

The file looks like this and click on commit changes

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699453557845/3badc428-61bc-40b8-b30b-d69b3b52f4c7.png?auto=compress,format&format=webp align="left")

Go to Sonarqube and click on continue

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699453614668/8ef310fb-7306-4863-9f0b-2eebf6d80ad0.png?auto=compress,format&format=webp align="left")

Now it will provide Variables to add to our GitLab

### Step 4B: Add the required variables for the project.

Variables Generated

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699454672224/22cea9c4-f102-44af-babb-4a4e1a5c77e7.png?auto=compress,format&format=webp align="left")

Now go to GitLab

Click on settings and CI/CD

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719244936845/21fd7a2f-36e7-44fb-b484-295a541e5692.png align="center")

Click on Expand in variables

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719245189011/875099fb-4224-452e-94a7-d1b41d47e819.png align="center")

Click on Add variable

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719245412756/82270e4e-7d68-4e3a-a8fd-219565198dd2.png align="center")

Now go back to Sonarqube and copy the Key

Click on Generate a token

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699455069519/8d9b9784-ee94-45f4-b31d-2fc489cb8fe1.png?auto=compress,format&format=webp align="left")

Again Click on Generate

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699455192574/d7b64024-d697-4994-bfcf-a1e992242864.png?auto=compress,format&format=webp align="left")

Copy the token

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699455122645/0fee8721-f955-4823-ad72-96fb76c81d06.png?auto=compress,format&format=webp align="left")

Now come back to GitLab and add them like the below image and click on add variable.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699455326754/a3e4c640-3d74-45c5-a384-c693b12762ae.png?auto=compress,format&format=webp align="left")

Sonar token is added

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699458436039/80e686d4-fb61-4290-ac95-c9cf10fe0bab.png?auto=compress,format&format=webp align="left")

Now go to the Sonarqube Dashboard again

Let’s add another variable, copy them

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699458516421/555516f9-2dfa-432c-884f-a74940e1bbab.png?auto=compress,format&format=webp align="left")

Now go to GitLab and click on Add variable

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699458605982/ffec759c-7dae-40f5-a298-dd9d6ce8bbea.png?auto=compress,format&format=webp align="left")

Add the copied values like the below image

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699458655369/84b8e4c3-4521-4ff2-aa33-ec1dff726c0f.png?auto=compress,format&format=webp align="left")

Two variables were added.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699458724321/4d614a87-4c37-482b-98f0-de7f46311159.png?auto=compress,format&format=webp align="left")

Now go back to the Sonarqube Dashboard

Click on continue

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699458788856/9016e8c4-c62f-46fd-a3ba-c632ae12a52d.png?auto=compress,format&format=webp align="left")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699458903130/777eab4b-46a2-4cd9-897a-89d6ec124d08.png?auto=compress,format&format=webp align="left")

Now go back to GitLab and edit the `.gitlab-ci.yml` file

Full file (update with your content)

```yaml
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
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699459068250/84f95e79-99a8-4555-9f45-4a7c2984f82e.png?auto=compress,format&format=webp align="left")

Commit changes and it will automatically start the build.

Click on Build –&gt; Pipelines

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719249426976/5f0e9d07-9157-4e55-8e0d-08cfe8c1730b.png align="center")

Click on Running

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719249791618/ff0a008a-ccbb-4140-9863-6933174a5949.png align="center")

Now click on Sonarqube-check

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719250127604/8854505d-fa9f-4286-86ea-62ec9c0deddb.png align="center")

Build output

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699459421037/66465bd9-851a-4624-bd5d-d27a60d64c42.png?auto=compress,format&format=webp align="left")

Now add the next stage of the Trivy file scan

Update the `.gitlab-ci.yml` file

```yaml
stages:
    - npm
    - sonar
    - trivy file scan
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 .
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699459867536/6b14f916-3182-44ba-a2d6-efa6c2fbcbac.png?auto=compress,format&format=webp align="left")

Commit changes and go to pipeline stages

Click on the Trivy file scan

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719250452779/8b09ffd1-7401-4a9a-a2dd-09289c61b800.png align="center")

Build output

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699460035893/569b64ca-54f5-4d18-8c57-0e0e41a6a168.png?auto=compress,format&format=webp align="left")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719250950781/80889dc4-8036-40c1-a934-5e7fb47a9212.png align="center")

Now click on Security –&gt; New Access Token

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719251089992/f029ec93-7268-43a9-92c6-b22dcdc5a0bd.png align="center")

Provide a name –&gt; Generate

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719251394759/4931e29e-9d4e-463e-857f-47a7ee6ef7eb.png align="center")

Now copy the token and keep it safe

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719251742649/1e5fd223-3e61-40dd-93b4-5e3daeabd075.png align="center")

Now go back to Gitlab

Click on settings and CI/CD

Click on Expand in variables

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719252066740/e3795b3a-e765-45ec-a0d1-41b01b13e3b6.png align="center")

Click on Add variable

Use your DockerHub username in value and Add variable

Key DOCKER\_USERNAME

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719254220644/5dfa0c8c-de36-4d51-bd01-6bb94be9b53c.png align="center")

Click on Add variable again

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699461254121/79877f0e-4100-4ad6-a527-901f1ac169dd.png?auto=compress,format&format=webp align="left")

Key DOCKER\_PASSWORD

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719254181515/79307659-0c0d-417d-8985-b2fbed60fe40.png align="center")

Variables added

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699461412146/24f15467-af4d-4b2a-a8ec-00773b031e48.png?auto=compress,format&format=webp align="left")

Now add the below stage to the Configuration `.gitlab-ci.yml` file

Added Docker and Trivy image scan stages

```yaml
stages:
    - npm
    - sonar
    - trivy file scan
    - docker
    - trivy image scan
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=f0ead79813mshb0aa7ddf114a7dap1adb3djsn483b017de1a9 -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 sevenajay/youtubev1:latest
```

Added stages

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699461630726/95375272-9cff-40ac-9314-ed7170081d1a.png?auto=compress,format&format=webp align="left")

Commit changes and it will automatically start building.

Go to Pipelines view

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699461750535/b0365c17-c9a0-4210-bd8b-5cccaee8350d.png?auto=compress,format&format=webp align="left")

Now click on Docker build and push

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719255143750/59deb712-3b6d-4202-b365-3eccdb668f1e.png align="center")

Build view

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699462057276/407cc0f4-cd34-4536-9493-1608ec076939.png?auto=compress,format&format=webp align="left")

Go to Dockerhub and see that docker image is uploaded there

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719255855391/c5dbffae-5a82-479b-b57f-70a8c83b7487.png align="center")

Now come back to GitLab and click on Trivy image scan

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719256567053/fb9b1a0e-af36-48e5-b29a-d4b86c12cd28.png align="center")

Output Log

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699462215958/0dc44340-f401-4db8-b895-c29239cd7ed2.png?auto=compress,format&format=webp align="left")

### Step 5: Install Gitlab Runner on Ec2

Go to GitLab and Click on Settings and CI/CD

Click on Expand at Runners

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719256765960/8f750968-1244-4c3f-8da0-9917b9e86eda.png align="center")

Click on Three dots and then click on Show Runner installation

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699496481444/b65ca11e-c3ed-482b-b88f-ca09de982e91.png?auto=compress,format&format=webp align="left")

Click on Linux and amd64 and copy the commands

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699496578546/89e00113-ad74-49e5-8429-8afbe613432e.png?auto=compress,format&format=webp align="left")

Now come back to Putty or Mobaxtreme

Create a new file

```plaintext
sudo vi gitlab-runner-installation
```

Paste the below commands into it

```plaintext
# Download the binary for your system
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
# Give it permission to execute
sudo chmod +x /usr/local/bin/gitlab-runner
# Create a GitLab Runner user
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
# Install and run as a service
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699496847567/f7d6b7b4-8911-4716-a005-3fc9a40aea8e.png?auto=compress,format&format=webp align="left")

Provide executable permissions and run the script

```plaintext
sudo chmod +x <file-name>
./<file-name>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699496912602/8b0ce19c-9936-4af7-a31a-ef9bdff61ca6.png?auto=compress,format&format=webp align="left")

Installation completed

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699496951977/da63c9a2-7b9b-4704-8773-f48e6df332db.png?auto=compress,format&format=webp align="left")

Start the GitLab runner

```plaintext
sudo gitlab-runner start
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699497141540/d2f13a08-dbe8-4a69-b6cc-53d959a576b0.png?auto=compress,format&format=webp align="left")

Now run the below command or your command to register the runner

Update the token is enough

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699497176944/40784d71-02ec-4ea2-9a2a-a27bc2ef176e.png?auto=compress,format&format=webp align="left")

```plaintext
sudo gitlab-runner register --url https://gitlab.com/ --registration-token <token>
```

Provide the details for registering the runner

1. Provide Enter for [GitLab.com](http://gitlab.com/)
    
2. For token we already added with token, so click on Enter again
    
3. Description as your wish
    
4. Tags also and you can use multiple tags by providing a comma after each tga
    
5. Maintenance note is just optional
    
6. For executors use Shell
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699497412725/c1a2fa03-a00f-4680-a063-9a7628a70c25.png?auto=compress,format&format=webp align="left")

Runner added successfully.

Start the GitLab runner

```plaintext
sudo gitlab-runner start
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699497141540/d2f13a08-dbe8-4a69-b6cc-53d959a576b0.png align="left")

Run the GitLab runner

```plaintext
sudo gitlab-runner run
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699497647142/86e76d77-d0c1-4f8f-b33c-235fccd872bf.png?auto=compress,format&format=webp align="left")

Go to GitLab and refresh the page once or click on Enable for this project

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699497778257/f644495c-f055-4248-9284-44170acd350d.png?auto=compress,format&format=webp align="left")

Now the runner is active and waiting for jobs

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699497860970/90704675-cf26-476e-988f-aa19d1a4839e.png?auto=compress,format&format=webp align="left")

Click on the Pencil mark to edit

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699497961772/47ae60e1-4bd3-44cb-9ed2-1ea5a54e206f.png?auto=compress,format&format=webp align="left")

Click on the Check box to indicate whether this runner can pick jobs without tags.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699498014680/42aeb192-53e8-4034-af22-dcd8f824a6e1.png?auto=compress,format&format=webp align="left")

Click on save changes.

### Step 6: Run the Application on the Docker container

Now edit the `.gitlab-ci.yml` file for the deploy stage

The complete file

```yaml
stages:
    - npm
    - sonar
    - trivy file scan
    - docker
    - trivy image scan
    - run container
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=f0ead79813mshb0aa7ddf114a7dap1adb3djsn483b017de1a9 -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 sevenajay/youtubev1:latest
deploy:
  stage: run container
  tags:
    - youtube        #use your own tags 
  script:
    - docker run -d --name youtube -p 3000:3000 sevenajay/youtubev1:latest
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699498330575/911dcf06-c765-472d-9365-9deb15d40aa7.png?auto=compress,format&format=webp align="left")

Commit changes, it will automatically start to build

Click on Build –&gt; Pipelines

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699498482326/67580d52-ac14-4366-8311-486ca6992da3.png?auto=compress,format&format=webp align="left")

Click on Running

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719258453250/269dc249-b703-4367-bec1-3111fb7a1cd5.png align="center")

The last stage is added to the Pipeline

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699498599684/c6df4448-104e-4f6c-ad2c-47c89b0b2e3f.png?auto=compress,format&format=webp align="left")

If you get an error like this

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699502625809/10f5e76f-0f2a-4c1b-9476-c127137424cd.png?auto=compress,format&format=webp align="left")

Go to GitLab and click on deploy job

Let’s see what is the error

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719258794797/0e17d435-122d-461e-bf77-abfe7732f78e.png align="center")

If you get an error like this, click on that link

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719259095133/af9728af-6cc9-4052-b395-9a3f37aaf9a5.png align="center")

It will open a new tab and provide a solution for that

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699502840964/4e11fc1d-5470-4f3d-9409-b8aa87bb6bea.png?auto=compress,format&format=webp align="left")

Now go to Mobaxtreme and stop the Runner

Go to root and use the below commands

```plaintext
sudo su
sudo vi /home/gitlab-runner/.bash_logout
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699502967163/b6b738e4-55ef-47c2-9b11-b4161be5a892.png?auto=compress,format&format=webp align="left")

You will see file like this

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699503008404/5065157e-8f53-4b3e-8498-0be4ebeb725a.png?auto=compress,format&format=webp align="left")

Comment all lines

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699503040759/93a93949-7031-4514-9917-fa1af41d0841.png?auto=compress,format&format=webp align="left")

Save and exit from that file and restart GitLab runner

```plaintext
sudo gitlab-runner restart
exit #from root
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699506206570/47caec6b-d6e8-4be7-9e1f-6bed8c93b7b7.png?auto=compress,format&format=webp align="left")

Now start and Run the GitLab runner

```plaintext
sudo gitlab-runner start
sudo gitlab-runner run
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699506253690/007c6be8-483c-4d76-a15d-6119fcb539e7.png?auto=compress,format&format=webp align="left")

Now go to GitLab –&gt; Build –&gt; Pipelines

Click on Run Pipeline

Build completed and click on Deploy job

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719259617203/3f1cbb04-d5e0-4e4e-99ac-c7be27881c9f.png align="center")

See the output it ran a container on ec2 instance

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719259890899/24352770-e7b4-495a-8a3b-7f2d52d8311c.png align="center")

Now go to MobaXtreme or Putty connect to ec2 instance and Provide the below command to see running containers.

```plaintext
docker ps
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699506425447/c92295b1-6c54-4f31-b826-48dd34ccda51.png?auto=compress,format&format=webp align="left")

Container running.

### 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

```plaintext
<Public-ip:3000>
```

Output

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699506627834/248f2f9c-f58d-4c3f-8590-cc06133dfede.png?auto=compress,format&format=webp align="left")

### Step 8: Termination

1. Delete the personal Access token of the docker.
    
2. Delete the containers.
    
    ```plaintext
    docker stop <container name>
    docker rm <container name>
    ```
    
3. Delete the Ec2 instance.
    

And there you have it, folks! You’re now the DevOps master of your YouTube app deployment. We’ve journeyed through code, quality, security, containers, and automation, all with GitLab. Now it’s your turn to unleash the power of DevOps and take your YouTube app to the next level!

So, what’s next on your development adventure? Whether it’s conquering new projects, exploring even cooler tech, or simply celebrating with a well-deserved coffee break, remember that DevOps is all about making your life easier and your code better. Embrace the joy of seamless deployments, keep learning, and never stop having fun in the ever-evolving world of software development. 🚀😎
