GitLab Project-Setting Up CI/CD for Node.js To-Do App on a Self-Hosted GitLab Runner

 

Step 1: Clone the Project to GitLab

  1. Go to GitLab and click on Import Project.
  2. Copy the GitHub repository URL for node-todo-cicd (search for shubham londhe on GitHub).
  3. Paste the URL into GitLab and start the import process.
  4. Once imported, you’ll see the project on your GitLab homepage.

Step 2: Configure GitLab Runner

  1. Go to Groups > Settings > CI/CD.
  2. Find Runners and click Project Runner.
  3. Click New Project Runner.
  4. Add a tag (e.g., dev)—this is similar to labels in Jenkins.
  5. Click Create Runner.

Step 3: Install GitLab Runner on an EC2 Instance

  1. Copy the installation commands from GitLab (look for How to Install… section).
  2. SSH into your EC2 instance and run the commands as a superuser (sudo su).
  3. Choose the appropriate architecture (e.g., amd64).
  4. Once installed, start the service:
  • sudo gitlab-runner status # Verify it's running

Step 4: Register GitLab Runner

  1. Run the command:
  • gitlab-runner register

2. Enter the GitLab URL and token from the runner settings.

3. Press Enter to accept the default GitLab URL.

4. Verify the runner with a name (e.g., raees-qazi).

5. Select the executor type (e.g., shell).

6. The runner is now registered successfully.

Step 5: Set Up SSH Keys

  1. Generate SSH keys:
  • ssh-keygen

2. Copy the public key:

  • cat ~/.ssh/id_rsa.pub

3. In GitLab, go to User Settings > SSH Keys.

4. Click New Key, paste the public key, and save it.

Step 6: Install Docker on EC2

Run the following commands to install Docker and configure permissions:

sudo apt-get update
sudo apt-get install docker.io docker-compose-v2 -y
sudo usermod -aG docker ubuntu
sudo usermod -aG docker gitlab-runner

Verify groups:

cat /etc/group

Step 7: Configure Environment Variables for Docker Hub

  1. In GitLab, go to Settings > CI/CD > Variables.
  2. Click Expand and add the following variables:
  • DOCKERHUB_USER: Your Docker Hub username (masked, protected).
  • DOCKERHUB_PASS: Your Docker Hub password/PAT (masked, protected).

Step 8: Create .gitlab-ci.yml Pipeline

Write the CI/CD pipeline in YAML format:

stages:
- build
- test
- push_to_dockerhub
- deploy
build_job:
stage: build
script:
- docker build -t node-app:latest .
tags:
- dev
test_job:
stage: test
script:
- echo "testing..."
tags:
- dev
push_job:
stage: push_to_dockerhub
script:
- docker login -u $DOCKERHUB_USER -p $DOCKERHUB_PASS
- docker tag node-app:latest $DOCKERHUB_USER/node-app:latest
- docker push $DOCKERHUB_USER/node-app:latest
tags:
- dev
deploy_job:
stage: deploy
script:
- docker compose up -d
tags:
- dev

Step 9: Configure AWS Security Group

  1. Go to AWS Security Groups.
  2. Allow port 8000 (or change based on project needs).

Step 10: Verify Deployment

  1. Navigate to the project directory:
  • cd /home/gitlab-runner/builds/0/<project-name> ls # Your project files should be visible

2. Configure shell settings:

  • cd /home/gitlab-runner ls -a # List hidden files vim .bash_logout # Open file in Vim editor
  • Add comments inside the file:
  • # if # <command will be written> # fi
  • Save and exit Vim.

3. Return to the main terminal:

  • cd ~

4. Run the GitLab Runner:

  • gitlab-runner run

Your Node.js To-Do application is now set up with CI/CD using a self-hosted GitLab Runner on AWS EC2!

Comments

Popular posts from this blog

📘 Understanding Prometheus in a Simple Way-Part 3 (For DevOps Beginners)

Grafana Setup & Dashboard Creation (Part-5)— Explained by Raees Yaqoob Qazi

My First Python Program: A Simple Calculator