Docker
GitLab
public
GitLab Runner を使う
ここに載せるのは Linux 実環境上での Docker で GitLab Runner を動作させる方法である。
用語
ジョブ
.gitlab-ci.yml内の作業単位を指す。以下のjob_testはジョブである。
.gitlab-ci.yml 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 stages:
- test
- build
- deploy
job_test:
stage: test
script:
- npm ci
- npm run test
job_build:
stage: build
script:
- npm ci
- npm run build
job_deploy:
stage: build
script:
- npm ci
- npm run build
ステージ
ジョブをまとめたもの。
パイプライン
複数のステージをまとめたもの。.gitlab-ci.ymlはパイプラインといえる。
docker-compose.yml
docker-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
25
26 services:
gitlab:
image: gitlab/gitlab-ce:latest
container_name: gitlab
restart: always
ports:
- 8082:80
- 4432:443
- 2202:22
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url "http://xxx.xxx.xxx.xxx:8082"
nginx["listen_port"] = 80
volumes:
- ./gitlab/config:/etc/gitlab
- ./gitlab/logs:/var/log/gitlab
- ./gitlab/data:/var/opt/gitlab
gitlab-runner:
image: gitlab/gitlab-runner:latest
container_name: gitlab-runner
restart: always
volumes:
- ./gitlab-runner/config:/etc/gitlab-runner
- /var/run/docker.sock:/var/run/docker.sock
depends_on:
- gitlab
$ sudo mkdir -p ./gitlab-runner/config
$ sudo touch ./gitlab-runner/config/config.toml
$ docker compose up
root の初期パスワードを確認する。
$ docker compose exec gitlab bash -c "cat /etc/gitlab/initial_root_password"
# WARNING: This value is valid only in the following conditions
# 1. If provided manually (either via `GITLAB_ROOT_PASSWORD` environment variable or via `gitlab_rails['initial_root_password']` setting in `gitlab.rb`, it was provided before database was seeded for the first time (usually, the first reconfigure run).
# 2. Password hasn't been changed manually, either via UI or via command line.
#
# If the password shown here doesn't work, you must reset the admin password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password.
Password: { ここにrootのパスワード}
# NOTE: This file will be automatically deleted in the first reconfigure run after 24 hours.
root ユーザーでログインしたら、ユーザーを作成する。
ユーザーを作成したら再度ユーザーの編集画面に移動し、一時的なパスワードを設定する。
作成したユーザーでログインし直し、プロジェクトを作成する。
プロジェクトを clone する。
$ git clone http://xxx.xxx.xxx.xxx:8082/user/project.git
次のファイルをコミットする。
.gitlab-ci.yml 1
2
3
4
5
6
7
8
9
10
11
12
13 stages:
- build
- test
job_build:
stage: build
script:
- echo "Building the project"
job_test:
stage: test
script:
- echo "Running tests"
Settings-CI/CDを選択する。
RunnersのProject runnersにあるCreate project runnerを選択する。
Run untagged jobsにチェックを入れて作成する。
画面が変わり、トークンが表示されるのでコピーする。
トークンを使って GitLab と GitLab Runner を結びつける。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 $ docker run --rm -it -v ./gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register
Runtime platform arch = amd64 os = linux pid = 7 revision = 0731d300 version = 18 .1.0
Running in system-mode.
Enter the GitLab instance URL ( for example, https://gitlab.com/) :
http://xxx.xxx.xxx.xxx:8082/
Enter the registration token:
glrt-dp01e4_fr5JMl7FS_gh6L286MQpwOjEKdDozCnU6Mg8.01.171mmlpm3
Verifying runner... is valid correlation_id = 01K2PCBXGZDQ830GD0R5SN8ZAP runner = dp01e4_fr
Enter a name for the runner. This is stored only in the local config.toml file:
[ 767f9a0285db] :
Enter an executor: instance, ssh, parallels, docker, kubernetes, custom, shell, virtualbox, docker-windows, docker+machine, docker-autoscaler:
docker
Enter the default Docker image ( for example, ruby:2.7) :
ubuntu:24.04
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"
Build-Jobsに移動して、ジョブの実行に成功していることを確認する。
Vitest を使う
GitLab でプロジェクトを作成する。(vitest-exampleとする)
クローンしてコードを書く。
$ git clone http://xxx.xxx.xxx.xxx:8082/user/vitest-example.git
$ cd vitest-example
$ npm init -y
$ npm i -D typescript tsx @types/node vitest
$ npx tsc --init
次のディレクトリ構成にする。
.
│ .gitignore
│ .gitlab-ci.yml
│ package-lock.json
│ package.json
│ tsconfig.json
│
├─node_modules
└─src
app.test.ts
app.ts
src/app.ts export function sum ( a : number , b : number ) : number {
return a + b ;
}
src/app.test.ts import { describe , expect , test } from "vitest" ;
describe ( "describe" , () => {
test ( "test" , () => {
expect ( 1 + 1 ). toBe ( 2 );
});
});
package.jsonに以下をマージする。
package.json {
"scripts" : {
"dev" : "tsx watch src/app.ts" ,
"test" : "vitest"
},
"type" : "module"
}
.gitlab-ci.yml 1
2
3
4
5
6
7
8
9
10
11
12
13
14 stages:
- build
- test
job_build:
stage: build
script:
- echo "Building the project"
job_test:
stage: test
script:
- npm ci
- npm run test
node:22.14.0-bookwormをベースにした Runner を登録する。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 $ docker run --rm -it -v ./gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register
Runtime platform arch = amd64 os = linux pid = 7 revision = 0731d300 version = 18 .1.0
Running in system-mode.
Enter the GitLab instance URL ( for example, https://gitlab.com/) :
http://xxx.xxx.xxx.xxx:8082/
Enter the registration token:
glrt-bZZjomd5nRAcyp1tDLhQqW86MQpwOjIKdDozCnU6Mg8.01.171kzk6v1
Verifying runner... is valid correlation_id = 01K2PFH7SBVH563TVPSZFAA2DM runner = bZZjomd5n
Enter a name for the runner. This is stored only in the local config.toml file:
[ 8a44ecca182c] :
Enter an executor: shell, docker-windows, docker+machine, docker-autoscaler, custom, ssh, parallels, virtualbox, docker, kubernetes, instance:
docker
Enter the default Docker image ( for example, ruby:2.7) :
node:22.14.0-bookworm
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"
テストが実行されていることを確認する。
環境変数を使う
テストコードで環境変数を読み込んでみる。
src/app.test.ts import { describe , expect , test } from "vitest" ;
import { sum } from "./app.js" ;
console . log ( "ENDPOINT_URL" , process . env . ENDPOINT_URL );
describe ( "describe" , () => {
test ( "test" , () => {
expect ( sum ( 1 , 1 )). toBe ( 2 );
});
});
Settings-CI/CDにあるVariablesで環境変数を設定することができる。
テストを再実行し、環境変数が読み込まれていることを確認する。
GitLab Runner インスタンスを削除する
# Runnerの一覧を確認する
$ docker run --rm -it -v ./gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner list
# Runnerを削除する
$ docker run --rm -it -v ./gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner unregister --name test-runner
上記のコマンドを実行すると GitLab Runner 上から消えるが GitLab 上の Runner の一覧からは消えない。
さらにhttp://xxx.xxx.xxx.xxx:xxxx/admin/runnersにアクセスして Runner を削除する。
手動でジョブを実行する
.gitlab-ci.yml stages:
- test
- build
- deploy
job_manual:
stage: deploy
script:
- echo "Manual job"
when: manual
allow_failure: true
アーティファクトを使う
.gitlab-ci.yml 1
2
3
4
5
6
7
8
9
10
11
12
13 stages:
- test
- build
- deploy
job_test:
stage: test
script:
- npm ci
- npm run test > report.txt
artifacts:
paths:
- report.txt