LoginSignup
4
7

Gitlab Runnerのインストールと初期設定

Posted at

GitLab Runnerとは

GitLabで定義されたCI/CDジョブを実行するもの。
インフラ上、Dockerコンテナ内、またはKubernetesクラスタへデプロイしインストールする。
Goで書かれているOSS。
GitLabインスタンスへ登録することで実行される。

Runnerの種類

共有Runner

Gitlabインスタンス内のすべてのプロジェクトで使用できる。
デフォルトでは有効になっている。セルフホストのインスタンスであれば新しいプロジェクトでも有効となる。

グループRunner

グループ内のプロジェクトで有効化できる。
FIFOキューを使用してJobを処理する。

プロジェクトRunner

特定のプロジェクトでRunnnerを利用する場合に使用する。
プロジェクトごとの設定から有効化する必要がある。

参考

https://archives.docs.gitlab.com/15.11/ee/ci/runners/runners_scope.html

今回はすべてのプロジェクトで有効化したいため、共有Runnerを設定する。

環境

Runner用EC2インスタンス:Linux /amazon linux 2023/ t3.small

大まかな手順

  1. Runners用インスタンスにDockerおよびGitlab Runnerをインストールする
  2. セルフホストのGitlabに管理者権限でログイン後Runner設定ページを開く
  3. GitlabのUIに表示されるトークンをGitlab Runnerへ登録

https://docs.gitlab.com/runner/install/docker.html

1. Runners用インスタンスにDockerおよびGitlab Runnerをインストールする

  • Dockerのインストール
    sudo dnf install -y docker

  • Dockerサービスの自動起動有効化

sudo systemctl enable --now docker

  • GitLab Runnerのインストール

https://gitlab-docs.creationline.com/runner/install/docker.html#option-1-use-local-system-volume-mounts-to-start-the-runner-container

以下を実行する。

sudo docker run -d --name gitlab-runner --restart always \
  -v /srv/gitlab-runner/config:/etc/gitlab-runner \
  -v /var/run/docker.sock:/var/run/docker.sock \
  gitlab/gitlab-runner:latest

2. セルフホストGitLabでRunner用設定ページを開く

  1. 管理者権限のアカウントでログインする。
  2. {GitLabServerIP}/admin/runners にアクセスする。
  3. トークンを取得し、控える

3. GitlabのUIに表示されるトークンをGitlab Runnerへ登録

https://gitlab-docs.creationline.com/runner/register/index.html#docker

  1. ローカルシステムのボリュームマウントに基づいてresisterコマンドを実行
    sudo docker run --rm -it -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register
  2. GitlabインスタンスのURLを入力する
    http://xxx.xx.x.xx/
  3. GitlabのUIから取得したトークンを入力
  4. ランナーの説明を入力(Gitlabから変更可能)
  5. タグを入力(スキップ)
  6. メンテナンスノートを入力(スキップ)
  7. ランナーのExecuterにDockerを入力
  8. デフォルトのdockerイメージを指定
ログ
```
[ec2-user@ip-xxx-xx-x-xxx ~]$ sudo docker run --rm -it -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=7 revision=3046fee8 version=16.6.0
Running in system-mode.

Enter the GitLab instance URL (for example, https://gitlab.com/):
http://172.31.4.15/
Enter the registration token:
-ED9omstpktCJV98bAfK
Enter a description for the runner:
[d5ce86fb472d]: gitlab runner for fsworks-prd-gitlab_server_seisanweb
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://docs.gitlab.com/ee/ci/runners/new_creation_workflow
Registering runner... succeeded                     runner=-ED9omst
Enter an executor: ssh, docker-autoscaler, docker+machine, instance, kubernetes, custom, docker-windows, parallels, docker, shell, virtualbox:
docker
Enter the default Docker image (for example, ruby:2.7):

Enter the default Docker image (for example, ruby:2.7):
php:8.2-cli
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"
```

構築が完了できているか確認

GitLab管理者エリア(http://{GitLabServerIP}/admin/runners)で確認

rapture_20231121152142.jpg

プロジェクトで利用できるか確認

新規ブランチを作成し.gitlab-ci.ymlをプロジェクトルートに作成

mainへのMRを作成し左サイドバーの「CICD」から実行されていることを確認。

rapture_20231122080106.jpg

4
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
7