4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

オンプレ版GitLabでGitLab Pagesを実行するまで

Last updated at Posted at 2023-07-30

概要

最近オンプレ版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のテンプレートを使用するをクリックして必要事項を記入してプロジェクトの作成をクリックする。
  • パイプラインの実行
    • プロジェクトのサイドバーにあるビルド項目のパイプラインをクリックする。
    • 右上のパイプラインの実行ボタンをクリックし、次の画面のパイプラインの実行ボタンをクリックする。
    • パイプラインが実行されるのでしばらく待つ。
    • 成功が表示されたら終了。
  • ページにアクセス
    • プロジェクトのサイドバーにあるデプロイ項目のPagesクリックする。
    • 表示されたURLをクリックしHello World!と表示された画面にアクセスできたら正常に動作した事がわかる。
4
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?