Dans ce tutoriel nous allons voir comment créer une instance de Gitlab et créer un runner dans l’objectif de mettre en place une CI/Cd
Docker-compose
Premièrement nous devons définir notre docker-compose.yml :
Nous créer un réseau pour nos deux instances et affecter des adresses IP fixes
Gitlab : 10.5.0.10
Gitlab-runner : 10.5.0.11
services:
# GITLAB
gitlab-web:
image: 'gitlab/gitlab-ce:latest'
restart: always
container_name: gitlab-web
hostname: '10.5.0.10:80'
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://10.5.0.10:80'
gitlab_rails['gitlab_shell_ssh_port'] = 2222
ports:
- "7878:80"
- "443:443"
- "2222:22"
volumes:
- './gitlab/config:/etc/gitlab'
- './gitlab/logs:/var/log/gitlab'
- './gitlab/data:/var/opt/gitlab'
networks:
gitlab-network:
ipv4_address: 10.5.0.10
# RUNNER
gitlab-runner1:
image: gitlab/gitlab-runner:alpine
restart: always
container_name: gitlab-runner1
hostname: gitlab-runner1
depends_on:
- gitlab-web
volumes:
- ./config/gitlab-runner:/etc/gitlab-runner
- /var/run/docker.sock:/var/run/docker.sock
networks:
gitlab-network:
ipv4_address: 10.5.0.11
networks:
gitlab-network:
name: gitlab-network
ipam:
config:
- subnet: 10.5.0.0/16
gateway: 10.5.0.1
Puis exécuter la commande docker-compose up -d
Interface GitLab
L’interface GitLab est accessible depuis localhost:7878
, vous pouvez créer un projet vierge puis nous allons y associer des runners.
Pour ce faire dans Settings → CI/CD → Runners créer un nouveau runner
En cliquant sur Create Runner vous aurez un léger problème. En effet il souhaite vous rediriger vers la page web sur 10.5.0.10 pour créer le runner (nous n’avons pas mis en place de DNS)
Attendez que la page échoue et remplacez 10.5.0.10 par localhost pour pouvoir continuer la configuration du runner :
http://10.5.0.10/root/testgitlab/-/runners/102/register
Par http://localhost:7878/root/testgitlab/-/runners/102/register
Setup le runner
Dans le container gitlab-runner exécuter la commande de l’image précédente
docker exec -it <container-id-gitlab-runner> bash
gitlab-runner register --url http://10.5.0.10 --token glrt-LRYmxvEFu2L1qr7PaKpS
Puis configurer le runner.
Etant donné que nos deux container sont sur le réseau gitlab-network
nous devons mettre à jour la configuration du runner /etc/gitlab-runner/config.toml
en ajoutant
[runners.docker]
network_mode = "gitlab-network"
Exécuter une pipeline
Vous pouvez maintenant créer votre projet et une gitlab-ci afin d’essayer la configuration
stages:
- build
- test
build-job:
stage: build
script:
- echo "Compiling the code..."
- echo "Compile complete."
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
- if: '$CI_MERGE_REQUEST_ID'
unit-test-job:
stage: test
script:
- echo "Running unit tests... This will take about 60 seconds."
- echo "Code coverage is 90%"
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
- if: '$CI_MERGE_REQUEST_ID'