Skip to content

GitLab Master Guide

GitLab is a web-based Git repository manager with wiki and issue tracking features. It provides a comprehensive platform for version control, continuous integration, and continuous deployment (CI/CD). This guide will walk you through the installation process and various scenarios for using GitLab, tailored for beginners, intermediate users, and advanced users.

Installation Guide

  1. Install Docker: Follow the installation instructions for your operating system from Docker's official website.

  2. Run GitLab via Docker:

    docker run --detach \
      --hostname gitlab.example.com \
      --publish 443:443 --publish 80:80 --publish 22:22 \
      --name gitlab \
      --restart always \
      --volume /srv/gitlab/config:/etc/gitlab \
      --volume /srv/gitlab/logs:/var/log/gitlab \
      --volume /srv/gitlab/data:/var/opt/gitlab \
      gitlab/gitlab-ce:latest
    

  3. Access GitLab: Open your web browser and navigate to http://gitlab.example.com. Follow the initial setup instructions.

On Ubuntu

  1. Add GitLab Repository:

    sudo apt-get update
    sudo apt-get install -y curl
    curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
    

  2. Install GitLab:

    sudo apt-get install -y gitlab-ce
    

  3. Configure GitLab: Edit the /etc/gitlab/gitlab.rb file to configure GitLab settings.

  4. Reconfigure GitLab:

    sudo gitlab-ctl reconfigure
    

  5. Access GitLab: Open your web browser and navigate to http://your-server-ip.

BEGINNER LEVEL: First Steps with GitLab

Scenario 1: Setting Up GitLab for the First Time

Unlocking GitLab and installing essential plugins.

sequenceDiagram
    participant Developer
    participant GitLab Server
    Developer->>GitLab Server: Access GitLab URL
    GitLab Server->>Developer: Show initial setup page
    Developer->>GitLab Server: Set root password
    GitLab Server->>Developer: Redirect to login page
    Developer->>GitLab Server: Login with root credentials
    GitLab Server->>Developer: Show dashboard
# Access GitLab URL in your browser
# Set root password when prompted
# Login with root user and the set password

Scenario 2: Creating Your First Project

Creating a new project and adding a file.

sequenceDiagram
    participant Developer
    participant GitLab Server
    Developer->>GitLab Server: Click "New Project"
    GitLab Server->>Developer: Show project creation form
    Developer->>GitLab Server: Fill project details
    GitLab Server->>Developer: Create project
    Developer->>GitLab Server: Add README.md
    GitLab Server->>Developer: Commit file
# Create a new project via GitLab UI
# Add a README.md file via GitLab UI
# Commit the file

Scenario 3: Cloning a Project

Cloning a project to your local machine.

sequenceDiagram
    participant Developer
    participant GitLab Server
    Developer->>GitLab Server: Get project URL
    Developer->>GitLab Server: Clone project
    GitLab Server->>Developer: Download project files
# Get the project URL from GitLab
git clone http://gitlab.example.com/your-project.git
cd your-project

INTERMEDIATE LEVEL: Working with GitLab CI/CD

Scenario 4: Setting Up CI/CD Pipeline

Creating a .gitlab-ci.yml file for CI/CD.

sequenceDiagram
    participant Developer
    participant GitLab Server
    Developer->>GitLab Server: Create .gitlab-ci.yml
    GitLab Server->>Developer: Detect CI/CD configuration
    Developer->>GitLab Server: Push changes
    GitLab Server->>Developer: Trigger pipeline
    GitLab Server->>Developer: Run jobs
# .gitlab-ci.yml
stages:
  - build
  - test
  - deploy

build_job:
  stage: build
  script:
    - echo "Building the project"

test_job:
  stage: test
  script:
    - echo "Running tests"

deploy_job:
  stage: deploy
  script:
    - echo "Deploying the project"

Scenario 5: Using GitLab Runner

Setting up GitLab Runner for CI/CD.

sequenceDiagram
    participant Developer
    participant GitLab Server
    Developer->>GitLab Server: Install GitLab Runner
    Developer->>GitLab Server: Register Runner
    GitLab Server->>Developer: Runner ready
# Install GitLab Runner
sudo apt-get install -y gitlab-runner

# Register Runner
sudo gitlab-runner register

ADVANCED LEVEL: Advanced GitLab Features

Scenario 6: Using GitLab Merge Requests

Creating and managing merge requests.

sequenceDiagram
    participant Developer
    participant GitLab Server
    Developer->>GitLab Server: Create new branch
    Developer->>GitLab Server: Push changes
    Developer->>GitLab Server: Create merge request
    GitLab Server->>Developer: Review and merge
# Create a new branch
git checkout -b feature/new-feature

# Push changes
git push origin feature/new-feature

# Create a merge request via GitLab UI

Scenario 7: GitLab Security Scanning

Setting up security scanning for your project.

sequenceDiagram
    participant Developer
    participant GitLab Server
    Developer->>GitLab Server: Add security scan job
    Developer->>GitLab Server: Push changes
    GitLab Server->>Developer: Run security scan
    GitLab Server->>Developer: Report vulnerabilities
# .gitlab-ci.yml
stages:
  - build
  - test
  - security

build_job:
  stage: build
  script:
    - echo "Building the project"

test_job:
  stage: test
  script:
    - echo "Running tests"

security_scan:
  stage: security
  script:
    - echo "Running security scan"
    - gitlab-sast

Quick Reference: Essential Commands

Command Description Level
git clone Clone a repository Beginner
git push Push changes to remote Beginner
git pull Pull changes from remote Beginner
git checkout Switch branches Intermediate
git merge Merge branches Intermediate
git rebase Rebase branch Advanced
git stash Stash changes Intermediate
git bisect Find bad commit Advanced

Pro Tips for All Levels

  1. Use descriptive commit messages: "Fix login bug" > "Update stuff".
  2. Commit early, commit often: Small commits are easier to manage.
  3. Never git reset --hard on shared branches: It rewrites history.
  4. Use branches for every feature: Keep the main branch clean.
  5. Review before committing: git diff --staged shows what you're about to commit.
  6. Configure helpful aliases: git config --global alias.co checkout.

Happy coding!🚀