Project Overview
In this project, we will explore the deployment of a TO-DO application built using NodeJS on using a GitLab CI/CD pipeline.
This project involves a NodeJS TO-DO web application. To automate the deployment process, we have set up a CI/CD pipeline using GitLab. The pipeline consists of several stages, including code checkout, building Docker images, deploying services to servers using Docker Compose, and updating the Docker registry on the server.
By setting up this pipeline, we aim to streamline the deployment process of our NodeJS web application and ensure that each new change to the codebase is automatically deployed. This approach will save us time and effort while ensuring the reliability and scalability of our application.
Architecture of Project
Pre-requisites for Project
Setup SSH key in local machine
Go to your local machine -> ssh-keygen -t rsa
Paste generated public key in project setting.
- Go to GitLab profile -> Select your avatar -> Edit Profile -> SSH Keys
- Add new key.
- Paste copied key into box.
- Now, Clone project from GitLab repository : TODO App
- Copy SSH URL
- Paste it local system.
- Now, we have source code for the project.
Setup CI/CD pipeline of the project
Create
.gitlab-ci.yml
file in local repository.Push to the GitLab project.
As soon as file pushed to project, pipeline will be triggered.
- Our Job is running on SaaS runner, Let's activate project runner from our previous blog.
- Go to setting of Project -> CICD -> Runners -> Expand -> Enable for this project
- Now, dev-runner is enable for our TODO project.
Install pre-requisites on runner
- Install docker
$ sudo apt install docker.io -y
- Install docker compose V2
$ sudo apt install docker-compose-v2
Now, our jobs are running on project runner.
Setup variables for pipeline
Go to project setting -> CICD -> Variables
- Click on Add variables.
- Creating DockerHub username variables,
Creating DockerHub Password variable,
For this you need token generated from DockerHub. Go to your DockerHub account setting -> Personal Access Token and create new token.
- Now add new variable,
- Now both variables are created.
- Pipeline will be,
stages: # Define your stages first to configure pipeline and its an mandatory steps.
- build
- test
- push
- deploy
build_job:
stage: build
script:
- echo "Image building in progress"
- 'whoami'
- 'docker build -t node-app:v1 .'
- echo "Image Build SUCCESS"
tags:
- dev
test_job:
stage: test
script:
- echo "This is testing stage of our docker build"
tags:
- dev
push_job:
stage: push
script:
- echo "Pushing image to dockerHub"
- 'docker login -u $DOCKER_USER -p $DOCKER_PASS'
- 'docker image tag node-app:v1 $DOCKER_USER/node-app:v1'
- 'docker push $DOCKER_USER/node-app:v1'
- echo "Docker image pushed successfully..."
tags:
- dev
deploy_job:
stage: deploy
script:
- echo "Deploying................"
- 'docker stop node-app && docker rm node-app'
- 'docker run -d --name node-app -p 8000:8000 node-app:v1'
tags:
- dev
- Now, run the pipeline.
- Final Result of running application,
Deployment using Docker Compose
- Create docker-compose.yml file in project,
version: '3.3'
services:
web:
image: "ojasjawale/node-app:v1"
ports:
- "8000:8000"
- Modify deploy_job in pipeline,
deploy_job:
stage: deploy
script:
- echo "Deploying................"
- 'docker-compose down'
- 'docker-compose up -d'
tags:
- dev
- Running pipeline,
- Final output,
Connect With Me
Follow me for more amazing content :)