Skip to content
Chavc0de

Self Hosted Gitlab Pipeline End to End

Aug 21, 2025 — CI/CD, GCP, AWS

Gitlab Set Up on Ec2 Instance/GCP VM

Prerequisites

Installing Docker and Docker Compose on your EC2 instance or GCP VM is essential for running GitLab. Here’s how to do it:

Terminal window
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

Other linux distributions can follow the official Docker installation guide (opens in a new window).

Installing docker-compose

Terminal window
sudo apt-get update
sudo apt-get install docker-compose-plugin
docker-compose version

Next Create a Docker Compose file for GitLab

mkdir gitlab cd gitlab vi docker-compose.yml

version: '3.8'
services:
gitlab:
image: gitlab/gitlab-ce:latest
restart: always
hostname: gitlab.${EXTERNAL_IP}.nip.io # Replace with your domain or IP
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://{EXTERNAL_IP}:8088'
gitlab_rails['initial_root_password'] = 'password' # Change this to a secure password
ports:
- '8088:8088'
volumes:
- gitlab-config:/etc/gitlab
- gitlab-logs:/var/log/gitlab
- gitlab-data:/var/opt/gitlab
restart: unless-stopped

Start GitLab

Terminal window
docker-compose up -d

Access GitLab

Terminal window
docker exec -it gitlab grep 'Password:' /etc/gitlab/initial_root_password

Configure GitLab

After logging in, you can configure GitLab settings, create projects, and manage users as needed

Create a New repository

  1. Click on the New Project button.
  2. Choose Create blank project.
  3. Fill in the project details:
    • Project name: Enter a name for your project.
    • Can keep other settings as default.
  4. Click on Create project.

Create a New Pipeline

  1. Navigate to your project.
  2. You can clone repository to your local machine using the command:
    Terminal window
    git clone http://<EXTERNAL_IP>:8088/<your-username>/<your-project-name>.git
  3. Create a new file named .gitlab-ci.yml in the root of your project directory.
  4. Before making changes we need runner to execute the pipeline. Can click here to jump how it is set up.

Set Up GitLab Runner

  1. Install GitLab Runner on your EC2 instance or GCP VM:
Terminal window
sudo apt-get install gitlab-runner
  1. Create runner in GitLab:
  1. Register the runner:
Terminal window
sudo gitlab-runner register
  1. Add the following content to the .gitlab-ci.yml file:
stages:
- build
- test
- deploy
build_job:
stage: build
script:
- echo "Building the application..."
- sleep 10 # Simulating build time
test_job:
stage: test
script:
- echo "Running tests..."
- sleep 5 # Simulating test time
deploy_job:
stage: deploy
script:
- echo "Deploying the application..."
- sleep 5 # Simulating deployment time
  1. Commit and push the changes to your GitLab repository:
    Terminal window
    git add .gitlab-ci.yml
    git commit -m "Add GitLab CI/CD pipeline"
    git push origin main

check the pipeline status in GitLab:

Create Child Pipelines

Child pipelines allow you to break down complex CI/CD processes into smaller, manageable parts. Here’s how to create child pipelines in GitLab:

  1. Create a Parent Pipeline: In your .gitlab-ci.yml, define a parent pipeline that triggers child pipelines.
frontend:
trigger:
include: frontend/.gitlab-ci.yml
strategy: depend
rules:
- changes: [frontend/*]
backend:
trigger:
include: backend/.gitlab-ci.yml
strategy: depend
rules:
- changes: [backend/*]
  1. Create Child Pipelines: In the frontend and backend directories, create separate .gitlab-ci.yml files for each child pipeline.
  2. Example Child Pipeline: Here’s an example of a child pipeline in frontend/.gitlab-ci.yml:
stages:
- build
- test
build_frontend:
stage: build
script:
- echo "Building frontend..."
- sleep 5 # Simulating build time
test_frontend:
stage: test
script:
- echo "Testing frontend..."
- sleep 5 # Simulating test time
  1. Example Child Pipeline for Backend: Similarly, create a backend/.gitlab-ci.yml file:
stages:
- build
- test
build_backend:
stage: build
script:
- echo "Building backend..."
- sleep 5 # Simulating build time
test_backend:
stage: test
script:
- echo "Testing backend..."
- sleep 5 # Simulating test time
  1. Commit and Push Changes: Commit and push the changes to your GitLab repository.
    Terminal window
    git add frontend/.gitlab-ci.yml backend/.gitlab-ci.yml
    git commit -m "Add child pipelines for frontend and backend"
    git push origin main
  2. Check Child Pipelines: Navigate to your project in GitLab and check the pipelines section. You should see the parent pipeline triggering the child pipelines for frontend and backend.

Conclusion

In this guide, we covered how to set up a self-hosted GitLab instance on an EC2 instance or GCP VM, create a new repository, set up a basic CI/CD pipeline, and create child pipelines. This setup allows you to manage your code and automate your development workflows effectively. Feel free to customize the .gitlab-ci.yml files and child pipelines according to your project’s requirements. GitLab provides extensive documentation and community support to help you explore more advanced features and configurations. For more information, refer to the GitLab CI/CD documentation (opens in a new window).

Additional Resources

Troubleshooting