概要
最近オンプレ版GitLabを触る機会があったため、インストールからGitLab Pagesの導入までを自分用にメモする。
目標
オンプレ版GitLabを導入して、GitLab Pagesのサンプルプログラムを動かす。
自PC上で動かすことだけを考慮する。外部からのアクセスは考慮しない。
参考
環境
- Ubuntu 22.04
- GitLab v16.2.0-ee
- GitLab Runner 16.2.0
- Docker version 24.0.4
事前準備
- ホスト名とIPアドレスを関連付ける
gitlabはデフォルトではhttp://gitlab.example.com/というホスト名でアクセスするようになっている。
そのためホスト名とIPアドレスの関連付けをしておかないとアクセスできないのでやっておく。
# 以下のコマンドでhostsファイルを開く
sudo gedit /etc/hosts
# 以下のフォーマットに従い、IPアドレスとホスト名を関連付ける
# IPアドレス ホスト名
# 例 xxx.xxx.xxx.xxx gitlab.example.com
# IPアドレスはip aコマンドで調べることができる。
- 環境変数の設定
コンテナーが参照する変数を~/.bashrc
などに追加しておく。
export GITLAB_HOME=/srv/gitlab
Dockerを用いたインストール
- GitLab本体
参考:https://docs.gitlab.com/ee/install/docker.html
# docker run実行後、http://gitlab.example.com/にアクセスしてログイン画面が表示されるまで待つ。
sudo docker run --detach --hostname gitlab.example.com --publish 443:443 --publish 80:80 --publish 22:22 --name gitlab --restart always --volume $GITLAB_HOME/config:/etc/gitlab --volume $GITLAB_HOME/logs:/var/log/gitlab --volume $GITLAB_HOME/data:/var/opt/gitlab --shm-size 256m gitlab/gitlab-ee:latest
702 docker exec -it gitlab bash
# ログイン画面表示後、ユーザー名rootでパスワードは以下のコマンドで表示されるものでログインする。
docker exec -it gitlab grep 'Password:' /etc/gitlab/initial_root_password
# 必要に応じてパスワードは変更しておく。
- GitLab Runner
参考:https://docs.gitlab.com/runner/install/
# Gitlab RunnerコンテナーからGitlabコンテナーが見れる用に--add-hostオプションを追加。追加する内容は/etc/hostsと同じ
sudo docker run -d --name gitlab-runner --restart always --add-host gitlab.example.com:xxx.xxx.xxx.xxx -v /srv/gitlab-runner/config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:latest
Runnerの作成
rootユーザーでログインし管理エリアのダッシュボードへ行く。
機能の欄にある共有Runner
をクリックする。
右上にある新規新スタンスRunner
をクリックし、プラットフォームはLinux
のまま、Tagsにshared
を記入し、タグのないジョブの実行にチェックを入れる。
その他は必須ではないため、最後にランナーの作成をクリックする。
Runner登録画面に遷移したら、ステップ1のコマンドをコピーしてGitLab Runnerコンテナー内で実行する。
実行すると以下のような画面が出て入力を促されるため、必須事項を入力していく。
root@16fd588baacb:~# gitlab-runner register --url http://gitlab.example.com --token glrt-N2hNc5_nxH6VPaPhADs4
Runtime platform arch=amd64 os=linux pid=1692 revision=782e15da version=16.2.0
Running in system-mode.
There might be a problem with your config based on jsonschema annotations in common/config.go (experimental feature):
jsonschema: '/runners/0/docker/ulimit' does not validate with https://gitlab.com/gitlab-org/gitlab-runner/common/config#/$ref/properties/runners/items/$ref/properties/docker/$ref/properties/ulimit/type: expected object, but got null
Enter the GitLab instance URL (for example, https://gitlab.com/):
[http://gitlab.example.com]: ※入力は済んでいるのでEnterキーでスキップ
Verifying runner... is valid runner=N2hNc5_nx
Enter a name for the runner. This is stored only in the local config.toml file:
[16fd588baacb]: ※入力は済んでいるのでEnterキーでスキップ
Enter an executor: parallels, virtualbox, docker-autoscaler, docker+machine, instance, kubernetes, custom, docker, docker-windows, shell, ssh:
docker ※パイプラインはdockerコンテンテナー上で動かしたいのでdockerと入力
Enter the default Docker image (for example, ruby:2.7):
ruby:2.7 ※デフォルトDocker imageはとりあえずruby:2.7と入力
Runner registered successfully. Feel free to start it, but if its running already the config should be automatically reloaded!
Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"
root@16fd588baacb:~#
Runnerの一覧画面に戻り、オンライン状態で待機中のRunnerが生成されていたら完了。
Runnerのconfigファイル修正
Runnerで動作するDockerコンテナーがホスト名gitlab.example.com
を名前解決できるようにextra_hosts
を追加する。
configファイルはローカルPC上の/srv/gitlab-runner/config/config.toml
を修正すればコンテナーへ自動で反映される。
concurrent = 1
check_interval = 0
shutdown_timeout = 0
[session_server]
session_timeout = 1800
[[runners]]
name = "16fd588baacb"
url = "http://gitlab.example.com"
...
executor = "docker"
[runners.cache]
MaxUploadedArchiveSize = 0
[runners.docker]
tls_verify = false
image = "ruby:2.7"
privileged = false
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/cache"]
shm_size = 0
extra_hosts = ["gitlab.example.com:xxx.xxx.xxx.xxx"]
GitLab Pages
Pagesの有効化
デフォルトではPagesは無効化されているため、GitLabの設定ファイル修正して有効にする。
ローカルPC上で/srv/gitlab/config/gitlab.rb
を以下のように修正し、GitLabコンテナー内でgitlab-ctl reconfigure
を実行する。
################################################################################
## GitLab Pages
##! Docs: https://docs.gitlab.com/ee/administration/pages/
################################################################################
##! Define to enable GitLab Pages
pages_external_url "http://pages.example.com/"
gitlab_pages['enable'] = true
gitlab_pages['dir'] = "/var/opt/gitlab/gitlab-pages"
gitlab_pages['log_directory'] = "/var/log/gitlab/gitlab-pages"
GitLabにrootユーザーでサインインし、管理者エリアの機能欄にあるGitLa Pagesにチェックマークがついていれば有効化完了。
Pagesの名前解決
これまでの設定でPagesで生成されたページはhttp://{ユーザー名}.pages.example.com
で公開される用になっている。
このホスト名にアクセスできるよう事前準備と同様にhostsファイルに追加する。
# 以下のコマンドでhostsファイルを開く
sudo gedit /etc/hosts
# 以下のフォーマットに従い、IPアドレスとホスト名を関連付ける
# IPアドレス ホスト名
# 例 xxx.xxx.xxx.xxx {ユーザー名}.pages.example.com
Pagesの実行
GitLabが標準で用意してくれているPagesのテンプレートを動かしてみる。
- 新規プロジェクトの作成
- 新規プロジェクトの作成kから
テンプレートからの作成
を選択する。 -
Pages/Plain HTML
のテンプレートを使用するをクリックして必要事項を記入してプロジェクトの作成をクリックする。
- 新規プロジェクトの作成kから
- パイプラインの実行
- プロジェクトのサイドバーにある
ビルド
項目のパイプライン
をクリックする。 - 右上の
パイプラインの実行
ボタンをクリックし、次の画面のパイプラインの実行
ボタンをクリックする。 - パイプラインが実行されるのでしばらく待つ。
- 成功が表示されたら終了。
- プロジェクトのサイドバーにある
- ページにアクセス
- プロジェクトのサイドバーにある
デプロイ
項目のPages
クリックする。 - 表示されたURLをクリックし
Hello World!
と表示された画面にアクセスできたら正常に動作した事がわかる。
- プロジェクトのサイドバーにある