gitlab-runnerのDockerイメージを使用して、ローカル環境でGitLab CIを実行する
はじめに
以前 Homebrewでgitlab-runnerをインストール、ローカル環境でGitLab CIを実行するという記事を書きました。
今回はローカル環境にgitlab-runnerのdockerイメージを使用して、runnerを動かしてみようと思います。
https://gitlab.com/ に作成したリポジトリは空のリポジトリを作成する、と同じものを使います。
環境
バージョン | |
---|---|
MacBook Pro M1 | Ventura 13.2.1 |
Docker | Docker version 20.10.12, build e91ed57 |
Docker Compose | Docker Compose version v2.2.3 |
limactl | 0.14.2 |
GitLab | Enterprise Edition 15.11.0-pre |
docker compose
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:
実行
$ docker compose up
gitlab-runnerが使っているイメージはUbuntuです。
$ docker compose exec runner cat /etc/os-releaseNAME="Ubuntu"VERSION="20.04.5 LTS (Focal Fossa)"ID=ubuntuID_LIKE=debianPRETTY_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=focalUBUNTU_CODENAME=focal
Dockerfileを読む
gitlab/gitlab-runnerイメージのentrypointとcommandの設定を確認すると、以下のように設定されていました。
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
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
を指定します。
$ 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.1Running 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/380872Registering runner... succeeded runner=xxxxxxxxxxxxxRunner 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が登録されていることを確認しました。
CIを実行する
stages: - test
test: stage: test script: - node -v
登録を解除
$ docker compose exec runner gitlab-runner unregister --all-runners
$ docker compose down -v