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:
Explore GitLab CI/CD in a safe environment
Experiment with settings without risking production systems
Create a test site for practicing CI/CD practices
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:
docker-compose up --build
3. Access to the GitLab web interface:
4. Setting up an SSH key:
Go to
http://localhost:8000/-/user_settings/ssh_keys
Add the contents of your
~/.ssh/id_rsa.pub
Registering GitLab Runner to Run Tasks in Docker
Create in the same folder with
docker-compose.yml
file.env
:
RUNNER_NAME=ИмяПроекта
REGISTRATION_TOKEN=ВашТокенРегистрации
REGISTRATION_TOKEN
– It can be obtained athttp://localhost:8000/root/${Проект}/-/settings/ci_cd
In chapterRunner's
->Project runners
->:
->Скопировать
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
Create a new project in GitLab, named
test
Register your runner. How to do this was written above
Clone the project:
git clone ssh://git@localhost:8822/root/test.git
Create a branch:
git checkout -b test_ci
Add a file
main.py
:
def main():
print('Hello World')
main()
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
Commit the changes:
git add -A && git commit -m "feat: #12345 Добавлен CI пайплайн"
Submit changes:
git push -u origin test_ci
Create a Merge Request in GitLab
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.