How to Use GitLab Like a Pro: Projects, Pipelines & Cloud Deployment

 

Introduction

In this blog, we’ll explore GitLab’s key features and services. We’ll start with creating groups and projects, connecting GitLab to an AWS EC2 instance, setting up a CI/CD pipeline, and understanding important GitLab features like runners and mirroring.

Creating a Group in GitLab

  1. Go to the GitLab homepage and click on Groups.
  2. Click on Create Group.
  3. Enter a Group Name (e.g., Brillernotes).
  4. Choose the Visibility (Public or Private). I selected Private.
  5. Define your Role (e.g., DevOps).
  6. Specify Who will use this group (e.g., My Company).
  7. Select What the group will be used for (e.g., I want to manage my code).
  8. Optionally, Invite Members to the group.
  9. Click Create Group.

Once created, if the group is public, anyone can contribute; if private, only invited members can access it.

Creating a Project in GitLab

  1. From the GitLab homepage, click on Create Blank Project.
  2. Enter a Project Name (e.g., DevOps Notes).
  3. Set the Visibility (Public or Private).
  4. Select Project Configuration (e.g., initialize with a README).
  5. Click Create Project.

If the project is inside a private group, only group members can make changes. If public, anyone can contribute.

Connecting an AWS EC2 Instance to GitLab using SSH

Since GitLab does not allow password authentication, we use SSH keys for secure access.

Steps:

  1. Create an EC2 Instance on AWS.
  2. Generate SSH Keys on the EC2 instance:
  • ssh-keygen

3. Copy the public key.

4. Add the public key to GitLab:

  • Go to GitLab homepage → Settings → SSH Keys.
  • Click New Key, paste the public key, and click Add Key.

Now, your EC2 instance is authenticated with GitLab.

Cloning a GitLab Repository on EC2

To clone a GitLab project using SSH:

mkdir gitlab
cd gitlab
git clone <SSH-URL> # Use SSH instead of HTTPS
ls # Verify the project is cloned

Importing a GitHub Repo into GitLab Project

  1. Click Import Project in GitLab.
  2. Copy the GitHub project URL.
  3. Select Repository by URL and paste the GitHub URL.
  4. Assign the Group and set Visibility.
  5. Click Create Project.

Now, your GitHub project is imported into GitLab.

Setting Up a GitLab CI/CD Pipeline

GitLab CI/CD automates testing and deployment using a .gitlab-ci.yml file.

Creating a .gitlab-ci.yml File

  1. Click + → New File.
  2. Name it .gitlab-ci.yml.
  3. Add the following pipeline configuration:
stages:
- build
- test
- push
- deploy
build_job:
stage: build
script:
- echo "Building the application"
test_job:
stage: test
script:
- echo "Running tests"
push_job:
stage: push
script:
- echo "Pushing to Docker Hub"
deploy_job:
stage: deploy
script:
- echo "Deploying to EC2"
  1. Commit the file to the master branch.
  2. The pipeline will trigger automatically, running each stage in sequence.

Creating Parallel Jobs for Different Environments

To run tests in both development (dev) and production (prd) environments, modify the pipeline:

stages:
- build
- test
- push
- deploy
build_job:
stage: build
script:
- echo "Building the application"
test_job:
stage: test
script:
- echo "Testing in general"
dev_test_job:
stage: test
script:
- echo "Testing in development"
prd_test_job:
stage: test
script:
- echo "Testing in production"
push_job:
stage: push
script:
- echo "Pushing to Docker Hub"
deploy_job:
stage: deploy
script:
- echo "Deploying to EC2"

This runs test_jobdev_test_job, and prd_test_job in parallel within the test stage.

Key GitLab Features

GitLab Runners

  • GitLab provides Self-hosted Runners, where you configure your own servers for CI/CD.
  • Alternatively, GitLab-hosted Runners are available globally for automatic execution.

Mirroring Repositories

  • GitLab’s Mirror Repo feature syncs changes from GitHub to GitLab automatically.
  • This eliminates the need for manual cloning and pushing updates.

Auto DevOps

  • This feature automatically creates a CI/CD pipeline for your projects without writing a .gitlab-ci.yml file.

Here is the YouTube Link: https://youtube.com/@raeesq.?si=v_QK6Q2XXMf9mKep

Conclusion

GitLab is a powerful platform for managing source code, CI/CD pipelines, and team collaboration. By following this guide, you can:
✅ Create Groups & Projects
✅ Connect AWS EC2 with GitLab using SSH
✅ Clone and Import Projects
✅ Set Up CI/CD Pipelines
✅ Use Advanced GitLab Features

If you found this guide helpful, share it with others! Stay focused, stay consistent, and keep learning. 🚀

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