0
0

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 3 years have passed since last update.

GitLab Pagesと本番サイトの双方にJekyllで生成されたページをデプロイする

0
Last updated at Posted at 2023-02-02

GitLab CI/CD を使って、GitLab.com 上で管理しているリポジトリから Jekyll を使って静的なサイトを構築し、
ドラフト版(master以外)は GitLab Pages で、
本番(master)は外部のサイト (FTPS接続を受け付けている) で公開するという運用をしています。

この場合に注意することをメモします。

ポイント

  1. GitLab Pages で公開される URL はプロジェクト名から自動生成されるため、本番サイトとルートの場所が違う構成になる場合がある。その場合はbaseurl 設定をコマンドライン引数(例: --baseurl "/")により変更してビルドを分ければ良い。コマンドライン引数による設定は、_config.yml による設定よりも優先される。
  2. とりあえず、Docker Image としては、Ruby 公式を使えば良さそう。この Docker Image では apt が使えるので、必要なライブラリなどは apt-get でインストールできる。
  3. 本番サイトへのアップロード手段としてFTP, FTPS, SFTPなどが使えるのであれば、lftp を使うのが比較的簡単。このパッケージはデフォルトでは入っていないので、CIのレシピ中でインストールする。
  4. 毎回、Bundler を再構成していると無駄が多いので、キャッシュを使うと良い
    参考: Create a GitLab Pages website from scratch
  5. .gitlab-ci.yml に直接FTPパスワードを書くのはセキュリティ上推奨できないので、環境変数を使うと良さそう。
    参考: GitLab CI: Deploy to FTP / SFTP with lftp

TODO: インクリメンタルビルドを使ってJekyllのサイト構築の時間を減らせる可能性があるが、キャッシュを使っても今の所想定通りの動作にならない。

使っている .gitlab-ci.yml の内容

image: ruby:latest

variables:
  JEKYLL_ENV: production
  LC_ALL: C.UTF-8
  # DST_DIR is used to set root directory for HTTP(S) service.
  DST_ROOT: www/jk
  # DST_DIR is used as base-url, it might be '/'.
  DST_DIR: /

cache:
  paths:
    - vendor/
    - dist/
    - .jekyll-metadata

before_script:
  - bundle install --path vendor
  - apt-get update -qy
  - apt-get install -y git-restore-mtime  

pages:
  stage: deploy
  script:
    - /usr/lib/git-core/git-restore-mtime
    - bundle exec jekyll build -I -d public
  artifacts:
    paths:
    - public
  except:
  - master

build_pages:
  stage: build
  script:
    #- ls -l dist/
    - /usr/lib/git-core/git-restore-mtime
    - bundle exec jekyll build -V -I -d dist --baseurl "$DST_DIR"
    #- ls -l dist/
  only:
  - master

upload:
  stage: deploy
  script:
    - date
    #- ls -l dist/
    - apt-get install -y lftp
    - lftp -e "set ftp:ssl-allow yes; open $FTP_SERVER; user $FTP_USERNAME $FTP_PASSWORD; mirror -X .* -X .*/ --reverse --only-newer --verbose dist/ $DST_ROOT$DST_DIR; bye"
  only:
  - master
  • master 以外のブランチでは Job pages で GitLab Pages 向けのデータをビルドします。
  • master ブランチでは Job build_pages でデータの生成を行い、 upload で本番環境にアップロードしています。
  • 対象のサーバが FTPS に対応していない場合は、 set ftp:ssl-allow no に変更してご利用下さい。
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?