Skip to content

Homebrewでgitlab-runnerをインストール、ローカル環境でGitLab CIを実行する

はじめに

GitLab CIの動作検証のため何回もコミットしていると以下の問題が発生すると思います。

  1. コミットが汚れる
  2. runnerの待ち時間が長く、素早く改善できない
  3. gitlabのプランによっては使用時間に制限があり、気軽に試せない

1のコミットに関しては、最悪mainにマージするタイミングでsquashすれば良いですが 2,3番目は開発に影響を与えかねません。

そのためこの記事ではGitLab Runner構築のススメ - GitLab blogを参考に runnerのみをローカル環境で実行し、気軽に検証できる環境を構築します。

環境

バージョン
MacBook Pro M1Ventura 13.2.1
Homebrew4.0.13
GitLabEnterprise Edition 15.11.0-pre

Gitlab Runnerをインストールする

検証端末はMacBookなので、Homebrewでインストールします。

Terminal window
$ brew install gitlab-runner
$ which gitlab-runner
/opt/homebrew/bin/gitlab-runner
$ brew services start gitlab-runner
==> Successfully started `gitlab-runner` (label: homebrew.mxcl.gitlab-runner

gitlab-runnerをインストールすると、~/Library/LaunchAgents/homebrew.mxcl.gitlab-runner.plistができます。

Terminal window
$ ls ~/Library/LaunchAgents/homebrew.mxcl.gitlab-runner.plist
/Users/<username>/Library/LaunchAgents/homebrew.mxcl.gitlab-runner.plist

空のリポジトリを作成する

https://gitlab.com/users/sign_in からGitLabにサインインします。

新規にprojectを作成します。 click-new-project-button

今回はlocal-runnerというproject名にします。 create-blank-project

Shared runnersはオフにします。後ほどregistration tokenを使用するので、この画面はそのままにしておいてください get-registration-token

gitlab-runnerのコマンド一覧

Terminal window
$ gitlab-runner -h
NAME:
gitlab-runner - a GitLab Runner
USAGE:
gitlab-runner [global options] command [command options] [arguments...]
VERSION:
15.10.1 (dcfb4b66)
AUTHOR:
GitLab Inc. <support@gitlab.com>
COMMANDS:
exec execute a build locally
list List all configured runners
run run multi runner service
register register a new runner
reset-token reset a runner's token
install install service
uninstall uninstall service
start start service
stop stop service
restart restart service
status get status of a service
run-single start single runner
unregister unregister specific runner
verify verify all registered runners
artifacts-downloader download and extract build artifacts (internal)
artifacts-uploader create and upload build artifacts (internal)
cache-archiver create and upload cache artifacts (internal)
cache-extractor download and extract cache artifacts (internal)
cache-init changed permissions for cache paths (internal)
health-check check health for a specific address
read-logs reads job logs from a file, used by kubernetes executor (internal)
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--cpuprofile value write cpu profile to file [$CPU_PROFILE]
--debug debug mode [$RUNNER_DEBUG]
--log-format value Choose log format (options: runner, text, json) [$LOG_FORMAT]
--log-level value, -l value Log level (options: debug, info, warn, error, fatal, panic) [$LOG_LEVEL]
--help, -h show help
--version, -v print the version

gitlab-runnerを登録する

gitlab-runner registerコマンドを実行します。

質問入力
Enter the GitLab instance URL (for example, https://gitlab.com/):https://gitlab.com/
Enter the registration token:空のリポジトリを作成するで確認した、registration token
Enter a description for the runner:そのままEnter
Enter tags for the runner (comma-separated):そのままEnter
Enter optional maintenance note for the runner:そのままEnter
Enter an executor: ssh, virtualbox, docker-ssh+machine, custom, docker-ssh, shell, docker+machine, instance, kubernetes, docker, parallels:shell

Runner registered successfully.が出たらGitLabのページに戻って、runnerが登録されたことを確認します。

Terminal window
$ gitlab-runner register
Runtime platform arch=arm64 os=darwin pid=19035 revision=dcfb4b66 version=15.10.1
WARNING: Running in user-mode.
WARNING: Use sudo for system-mode:
WARNING: $ sudo gitlab-runner...
Enter the GitLab instance URL (for example, https://gitlab.com/):
https://gitlab.com/
Enter the registration token:
gitlabの確認したregistration tokenをコピペする
Enter a description for the runner:
[xxxxxx.local]:
Enter tags for the runner (comma-separated):
Enter optional maintenance note for the runner:
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=1xxxxxxxxxxbGx
Enter an executor: ssh, virtualbox, docker-ssh+machine, custom, docker-ssh, shell, docker+machine, instance, kubernetes, docker, parallels:
shell
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 "/Users/<username>/.gitlab-runner/config.toml"

無事にrunnerが登録されていることが確認できました! succeed-in-registoration

gitlab-runnerのconfig.tomlを確認する

gitlab-runner registerでrunnerの登録が完了したらconfigファイルを確認してみます。

Terminal window
$ cat ~/.gitlab-runner/config.toml
concurrent = 1
check_interval = 0
shutdown_timeout = 0
[session_server]
session_timeout = 1800
[[runners]]
name = "xxxxxx.local"
url = "https://gitlab.com/"
id = 22693374
token = "xxxxxxxxxxx"
token_obtained_at = 2023-04-15T03:32:20Z
token_expires_at = 0001-01-01T00:00:00Z
executor = "shell"
[runners.cache]
MaxUploadedArchiveSize = 0

.gitlab-ci.ymlを設定する

それでは実際にgitlab runnerを動かしてみましょう。

CI/CD > Editor > Configure pipelineをクリックします。 configure-pipeline

CI/CD > Editor > Configure pipelineをクリックすると、.gitlab-ci.ymlの設定が出てるのでそのままCommit changesボタンをクリックします。 create-gitlab-ci-yml

無事に完了していますね。 passed-pipeline

ちなみにビルドはPCの~/buildsで実行されます。

Terminal window
$ ls -a ~/builds/x3FGoQ8r/0/kntks/local-runner
. .. .git .gitlab-ci.yml README.md

ローカルで実行できるコマンドをrunnerから実行する

筆者のPCでは、nodeが使える状態です。

$ node -v
v18.13.0

yamlにscriptを書いて実行してみます。

(asdf を使ってバージョン管理しているため、
. /opt/homebrew/opt/asdf/libexec/asdf.shというコマンドを入れています。)

stages:
- test
test:
stage: test
script:
- . /opt/homebrew/opt/asdf/libexec/asdf.sh
- node -v

exec-node-v

今度はbrewコマンドを実行してみます。

stages:
- test
test:
stage: test
script:
- brew help

exec-brew-help

runnerの登録を解除する

runnerの登録を解除します。

Terminal window
$ gitlab-runner list
Runtime platform arch=arm64 os=darwin pid=29667 revision=dcfb4b66 version=15.10.1
Listing configured runners ConfigFile=/Users/<username>/.gitlab-runner/config.toml
xxxxxxxxxxxx.local Executor=shell Token=xxxxxxxxxxH74ZPRk URL=https://gitlab.com/
$ gitlab-runner unregister --all-runners
Runtime platform arch=arm64 os=darwin pid=29783 revision=dcfb4b66 version=15.10.1
WARNING: Running in user-mode.
WARNING: Use sudo for system-mode:
WARNING: $ sudo gitlab-runner...
WARNING: Unregistering all runners
Unregistering runner from GitLab succeeded runner=x3FGoQ8r
Updated /Users/<username>/.gitlab-runner/config.toml
$ gitlab-runner list
Runtime platform arch=arm64 os=darwin pid=29820 revision=dcfb4b66 version=15.10.1
Listing configured runners ConfigFile=/Users/<username>/.gitlab-runner/config.toml

まとめ

Homebrewを使うことで、簡単に自分だけのCI環境を構築しました。これにより実行時間の制限を気にせず、すぐにCIの結果を受け取れるようになりました。

参考