launch GitLab and GitLab Runners locally

In this article, we'll look at how to deploy your own GitLab server and GitLab Runners using Docker Compose. This guide will help you create a local environment to learn and practice GitLab CI/CD. We'll go through all the steps: from setting up containers to registering runners and creating an example CI/CD pipeline. Whether you're new to CI/CD or an experienced developer, this guide will provide you with valuable knowledge to improve your development process.

It's important to note that there is quite a bit of information on the Internet about how to run GitLab Runner, which communicates with a local GitLab server. Most tutorials focus on using the official GitLab.com, but for training and testing purposes, installing locally has a number of advantages.

First, it allows you to experiment in a safe environment without risking real projects. Secondly, many companies use their own, often outdated, versions of GitLab. Having the skills to set up a local server, you can recreate an environment identical to the one used in your organization, which is extremely useful for debugging and testing CI/CD processes.

Main part of the article:

Why do you need your own GitLab server?

Your own GitLab server allows you to:

  1. Explore GitLab CI/CD in a safe environment

  2. Experiment with settings without risking production systems

  3. Create a test site for practicing CI/CD practices

  4. Get full control over your infrastructure and data

How to run your GitLab server in a container?

1. Preparing the file structure:

mkdir -p ./data/docker/gitlab/{var/opt/gitlab,var/log/gitlab,etc/gitlab-runner,var/run/docker.sock}
mkdir -p ./config
touch ./config/config.toml

2. Launch Docker Compose:

Link to docker-compose.yml

docker-compose up --build

3. Access to the GitLab web interface:

4. Setting up an SSH key:

  1. Go to http://localhost:8000/-/user_settings/ssh_keys

  2. Add the contents of your ~/.ssh/id_rsa.pub

Registering GitLab Runner to Run Tasks in Docker

  1. Create in the same folder with docker-compose.yml file .env:

RUNNER_NAME=ИмяПроекта
REGISTRATION_TOKEN=ВашТокенРегистрации

REGISTRATION_TOKEN – It can be obtained at http://localhost:8000/root/${Проект}/-/settings/ci_cd In chapter Runner's-> Project runners -> : -> Скопировать

  1. Restart the service register-runner:

docker-compose restart register-runner

Managing GitLab Runner

To run commands, log into the container register-runner:

# Запуск всех раннеров
gitlab-runner run

# Список раннеров
gitlab-runner list

# Проверка раннеров
gitlab-runner verify

# Удаление всех раннеров
gitlab-runner unregister --all-runners

# Удаление конкретного раннера
gitlab-runner unregister --name ${name_runner}

# Файл с конфигурациями раннеров
vim /etc/gitlab-runner/config.toml

Example of setting up a project with GitLab CI/CD

  1. Create a new project in GitLab, named test

  2. Register your runner. How to do this was written above

  3. Clone the project: git clone ssh://git@localhost:8822/root/test.git

  4. Create a branch: git checkout -b test_ci

  5. Add a file main.py:

def main():
    print('Hello World')

main()
  1. Create a file .gitlab-ci.yml:

stages:
    - linting

variables:
    COMMIT_MESSAGE_PATTERN: "^.{0,10}:[[:space:]]#[0-9]{5,6}[[:space:]].{5,150}$"

.default_rules: &default_rules
    rules:
        - if: '$CI_COMMIT_BRANCH != "main" && $CI_PIPELINE_SOURCE == "merge_request_event"'

commit-name-test:
    stage: linting
    <<: *default_rules
    script:
        - |
            if [[ "$CI_COMMIT_MESSAGE" =~ $COMMIT_MESSAGE_PATTERN ]]; then
              echo "Корректный формат сообщения коммита"
            else
              echo "Некорректный формат: $CI_COMMIT_MESSAGE"
              exit 1
            fi

pylint:
    stage: linting
    <<: *default_rules
    image: python:3.9
    before_script:
        - pip install pylint
    script:
        - pylint **/*.py
  1. Commit the changes: git add -A && git commit -m "feat: #12345 Добавлен CI пайплайн"

  2. Submit changes: git push -u origin test_ci

  3. Create a Merge Request in GitLab

  4. Check the execution of the pipeline in the Assembly Line tab

This guide will help you set up a local GitLab CI/CD environment for learning and experimentation. Keep in mind that a production environment will require additional security and performance settings.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *