0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ubuntu環境でGitLab Pagesの設定メモ

Posted at

前提条件

すでにGitLab Runnerが設定されていることを前提としています。
GitLab Runnerの設定メモは↓にあります。

やることリスト

  1. ドメインの準備
  2. 証明書の準備
  3. GitLab設定の変更

ドメインの準備

私はAWSのEC2上に作成していて、ドメインはRoute53で管理しています。
まず、Route53でEC2のグローバルIPに対して以下の2つレコード(仮のドメインです)を追加します。

  • pages.example.com
  • *.pages.example.com

ちなみに、GitLab本体はgitlab.example.com(仮の...)で設定しています。

証明書の準備

Let's Encryptの無料証明書を利用していますが、これはLegoという証明書の発行・更新するツールを使って管理しています。

sudo lego --accept-tos \
    --email="hogehoge@example.com" \
    --domains="pages.example.com" \
    --domains="*.pages.example.com" \
    -dns="route53" \
    --path="/opt/lego/letsencrypt" \
    run

ちなみにGitLab自体はIP制限をしているので、Let's Encryptからの確認はRoute53によるドメインでの認証にしています。

作成した証明書はGitLab側が参照するディレクトリにシンボリックリンクを作成します。
※パスは適宜調整してもらえればと。

sudo ln -sf /opt/lego/letsencrypt/certificates/pages.example.com.key /etc/gitlab/ssl/pages.example.com.key
sudo ln -sf /opt/lego/letsencrypt/certificates/pages.example.com.crt /etc/gitlab/ssl/pages.example.com.crt

※補足と言うかGitLabの設定でPagesの証明書のパスを指定できるけど、デフォルトがドメイン名になっているので名前を変更する際は設定ファイルで変更してください。

GitLab設定の変更

GitLabの設定ファイルを編集してPagesの設定を有効化します

sudo vi /etc/gitlab/gitlab.rb

↓ 設定する項目

pages_external_url "https://pages.example.com/"
gitlab_pages['enable'] = true
gitlab_pages['redirect_http'] = true
gitlab_pages['dir'] = "/var/opt/gitlab/gitlab-pages"
gitlab_pages['log_directory'] = "/var/log/gitlab/gitlab-pages"

※証明書のパスを変更する場合はgitlab_pages['cert']gitlab_pages['cert_key']も変更が必要です

設定の反映 ※2分ぐらいかかった

sudo gitlab-ctl reconfigure

ステータス確認

sudo gitlab-ctl status

ログ確認

sudo gitlab-ctl tail

GitLab Pagesの動作確認

適当に空のプロジェクトを作成して、publicディレクトリにindex.htmlを作成する.gitlab-ci.ymlをプロジェクトのルートに配置してプッシュすると、パイプラインが実行されてhtmlが生成されます。

gitlab-ci.yml
stages:
  - deploy

pages:
  stage: deploy
  script:
    - mkdir -p public
    - echo "<h1>Hello GitLab Pages!</h1>" > public/index.html
  artifacts:
    paths:
      - public

メニュー→デプロイ→Pagesから用意されているURLのリンクをクリックすると画面が確認できます。

↓こんな感じで表示されていれば完成で

image.png

ついでに

証明書が期限切れそうになったら自動で更新するシェルを作成してcronで適当にチェックしておくようにしましょう。

#!/bin/bash

# pages: 30日未満の場合は更新実行
/usr/local/bin/lego --accept-tos \
        --email="hogehoge@example.com" \
        --domains="pages.example.com" \
        --domains="*.pages.example.com" \
        --dns="route53" \
        --path="/opt/lego/letsencrypt" \
        renew  \
        --days 30 \
        --renew-hook "gitlab-ctl restart nginx && gitlab-ctl reconfigure"
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?