Homebrewでgitlab-runnerをインストール、ローカル環境でGitLab CIを実行する
はじめに
GitLab CIの動作検証のため何回もコミットしていると以下の問題が発生すると思います。
- コミットが汚れる
- runnerの待ち時間が長く、素早く改善できない
- gitlabのプランによっては使用時間に制限があり、気軽に試せない
1のコミットに関しては、最悪mainにマージするタイミングでsquashすれば良いですが 2,3番目は開発に影響を与えかねません。
そのためこの記事ではGitLab Runner構築のススメ - GitLab blogを参考に runnerのみをローカル環境で実行し、気軽に検証できる環境を構築します。
環境
バージョン | |
---|---|
MacBook Pro M1 | Ventura 13.2.1 |
Homebrew | 4.0.13 |
GitLab | Enterprise Edition 15.11.0-pre |
Gitlab Runnerをインストールする
検証端末はMacBookなので、Homebrewでインストールします。
$ 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
ができます。
$ 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を作成します。
今回はlocal-runner
というproject名にします。
Shared runnersはオフにします。後ほどregistration tokenを使用するので、この画面はそのままにしておいてください
gitlab-runnerのコマンド一覧
$ gitlab-runner -hNAME: 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が登録されたことを確認します。
$ gitlab-runner registerRuntime platform arch=arm64 os=darwin pid=19035 revision=dcfb4b66 version=15.10.1WARNING: 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/380872Registering runner... succeeded runner=1xxxxxxxxxxbGxEnter an executor: ssh, virtualbox, docker-ssh+machine, custom, docker-ssh, shell, docker+machine, instance, kubernetes, docker, parallels:shellRunner 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が登録されていることが確認できました!
gitlab-runnerのconfig.tomlを確認する
gitlab-runner register
でrunnerの登録が完了したらconfigファイルを確認してみます。
$ cat ~/.gitlab-runner/config.tomlconcurrent = 1check_interval = 0shutdown_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
をクリックします。
CI/CD > Editor > Configure pipeline
をクリックすると、.gitlab-ci.yml
の設定が出てるのでそのままCommit changes
ボタンをクリックします。
無事に完了していますね。
ちなみにビルドはPCの~/builds
で実行されます。
$ ls -a ~/builds/x3FGoQ8r/0/kntks/local-runner. .. .git .gitlab-ci.yml README.md
ローカルで実行できるコマンドをrunnerから実行する
筆者のPCでは、nodeが使える状態です。
$ node -vv18.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
今度はbrewコマンドを実行してみます。
stages: - test
test: stage: test script: - brew help
runnerの登録を解除する
runnerの登録を解除します。
$ gitlab-runner listRuntime platform arch=arm64 os=darwin pid=29667 revision=dcfb4b66 version=15.10.1Listing configured runners ConfigFile=/Users/<username>/.gitlab-runner/config.tomlxxxxxxxxxxxx.local Executor=shell Token=xxxxxxxxxxH74ZPRk URL=https://gitlab.com/
$ gitlab-runner unregister --all-runnersRuntime platform arch=arm64 os=darwin pid=29783 revision=dcfb4b66 version=15.10.1WARNING: Running in user-mode.WARNING: Use sudo for system-mode:WARNING: $ sudo gitlab-runner...
WARNING: Unregistering all runnersUnregistering runner from GitLab succeeded runner=x3FGoQ8rUpdated /Users/<username>/.gitlab-runner/config.toml
$ gitlab-runner listRuntime platform arch=arm64 os=darwin pid=29820 revision=dcfb4b66 version=15.10.1Listing configured runners ConfigFile=/Users/<username>/.gitlab-runner/config.toml
まとめ
Homebrewを使うことで、簡単に自分だけのCI環境を構築しました。これにより実行時間の制限を気にせず、すぐにCIの結果を受け取れるようになりました。