はじめに

前回の記事 で作成したGitLabの環境を使用する。
GitLab-runnerはCI/CDジョブを実行するエージェントのため、入れることでGitLabの機能が拡張される。

環境

1
2
3
4
Windows 10 Professional
WSL2 (Ubuntu22.04 LTS)
docker version 23.0.5
Docker Compose version v2.17.3

構築

compose.ymlの変更

前回作成した GitLabの compose.ymlを下記のようにする。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
services:
  gitlab:
    build: ./gitlab
    hostname: 'gitlab.example.com'
    environment:
      GITLAB_OMNIBUS_CONFIG:
        external_url 'http://gitlab.example.com/'
    volumes:
      - '$GITLAB_HOME/config:/etc/gitlab'
      - '$GITLAB_HOME/logs:/var/log/gitlab'
      - '$GITLAB_HOME/data:/var/opt/gitlab'
    ports:
      - '80:80'
      - '443:443'
      - '22:22'
    shm_size: '256m'
  runner:
    image: 'gitlab/gitlab-runner:latest'
    restart: always
    volumes:
      - ./srv/gitlab-runner/config:/etc/gitlab-runner
      - /var/run/docker.sock:/var/run/docker/sock
    ports:
      - "8093:8093"

前回との差分はここ

1
2
3
4
5
6
7
8
+  runner:
+    image: 'gitlab/gitlab-runner:latest'
+    restart: always
+    volumes:
+      - ./srv/gitlab-runner/config:/etc/gitlab-runner
+      - /var/run/docker.sock:/var/run/docker/sock
+    ports:
+      - "8093:8093"

ビルド+構築

下記を実行し、起動を行う。

1
docker compose up -d

Gitlab-runnerを使用するための準備

Gitlabへのログイン

前回作成したGitLabの環境の root ユーザのパスワードを忘れてしまったので、下記でパスワードを変更する。 https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password

GitLabコンテナへログインする。

1
docker compose exec gitlab bash

コンテナ内で、 gitlab-rake "gitlab:password:reset" を実行する。

1
root@gitlab:/# gitlab-rake "gitlab:password:reset"

ユーザ名を聞かれるので、 root と入力し、その後のパスワードを入力する。

1
2
3
4
Enter username: root
Enter password: 
Confirm password: 
Password successfully updated for user with username root.

これでパスワードリセットができたのでログインをする。

GitLabのプロジェクトにRunnerを登録する

トークンの取得

  1. 今回新しく test-runner プロジェクトを作成した。
    gitlab-runner-1

  2. 対象のプロジェクトの CI/CD のページへ遷移する。
    gitlab-runner-2

  3. RunnersExpand を押し、Specific runnersに表示されているURL と registration tokenを控える。 gitlab-runner-3

Gitlab-runner への登録

  1. gitlab-runnerコンテナへ入る。
1
docker compose exec runner bash
  1. gitlab-runner register を実行する
1
root@1ce083816d04:/# gitlab-runner register
  1. URLとtokenを入力する。
1
2
3
4
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://gitlab.example.com/
Enter the registration token:
GR1348941fAzV2AyrqiCG5NWKPz7t
  1. あとは適当に
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
Enter a description for the runner:
[1ce083816d04]: 
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=GR1348941fAzV2Ayr
Enter an executor: ssh, docker+machine, kubernetes, docker, docker-windows, docker-ssh, parallels, shell, custom, virtualbox, docker-autoscaler, docker-ssh+machine, instance:
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 "/etc/gitlab-runner/config.toml" 
  1. 登録が完了したら、 Gitlab-runnerを起動する。
1
gitlab-runner run
gitlab-runner-4

先程の Runners のページで上記のように追加されていればOK.

Gitlab-runnerを使う

.gitlab-ci.ymlの作成

  1. プロジェクトの CI/CD から Editor を押し、 Configure Pipeline を押す。 gitlab-runner-use-1

  2. 下記のように記載してコミットする。

1
2
3
build:
  script:
    - echo hello
gitlab-runner-use-2
  1. Jobs を確認してみる。 gitlab-runner-use-3 gitlab-runner-use-4

なんか良さそうだ…!

これでテストを実施するコマンドやら、ビルドが通るかのコマンドを入れておくと良いのかな~。

参考

おわりに

Gitlab-runner の登録の方法を学びたかったので調べた。
殆ど、GitLabのブログと同じ内容だが、写経という意味で書いた。
自分のプロジェクトは殆どGitHubなのでGitLabは使っていないのだが、業務ではGitLabを使っているので実践できそう。