Skip to content

Latest commit

 

History

History
464 lines (343 loc) · 13.5 KB

README.md

File metadata and controls

464 lines (343 loc) · 13.5 KB

GitLab

GitLab Logo

A complete DevOps platform in a single application

🚀 About

GitLab provides a complete CI/CD toolchain in a single application. One interface. One conversation. One permission model. Thousands of features. GitLab simplifies DevOps by providing everything you need to build, test, deploy, and monitor your applications in one place.

⏱️ Setup Information

  • Duration: 15-30 minutes
  • Requirements: Your Docker daemon should have at least 12GB RAM
    • GitLab: 6GB
    • Minikube: 2GB
    • Operating System: 2GB
  • Process: We'll bring up the Docker daemon and Minikube, then deploy GitLab on top of Minikube using Helm

📋 Provision

Github Codespace

Open in GitHub Codespaces

bash docker/docker.sh
bash minikube/minikube.sh
bash gitlab/gitlab.sh

Vagrant

vagrant up --provision-with basetools,docker,docsify,minikube,gitlab

Docker Compose

docker compose exec hashiqube /bin/bash
bash hashiqube/basetools.sh
bash docker/docker.sh
bash docsify/docsify.sh
bash minikube/minikube.sh
bash gitlab/gitlab.sh

🖥️ Installation Process

During the installation, you can monitor progress via the Kubernetes Dashboard once Minikube is installed. Initially, you'll see some red indicators as GitLab pods and services start up, which will gradually turn green as the deployment completes.

Minikube Dashboard during GitLab installation

Minikube Dashboard showing GitLab deployment progress

The GitLab provisioning process will look like this:

GitLab provision

Terminal output during GitLab provisioning

🔑 Accessing GitLab

Once installation is complete, you can access GitLab at:

  • URL: http://localhost:5580
  • Username: root
  • Password: The password is printed in the terminal output
    • Example: jMh629reoQ7FqtillBmLQZPY69JUStSFATXD11T5wMk39NtNezqIKohcIIwoxwvl
GitLab login page

GitLab login page

After logging in, you'll see the GitLab dashboard:

GitLab first login

GitLab dashboard after first login

🛠️ Hands-On Tutorial

Creating Your First Project

  1. Click on Create a ProjectCreate a Blank project
  2. Enter the project name as test
  3. Select the namespace as root
  4. Make it a Public repository
  5. Click Create Project
GitLab Create Project

Creating a new project in GitLab

Once created, you'll see your new repository:

GitLab Created Test Project

Newly created test project

Adding SSH Keys

To clone the repository using SSH, you need to add your SSH key to GitLab:

  1. Click on your profile icon in the top left corner
  2. Select Preferences
GitLab Navigate to Preferences

Navigating to user preferences

  1. Navigate to SSH Keys in the sidebar
GitLab Navigate to Preferences SSH Keys

SSH Keys section in user preferences

  1. On your local machine, retrieve your public SSH key:

    cat ~/.ssh/id_rsa.pub
  2. Copy the key and paste it into the GitLab SSH key field

  3. Important: Remove the expiry date

  4. Click Add Key

GitLab Enter SSH Keys

Adding an SSH key to your GitLab account

After adding the key:

GitLab Added SSH Keys

SSH key successfully added

Cloning Your Repository

  1. Navigate back to your test project
  2. Click the Clone button
  3. Copy the Clone with SSH URL

    ⚠️ Note: The HTTP link doesn't work correctly due to a bug with the port configuration. Use SSH instead.

GitLab Copy SSH Clone URL

Copying the SSH clone URL

  1. Clone the repository:

    git clone ssh://git@localhost:32022/root/test.git
GitLab SSH Clone Test Repository

Successfully cloning the repository

Creating a GitLab CI Pipeline

  1. In your local repository, create a .gitlab-ci.yml file:

    cd test
    nano .gitlab-ci.yml
  2. Add the following pipeline configuration:

    variables:
      REPOSITORY_URL: xxxxxxxxxxxxx.dkr.ecr.eu-west-1.amazonaws.com/container
    
    stages:
      - test
      - build
      - dev
      - stg
      - prd
    
    test:
      stage: test
      script:
        - echo 'Here you can run tests'
    
    build:
      stage: build
      script:
        - echo 'After Test stage was successful, here you can run build your container'
    
    dev:
      stage: dev
      script:
        - echo 'After Build stage was successful, here you can run your Development environment deployment'
    
    stg:
      stage: stg
      script:
        - echo 'After Dev stage was successful, here you can run your Staging environment deployment'
    
    prd:
      stage: prd
      script:
        - echo 'After Stg stage was successful, here you can run your Production environment deployment'
  3. Commit and push the changes:

    git add .gitlab-ci.yml
    git commit -am "adding .gitlab-ci.yml pipeline file"
    git push
GitLab Add Pipeline File

Adding a CI pipeline file to the repository

After pushing, you'll see the file in your repository:

GitLab Added Pipeline File

Pipeline file added to the repository

Setting Up a GitLab Runner

  1. In your project, navigate to SettingsCI/CD
GitLab Navigate to Settings CICD

Navigating to CI/CD settings

  1. Expand the Runners section
GitLab Expand Runners

