大して書いていなかったブログをgithub pages+hugoの組み合わせに移行したのでそのメモ。
とは言うものの、ほとんどHugoの入門用ページの内容をそのままやっただけなのでその辺は割愛。
最終的なディレクトリ構成は以下。
blog
 ├── Makefile
 ├── archetypes
 │   ├── default.md
 │   └── posts.md
 ├── bin
 │   ├── deploy.sh
 │   └── new_post.sh
 ├── config.toml
 ├── content
 │   ├── about
 │   │   └── index.md
 │   └── posts
 │       └── 2018-09-15-hugo-github-pages
 │           └── index.md
 ├── public(submodule)
 ├── static
 │   ├── css
 │   │   └── index.css
 │   └── images
 │       └── avatar.jpg
 └── themes
     └── coder(submodule)
config.toml Link to heading
自分はパーマリンクを日付+自分で設定した英単語にしたかったので、以下のようにconfig.tomlに追記。
slugは各postのfrontmatter(postの先頭のヘッダ部分)に設定すると反映される。
posts = "/posts/:year/:month/:day/:slug"のpostsの部分はcontentディレクトリにあるpostsディレクトリと同じ名前にする必要があるので注意。
[permalinks]
  posts = "/posts/:year/:month/:day/:slug"
Makefile Link to heading
makeコマンドだけでいろいろな作業を完結させたかったので以下のようにMakefileを作成。
# new post title
title=
init:
	@git submodule update --init
update:
	@git submodule foreach git checkout master
	@git submodule foreach git pull
post:
	@./bin/new_post.sh ${title}
server: init
	@hugo server --buildDrafts
server_prod: init
	@hugo server
pull:
	@git pull
	@cd public; git checkout master; git pull;
build: init pull
	@hugo
deploy: build
	@./bin/deploy.sh
deploy.sh Link to heading
公式のデプロイ方法に書いてあることに、少し自分に必要な要件を足して以下のようなスクリプトを用意した。
追加したことは以下。
- hugoコマンドの失敗に検知できるようにmakeファイルに役割を分割
 - 引数でメッセージを変更しないためmsgを固定
 - dateコマンドで出力されるフォーマットを英語表記になるようLANG=Cを追記
 - デプロイ後のcommit hashをブログリポジトリに反映させるためcommit & push
 
#!/bin/bash
LANG=C
echo -e "\033[0;32mDeploying updates to GitHub...\033[0m"
# Go To Public folder.
cd public
# Add changes to git.
git add .
# Commit changes.
msg="rebuilding site $(date)"
git commit -m "$msg"
# Push source and build repos.
git push origin master
# Come Back up to the Project Root.
cd ..
# Follows update of public.
git add public
git commit -m "$msg"
git push
new_post.sh Link to heading
postのディレクトリに新しく記事を追加するとき、ディレクトリにはcontent/posts/2018-09-15-my-fist-post/index.mdのように日付を設定したかったので、postを作成するためのscriptを用意。
#!/bin/bash
TITLE=${1}
if [ -z "${TITLE}" ]; then
  echo "Please give the post title!"
  exit 1
fi
dirname="$(date '+%Y-%m-%d')-${TITLE}"
hugo new posts/${dirname}/index.md --editor nvim