Skip to content

gitlab-runnerのDockerイメージを使用して、ローカル環境でGitLab CIを実行する

はじめに

以前 Homebrewでgitlab-runnerをインストール、ローカル環境でGitLab CIを実行するという記事を書きました。

今回はローカル環境にgitlab-runnerのdockerイメージを使用して、runnerを動かしてみようと思います。

https://gitlab.com/ に作成したリポジトリは空のリポジトリを作成する、と同じものを使います。

環境

バージョン
MacBook Pro M1Ventura 13.2.1
DockerDocker version 20.10.12, build e91ed57
Docker ComposeDocker Compose version v2.2.3
limactl0.14.2
GitLabEnterprise Edition 15.11.0-pre

docker compose

compose.yaml

compose.yaml
services:
runner:
image: gitlab/gitlab-runner:latest
restart: always
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- gitlab-runner-config:/etc/gitlab-runner
volumes:
gitlab-runner-config:

実行

Terminal window
$ docker compose up

gitlab-runnerが使っているイメージはUbuntuです。

Terminal window
$ docker compose exec runner cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.5 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.5 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

Dockerfileを読む

gitlab/gitlab-runnerイメージのentrypointとcommandの設定を確認すると、以下のように設定されていました。

Dockerfile
ENTRYPOINT ["/usr/bin/dumb-init", "/entrypoint"]
CMD ["run", "--user=gitlab-runner", "--working-directory=/home/gitlab-runner"]

https://gitlab.com/gitlab-org/gitlab-runner/-/blob/main/dockerfiles/runner/ubuntu/Dockerfile#L35-L36

entrypoint

Terminal window
exec gitlab-runner "$@"

https://gitlab.com/gitlab-org/gitlab-runner/-/blob/main/dockerfiles/runner/ubuntu/entrypoint#L22

gitlab-runnerを登録する

前回の記事 gitlab-runnerを登録するでは、registerコマンドを入力したときインタラクティブに設定を追加しました。

今回は、オプション引数に値を渡し、コマンド1回で登録します。--registration-tokenに渡すtokenはSettings > CI/CD > Runnersから確認できます。

executorをdockerにしているので、CIの実行に使用するイメージにnode:18-alpineを指定します。

Terminal window
$ docker compose exec runner gitlab-runner register --non-interactive --url https://gitlab.com --registration-token xxxxxxxxxxx --executor docker --docker-image node:18-alpine
Runtime platform arch=arm64 os=linux pid=289 revision=dcfb4b66 version=15.10.1
Running in system-mode.
WARNING: Support for registration tokens and runner parameters in the 'register' command has been deprecated in GitLab Runner 15.6 and will be replaced with support for authentication tokens. For more information, see https://gitlab.com/gitlab-org/gitlab/-/issues/380872
Registering runner... succeeded runner=xxxxxxxxxxxxx
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"

Settings > CI/CD > Runnersからrunnerが登録されていることを確認しました。 succeed-in-registoration

CIを実行する

.gitlab-ci.yml
stages:
- test
test:
stage: test
script:
- node -v

commit-changes

finish-job

登録を解除

Terminal window
$ docker compose exec runner gitlab-runner unregister --all-runners
$ docker compose down -v

参考