GitLab Runnerを試したい!

とりあえず動作できる手順

注意:いつも以上に雑に書いています。

今回の動作環境は以下の通り。

  • Windows11: ホストOS
  • Docker Desktop for Windows: GitLabはコンテナ
  • GitLab Runner: Windowsで用意

正直Runnerの仕組みとか色々よく分かっていなくって特にvolumesの所が今も納得いってないです…。

以下はGitLabのComposeファイル。

メモリを8Gbにすると早くなった。
ポートを80番以外で実行したい場合には環境変数で”external_url”でと”port”で指定するポートを一致させなければならない。(以前間違えたせいでWeb画面は表示されるがHTTPSでCloneするのにポート番号だけ間違った値が表示されていたことがある。)

もし一度起動した後Composeファイルを変更したら”docker compose down”した後”docker compose up -d”しましょう。(じゃないと環境変数などが反映しません。)

version: '3.6'
services:
  gitlab:
    container_name: gitlab
    image: 'gitlab/gitlab-ce:16.8.8-ce.0'
    # restart: always
    # hostname: 'gitlab.example.com'
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        # external_url 'http://localhost:8930/gitlab'
        external_url 'http://172.31.208.1:8930/gitlab'
        gitlab_rails['initial_root_password'] = "password"
        gitlab_rails['gitlab_shell_ssh_port'] = 2200
    ports:
      # - '8930:80'
      - '8930:8930'
      - '2200:22'
    volumes:
      - './gitlab/config:/etc/gitlab'
      # - './gitlab/logs:/var/log/gitlab'
      - './gitlab/data:/var/opt/gitlab'
    deploy:
      resources:
        limits:
          memory: 8gb
    shm_size: '512m'

ここの内容はGitLabのCI/CD画面からでも確認できた。

見ての通りPowerShellで実行すること。指定しているディレクトリは任意で設定が可能。

# Create a folder somewhere on your system, for example: C:\GitLab-Runner
New-Item -Path 'C:\GitLab-Runner' -ItemType Directory

# Change to the folder
cd 'C:\GitLab-Runner'

# Download binary
Invoke-WebRequest -Uri "https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-windows-amd64.exe" -OutFile "gitlab-runner.exe"

# Register the runner (steps below), then run
.\gitlab-runner.exe install
.\gitlab-runner.exe start

Runnerに上記Composeファイルで起動させたGitLabを設定する。

トークンはCI/CD画面で表示される。実はプロジェクト、グループ、全てといった形で設定することも可能らしいが割愛。

# 対話形式で実行
./gitlab-runner.exe register

# 引数で指定することも可能
./gitlab-runner.exe register --url http://localhost:8930/gitlab/ --registration-token <GitLab_Token>

# キャシュパスのパーミッションを変更(正直やる必要ないかも。公式に乗ってないはず。)
.\gitlab-runner-windows-amd64.exe cache-init "C:\GitLab-Runner"

以下のファイルは上記の”register”実行時に生成される”config.toml”ファイルです。ファイルの場所は上記の通り実行した場合”C:\GitLab-Runner”です。
個々の設定が未だに良く分かっておらず、一先ず動作した設定を揚げています。

  • shell: 初期値は”pwsh”、他に””等試しましたがエラーになりました。
  • volumes: 初期値は”c:\\cache”だったと思いますがこちらもエラーになりました。
concurrent = 1
check_interval = 0
connection_max_age = "15m0s"
shutdown_timeout = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "CI/CD sample of GitLab!"
  url = "http://172.16.1.104:8930/gitlab"
  id = 2
  token = "71xAcP6zJ8szhfQL5VKs"
  token_obtained_at = 2025-06-04T16:49:42Z
  token_expires_at = 0001-01-01T00:00:00Z
  executor = "shell"
  shell = "powershell"
  [runners.cache]
    MaxUploadedArchiveSize = 0
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.docker]
    tls_verify = false
    image = "debian"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0
    network_mtu = 0

簡単に試したいだけなので”.gitlab-ci.yml”は以下のように適当に設定した。というかどっかに載っていたものをコピペした。

実行内容はdockerがインストールされているか簡単な確認と、Composeファイルからアプリを起動することです。

services:
    # Specify the registry mirror to use
    - name: docker:24.0.5-dind
      command: ["--registry-mirror", "https://registry-mirror.example.com"]

stages:
    - TestDockerStatus
    - setup

TestDockerStatus:
    stage: TestDockerStatus
    image: 
        name: docker:28.2.2
    script: 
        - echo "start to Test Docker status."
        - docker ps
        - docker info
        - echo "end to Test Docker status."

AppSetup:
    stage: setup
    image: 
        name: docker:28.2.2
    script:
        - echo "start setup Docker Compose!"
        - docker compose stop
        - docker compose up -d
        - echo "end setup Docker Compose!"

Runner設定のVolumesについてのエラー

“[runners.docker]”欄にあるvolumesの設定エラーです。
どうやらコンテナ内のファイルパスを指定しなきゃ動作しなかった。

このサイトを見るにどうやら「Windowsのファイルシステムを指定するにはパス抜きのドライブを指定するか、Cドライブのファイルパスしか指定できない」らしい。ただそれでもエラーになったんですけどね…。

Running with gitlab-runner 18.0.2 (4d7093e1)
  on CI/CD sample of GitLab! 71xAcP6z, system ID: s_0fea081093d4
Preparing the "docker-windows" executor
00:00
ERROR: Failed to remove network for build
ERROR: Job failed: invalid volume specification: "c:\\cache"

Runnerがリポジトリにアクセスできないってエラー

Composeファイルで指定しているexternal_urlを”localhost”にしていると発生しました。
検索するとIPアドレスを指定することで解決した旨の記事を見て、実践してみると解決しました。

ちなみにその過程で”config.toml”ファイルの”url”のlocalhostもIPアドレスに指定するように変更しました。

Created fresh repository.
fatal: unable to access 'http://localhost:8930/gitlab/docker-oss-easy-setup/knowledge.git/': Failed to connect to localhost port 8930 after 0 ms: Could not connect to server
Retrying in 5s
Cleaning up project directory and file based variables
00:01
ERROR: Job failed: exit code 128

".gitlab-ci.yml"のscript欄を実行するターミナルのエラー("pwsh"版)

恐らくは”.gitlab-ci.yml”のscript欄で指定している実行ファイルはLinuxなのに指定したターミナルが”pwsh”、つまりパワーシェルになっていたんです。そのためにエラーになっていたんじゃないかと。

ただデフォルト値で触っていなかったんですけどね。

Cleaning up project directory and file based variables
00:02
ERROR: Job failed (system failure): Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: exec: "pwsh": executable file not found in $PATH: unknown (exec.go:72:0s)

".gitlab-ci.yml"のscript欄を実行するターミナルのエラー("powershell"版)

[“.gitlab-ci.yml”のscript欄を実行するターミナルのエラー(”pwsh”版)]と発生原因は同様です。ただエラーメッセージは異なっています。

Preparing environment
00:01
Using effective pull policy of [always] for container sha256:9a7f10a5b8ce801638d3d12f79ebe8e93da1a4a3836d25461b79e8ca46deb795
/bin/bash: line 1: syntax error near unexpected token `&'
'
ERROR: Job failed: prepare environment: exit code 2. Check https://docs.gitlab.com/runner/shells/#shell-profile-loading for more information

コメントを残す