Expanding the Runners section

  1. Click New Project Runner
  2. In the configuration form, check Run Untagged Jobs
  3. Click Create Runner
GitLab New Project Runner

Creating a new project runner

⚠️ Note: Due to a known bug, you might be redirected to a blank page. If this happens, manually add :5580 to the URL:

Instead of: http://localhost/root/test/-/runners/1/register?platform=linux
Use: http://localhost:5580/root/test/-/runners/1/register?platform=linux

GitLab Create Project Runner Correct Page

Runner registration page

Registering the Runner

  1. Copy the registration command, but remember to add the port :5580:

    Incorrect:

    gitlab-runner register --url http://localhost --token glrt-NRYUnqLZ2yzyutC1MYVV
    

    Correct:

    gitlab-runner register --url http://localhost:5580 --token glrt-NRYUnqLZ2yzyutC1MYVV
    
  2. SSH into your HashiQube instance:

    vagrant ssh
Vagrant SSH

SSHing into HashiQube to register the runner

  1. Register the runner with the corrected command

  2. Start the runner:

    gitlab-runner run
Vagrant SSH GitLab Runner Run

Starting the GitLab runner

  1. Return to GitLab to see the confirmation:
GitLab Runner Run Success

Runner successfully registered

  1. Click Go to the Runners page to see your registered runner:
GitLab Runners Page

Runners page showing the registered runner

Running Your Pipeline

  1. Navigate to BuildPipelines in the left sidebar
GitLab Test Project Run Pipeline

Pipelines section

  1. You'll see your pipeline is running:
GitLab Pipeline In Progress

Pipeline running in progress

  1. To see job details, navigate to Jobs in the left sidebar and click on a job:
GitLab Pipeline Job Details

Job details showing execution results

📚 Additional Resources

🔧 GitLab Provisioner Script

The GitLab environment is set up using this script:

#!/bin/bash

# Print the commands that are run
set -x

# Stop execution if something fails
set -e

# This script provisions Gitlab on Minikube

# Check if kubectl exists
if ! [ -x "$(command -v kubectl)" ]; then
  echo 'kubectl is not installed, please install minikube first" >&2'
  exit 1
fi

if ! [ -x "$(command -v helm)" ]; then
  echo 'helm is not installed, installing it ...' >&2
  curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3
  chmod 700 get_helm.sh
  ./get_helm.sh
  rm -f get_helm.sh
fi

# Check if Minikube environment exists
if ! [ -x "$(minikube status | grep Running)" ]; then
  echo 'Minikube is not running, starting it up ...' >&2
  # Start Minikube
  minikube start
fi

# Get the IP of the Minikube instance to allow access to Gitlab ports
MINIKUBEIP=$(minikube ip)
echo "Minikube is running at $MINIKUBEIP"

# Clean up
function clean_up {
  NAMESPACE=gitlab
  kubectl delete namespace $NAMESPACE
  helm uninstall gitlab
  echo "Successfully cleaned up $NAMESPACE namespace and gitlab helm release"
}

if [ "$1" == "clean" ] || [ "$1" == "delete" ] || [ "$1" == "destroy" ] || [ "$1" == "cleanup" ]; then
  clean_up
  exit 0
fi

# Get IP for Hashiqube
HOSTIP=$(hostname -I | awk '{print $2}')
if [[ -z "$HOSTIP" ]]; then
  HOSTIP=$(hostname -I | awk '{print $1}')
fi

# Add the GitLab Helm repository
helm repo add gitlab https://charts.gitlab.io/
helm repo update

# Install GitLab using Helm
helm upgrade --install gitlab gitlab/gitlab \
  --timeout 600s \
  --set global.hosts.domain=local \
  --set global.hosts.externalIP=$HOSTIP \
  --set global.edition=ce \
  --set [email protected] \
  --set nginx-ingress.enabled=false \
  --set global.ingress.enabled=false \
  --set global.ingress.configureCertmanager=false \
  --set gitlab-runner.install=true

# Wait for deployment to complete
echo "Waiting for GitLab deployment to complete..."
sleep 10

# Get the root password
ROOT_PASSWORD=$(kubectl get secret gitlab-gitlab-initial-root-password -ojsonpath='{.data.password}' | base64 --decode ; echo)

# Get the service ports
GITLAB_SSH_PORT=$(kubectl get service gitlab-gitlab-shell -ojsonpath='{.spec.ports[0].nodePort}')
GITLAB_HTTP_PORT=$(kubectl get service gitlab-webservice-default -ojsonpath='{.spec.ports[0].nodePort}')

# Forward ports for easy access
echo "Forward ports for easy access"
echo "kubectl port-forward svc/gitlab-webservice-default 5580:8080 &"
echo "kubectl port-forward svc/gitlab-gitlab-shell 32022:22 &"
kubectl port-forward svc/gitlab-webservice-default 5580:8080 > /dev/null 2>&1 &
kubectl port-forward svc/gitlab-gitlab-shell 32022:22 > /dev/null 2>&1 &

echo "GitLab is now running!"
echo "Access the web UI at http://localhost:5580"
echo "Login with username: root and password: $ROOT_PASSWORD"
echo "You can use SSH with port $GITLAB_SSH_PORT"
echo "You can use HTTP with port $GITLAB_HTTP_PORT"