zdogma's diary

徒然なるエンジニアな日々。

Github Pages + Jekyll でハマったこと

はじまりは Processing の成果物を蓄積する場所としてのシンプルな Github Pages を想定していたが、 Github Pages が Jekyll を推奨していたこともあり、久々に触ってみることにした。

そうしたら思いの外ハマってしまったので、そのログを残しておく。

問題1: _layouts が見つからない

概要

いつもの調子で下記のように打ってみたのだが、

bundle install --path=vendor/bundle --jobs=4
bundle exec jekyll new .

下記のようなエラーが出てビルドできない。

$ bundle exec jekyll build
Configuration file: /Users/tomohiro.zoda/git/github.com/zdogma/zdogma.github.io/_config.yml
            Source: /Users/tomohiro.zoda/git/github.com/zdogma/zdogma.github.io
       Destination: /Users/tomohiro.zoda/git/github.com/zdogma/zdogma.github.io/_site
 Incremental build: disabled. Enable with --incremental
      Generating...
     Build Warning: Layout 'post' requested in _posts/2016-08-07-welcome-to-jekyll.markdown does not exist.
  Liquid Exception: The included file '/Users/tomohiro.zoda/git/github.com/zdogma/zdogma.github.io/_includes/icon-github.html' should exist and should not be a symlink in about.md

jekyll new した時に作られるべき _layouts が存在しない...? 前に jekyll 使った時はこんなところでハマらなかったような...泣

解決方法

Github Pages 側の公式ドキュメントに導入方法があったので読んでみる。(最初から読んでおけという話)
Setting up your GitHub Pages site locally with Jekyll - User Documentation

bundle exec jekyll new . --force
New jekyll site installed in /Users/octocat/my-site.

force オプションが必要だったらしい。
すでに存在するディレクトリに jekyll の初期設定を展開する時には必須なのだそうだ。

If the existing directory isn’t empty, you’ll also have to pass the --force option like so jekyll new . --force.

jekyllrb.com

問題2: Invalid date でフォーマットエラー

概要

またもビルドで落ちる。

$ bundle exec jekyll serve
Configuration file: /Users/tomohiro.zoda/git/github.com/zdogma/zdogma.github.io/_config.yml
            Source: /Users/tomohiro.zoda/git/github.com/zdogma/zdogma.github.io
       Destination: /Users/tomohiro.zoda/git/github.com/zdogma/zdogma.github.io/_site
 Incremental build: disabled. Enable with --incremental
      Generating...
             ERROR: YOUR SITE COULD NOT BE BUILT:
                    ------------------------------------
                    Invalid date '<%= Time.now.strftime('%Y-%m-%d %H:%M:%S %z') %>': Document 'vendor/bundle/ruby/2.3.0/gems/jekyll-3.1.6/lib/site_template/_posts/0000-00-00-welcome-to-jekyll.markdown.erb' does not have a valid date in the YAML front matter.

なにか余計なものを見に行って落ちているようで、これはすぐに解決策が浮かんだ。

解決方法

_config.yml に下記を追記する。

exclude: [vendor]

vendor/bundle 以下に gem を入れている場合、ビルド時にそこを読まないように除外してやる必要がある。 除外含めた _config.yml の書き方は下記に詳しい。

Exclude Exclude directories and/or files from the conversion. These exclusions are relative to the site's source directory and cannot be outside the source directory.

jekyllrb.com

ビルド成功

なんとか上記を踏まえて、ビルドできるようになった。

$ bundle exec jekyll serve --watch
Configuration file: /Users/tomohiro.zoda/git/github.com/zdogma/zdogma.github.io/_config.yml
            Source: /Users/tomohiro.zoda/git/github.com/zdogma/zdogma.github.io
       Destination: /Users/tomohiro.zoda/git/github.com/zdogma/zdogma.github.io/_site
 Incremental build: disabled. Enable with --incremental
      Generating...
                    done in 0.273 seconds.
 Auto-regeneration: enabled for '/Users/tomohiro.zoda/git/github.com/zdogma/zdogma.github.io'
Configuration file: /Users/tomohiro.zoda/git/github.com/zdogma/zdogma.github.io/_config.yml
    Server address: http://127.0.0.1:4000/
  Server running... press ctrl-c to stop.

これでこちらの Github Pages も使えるようになったが、こちらでも記事を書くとこのはてなダイアリーと重複するため、 基本的に Github Pages は Processing など動くものを載せるところにしようと思う。