GitlabのCI/CDを使ってみたかったのでやってみました。
Gitlab、Gitlab Runnerは共にDockerを使用してします。また、Gitlab Runnerで実行するテストコードでもDockerを使用しています。
構築にとても苦労したので構築手順を残します。
色々試しすぎて記事中の各種設定ファイルに不要な設定があるかもしれませんがとりあえず構築できたので良しとします。
環境
- Windows11 Pro
- Docker Desktop 4.29.0
- Gitlab EE 16.11(EE版だけどライセンスは未購入)
構築手順
docker-compose.yamlの定義は以下。
services:
gitlab:
container_name: 'gitlab-local'
hostname: 'gitlab-local.com'
image: 'gitlab/gitlab-ee:latest'
restart: always
ports:
- '8989:80' # Hostの8989ポートをGitLabの80ポートにマッピング
- '2222:22' # Hostの2222ポートをSSHの22ポートにマッピング
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://gitlab-local/'
nginx['listen_port'] = 80
gitlab_rails['gitlab_shell_ssh_port'] = 2222 # SSHポートを2222に設定
# 他のgitlab.rb設定をこちらに追加
volumes:
- gitlab-config:/etc/gitlab
- gitlab-logs:/var/log/gitlab
- gitlab-data:/var/opt/gitlab
- gitlab-artifacts:/var/opt/gitlab/gitlab-rails/shared/artifacts
shm_size: '256m'
gitlab-runner:
image: gitlab/gitlab-runner:latest
restart: always
environment:
- GITLAB_URL=http://gitlab/
volumes:
- 'C:\develop\docker\gitlab-runner\config:/etc/gitlab-runner'
- /var/run/docker.sock:/var/run/docker.sock
depends_on:
gitlab:
condition: service_started
command: ["run", "--user=gitlab-runner", "--working-directory=/home/gitlab-runner"]
extra_hosts:
- "gitlab-local:172.18.0.2"
networks:
default:
external:
name: gitlab_default
volumes:
gitlab-config:
gitlab-logs:
gitlab-data:
gitlab-artifacts:
以下のフォルダを作成します。
docker-composeの定義によるのでパスは自由に変更してください。
- C:\develop\docker\gitlab
- C:\develop\docker\gitlab\config
- C:\develop\docker\gitlab\logs
- C:\develop\docker\gitlab\data
- C:\develop\docker\gitlab-runner
- C:\develop\docker\gitlab-runner\config
以下で起動します。起動に5分弱かかります。
> docker-compose up -d
GitLabにログイン
Gitlabにログインするためにパスワードが必要なので以下で初期パスワードを取得します。
Gitlabのコンテナ内で以下を実行
# cat /etc/gitlab/initial_root_password
# WARNING: This value is valid only in the following conditions
# 1. If provided manually (either via `GITLAB_ROOT_PASSWORD` environment variable or via `gitlab_rails['initial_root_password']` setting in `gitlab.rb`, it was provided before database was seeded for the first time (usually, the first reconfigure run).
# 2. Password hasn't been changed manually, either via UI or via command line.
#
# If the password shown here doesn't work, you must reset the admin password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password.
Password: QmIpmlv+CpUG1+BMxgILmK7Ug/iehoHbzIWFeYpFcIg=
# NOTE: This file will be automatically deleted in the first reconfigure run after 24 hours.
http://localhost:8989/ にアクセスしてユーザ名がroot、パスワードが先ほどcatして出てきたものを入力してログインします。
プロジェクト作成(Githubのリポジトリをインポート)
Githubでプロジェクトを管理していたのでそれを使用します。Springbootのプロジェクトです。
メニューの下に管理エリアとあるのでそれをクリック。

管理エリアに移動し、設定 – 一般をクリック。

設定のインポートとエクスポートを展開し、Githubにチェックを入れ設定を保存。

トップページに戻って新しいプロジェクトボタンをクリック。
プロジェクトのインポートを選択するとGitHubが選択項目として出てくるのでそちらをクリック。

Githubのトークンを作成後、以下に入力して認証。

Githubにあるリポジトリの一覧が表示されるので必要なプロジェクトをインポート。


Gitlab Runnerの設定
インポートしたプロジェクトにアクセスし、メニューの設定 – CI/CDをクリック。
Runnerを展開し、新規プロジェクトRunnerをクリック。

プラットフォームはLinuxを選択。
タグは適当でOKだが、後ほど.gitlab-ci.ymlにて使用するのでわかりやすいものを設定する。
で、ランナーを作成をクリック。
作成するとトークンが表示されるのでコピーしておく。
コマンドが記載されているが、そのコマンドは不要。

C:\develop\docker\gitlab-runner\configにconfig.tomlを以下の内容で作成する。
Gitlab Runnerの再起動等は特に必要なさそう。ログを見てると変更があれば勝手にリロードしている。
concurrent = 1
check_interval = 0
[session_server]
session_timeout = 1800
[[runners]]
name = "docker-runner"
url = "http://gitlab-local/"
token = "glrt-4XHXswxfgby66whtExpH" # Replace with your GitLab Runner registration token
output_limit = 102400 # ログ出力上限
executor = "docker"
[runners.custom_build_dir]
[runners.docker]
tls_verify = false
image = "docker:latest"
privileged = true
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]
shm_size = 0
network_mode = "gitlab_default"
extra_hosts = ["host.docker.internal:host-gateway"]
[runners.cache]
上記の設定が完了後に設定 – CI/CD – Runnerを見て、”Runnerの状態はオンラインです。”となっていればOK。

Docker Desktopの設定を変更
Docker Desktopの右上にある歯車から設定を開き、Generalで”Expose daemon on tcp://localhost:2375 without TLS”にチェックを入れる。

Docker Desktop Serviceが起動されていないかもしれないのでコントロールパネルからサービスを起動する。


.gitlab-ci.yml
以下のようにSpringbootのプロジェクト直下に.gitlab-ci.ymlを作成する。
tagsに記載するのは先ほどGitlab Runner作成時に指定したタグと同じ文字列。
image: docker:latest
services:
- name: docker:dind
command: ["--tls=false"]
stages:
- build
- test
variables:
TESTCONTAINERS_HOST_OVERRIDE: "host.docker.internal"
DATABASE_URL: "jdbc:postgresql://host.docker.internal:5432/xxxxx"
TZ: "Asia/Tokyo"
DOCKER_HOST: "tcp://docker:2375"
DOCKER_TLS_CERTDIR: ""
DOCKER_DRIVER: overlay2
cache:
paths:
- .m2/repository/
- target/
build_app:
tags:
- tagname_xxx
stage: build
before_script:
- apk add --update maven
script:
- sh mvnw clean compile
test_app:
tags:
- tagname_xxx
stage: test
before_script:
- apk add --update maven
script:
- sh mvnw package
ここまで設定すると、勝手に.gitlab-ci.ymlの設定に従ってジョブが実行されていきます。
以下のようになっていれば成功です。
冒頭でも言いましたが、試行錯誤しすぎて無駄な設定や変な設定があるかもしれませんがご了承ください。

以上です。
コメント