テーマをHugo Modulesベースで扱うHugoプロジェクトの作り方
Table of Contents
Hugo テーマを取り飲むのに以前からのthemes/
に配置する方法ではなく、Hugo Modulesを用いて外部参照して使用する方法。
0. 事前準備
Hugo Modulesを使うには、hugo
コマンドの他に、
git
コマンドgo
コマンド(golang
パッケージ内)
が必須になるので、プラットフォームごとの手段でインストールしておくこと。
1. 空プロジェクトから作る方法
作る前に、Hugoプロジェクト自体のMod IDを決めておく必要がある。
ふつうは、(実際にpushする必要はなくても、)ソースであるHugoプロジェクトをgithubに置く前提で命名する。
- organization/repositoryが、それぞれexample-org/example-org.github.ioという名前である場合、Mod IDは
github.com/example-org/example-org.github.io
となる。
Hugoプロジェクト自体のディレクトリ名は適当な名前もよい。
$ mkdir example-org-site/
$ cd example-org-site/
$ hugo mod init github.com/example-org/example-org.github.io
$ mkdir content/
$ emacs hugo.toml
たとえば、Hugo Bear Blogテーマを使用するものとする場合を扱う。
テーマの説明の中に、git submodule add https://github.com/janraasch/hugo-bearblog.git themes/hugo-bearblog
という記述がある。
この中のgitリポジトリのURL https://github.com/janraasch/hugo-bearblog.git
から、
テーマのMod IDはgithub.com/janraasch/hugo-bearblog
と決まる(必ずhttps://
と.git
は外す)。
最初のhugo.toml
の内容は、テーマをインポートする記述だけをする:
[module]
[[module.imports]]
path = "github.com/janraasch/hugo-bearblog"
(注: hugo-bearblogは違うが、テーマによってはSCSSなどのビルドのための環境の用意(sassc等)が必要になる場合がある)
この時点で、ディレクトリの下には、以下のファイルができている:
hugo.toml
go.mod
content/
resources/_gen/
(go.mod
とresources/_gen/
以下は、hugo mod init
で作られる)
ここで、サーバを立ち上げれば、テーマがダウンロードされ、http://localhost:1313/
のWebページに適用されることをブラウザで確認する。
$ hugo server
つぎに記事を追加できることを確認する。
$ hugo new blog/first-post.md
$ emacs content/blog/first-post.md
その後は、これまでと同様に、hugo.toml
でテーマの設定を変更などを行っていくことになるだろう。
2. Github ActionsでビルドしてGithub Pagesで公開する方法
続いて、hugo modベースのHugoプロジェクトを、gitリポジトリ化し、github上にpushし、Github ActionsでHTMLサイトをビルドさせてGithub Pagesで公開するまで行う。
gitリポジトリ化
$ cd example-org-site/
$ git init
$ emacs .gitignore
$ git add .gitignore hugo.toml go.mod go.sum content/
$ git commit -m "[init]"
Hugoプロジェクトのための.gitignore
ファイルのの内容は、以下のとおり:
/public/
/resources/_gen/
/assets/jsconfig.json
/.hugo_build.lock
hugo_stats.json
Github Actionsの設定
以下の内容の.github/workflows/gh-pages.yml
を追加する:
name: GitHub Pages
on:
push:
branches:
- main # Set a branch name to trigger deployment
pull_request:
jobs:
deploy:
runs-on: ubuntu-22.04
permissions:
contents: write
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
steps:
- uses: actions/checkout@v3
with:
submodules: true # Fetch Hugo themes (true OR recursive)
fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod
- name: Setup Go
uses: actions/setup-go@v4.0.0
with:
go-version: '^1.20'
- name: Setup Hugo
uses: peaceiris/actions-hugo@v2
with:
hugo-version: '0.113.0'
- name: Build
run: hugo --minify
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
# If you're changing the branch from main,
# also change the `main` in `refs/heads/main`
# below accordingly.
if: ${{ github.ref == 'refs/heads/main' }}
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./public
Hugo Modulesを使う場合はgo
コマンドも必要なため、actions-hugo
のREADMEにあるymlの内容のBuild
ステップの前に、setup-go
を加えたものになる。
$ mkdir .github/workflows/
$ emacs .github/workflows/gh-pages.yml
$ git add .github/
$ git commit -m "[add] .github/workflows/gh-pages.yml"
$ git push origin HEAD
Actions
で、pages build and deployment
が終了したあと、
Github Pagesを有効化するために、github.com上のリポジトリページで、Settings
- Pages
のBranch
をgh-pages
に変えれば、Webページが表示できるようになる。
A. 既存のHugoサイトをHugo ModulesベースのHugoプロジェクトに変更する方法
失敗しやすいので、作業をする前に Hugoサイトをまずバックアップしておく べきである。
Githubにおいてあるなら、別途git clone
したもの(--recursive
なし)で作業すべきである。
A.1. テーマを消す
hugo mod init
でもhugo.toml
を解釈するので、後に不要になるものを先に外しておく。
テーマはbinarioを使っているとする。
A.1,1. hugo.toml
からtheme
を消す
hugo.toml
内から、
theme = "binario"
を削除する。
A.1.2. themes/
ディレクトリのクリーンアップ
themes/binario
を消す。
- zip展開やgit cloneでの展開であれば、
themes/
ディレクトリごと削除するだけ
ただし、git submodule
でテーマを導入している場合は、以下の手順で行う:
$ git submodule deinit -f themes/binario
$ git rm -rf themes/
$ git rm .gitmodules
$ rm -rf .git/modules/
A.2. hugo mod init
Github上のorganization/repositoryがbellbind-trial/bellbind-trial.github.io
なので、
$ hugo mod init github.com/bellbind-trial/bellbind-trial.github.io
を実行する。
A.3. テーマの追加
hugo.toml
に以下のエントリーを加える(一番下でよい):
テーマのgitリポジトリのURLがhttps://github.com/vimux/binario
であるため、Mod IDはgithub.com/vimux/binario
になる。
[module]
[[module.imports]]
path = "github.com/vimux/binario"
A.4. .github/workflows/gh-pages.yml
の編集
Buildステップの前に、setup-goのエントリーを加える:
steps:
- uses: actions/checkout@v3
with:
submodules: true # Fetch Hugo themes (true OR recursive)
fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod
# ここから
- name: Setup Go
uses: actions/setup-go@v4.0.0
with:
go-version: '^1.20'
# ここまで
- name: Setup Hugo
uses: peaceiris/actions-hugo@v2
with:
hugo-version: '0.113.0'
- name: Build
run: hugo --minify
A.5. hugo server
で確認
gitリポジトリへ反映する前に、hugo server
で以前と同じように表示されることを確認する。
うまく表示されていない場合、git cloneから、途中で失敗しないようにしてやり直したらうまくいくかもしれない。
A.6. git commitし、githubへpush
$ git add hugo.toml .github/
$ git commit -m "[switch] to hugo mod"
$ git push origin HEAD
Github Actionsが成功することを確認し、Webページが表示されることを確認する。
B. 共有キャッシュを消すコマンド: hugo mod clean
現状、テーマのような外部のHugo Modulesは、Hugoプロジェクトごとに保持するのではなく、ローカルマシン上で別途、共有してキャッシュされている。
全キャッシュを消すコマンド:
$ hugo mod clean --all
たとえばhugo server
を立ち上げて、テーマモジュールのセットアップ中などに強制終了させた場合、その後のhugo server
のHTML生成がおかしくなる場合がある。
そういう場合には、一度キャッシュを全部消して、やり直すとなおるかもしれない。
C. 依存モジュールを最新版にアップデートするコマンド: hugo mod get -u
すべての依存モジュールを最新バージョンに更新するコマンド:
$ hugo mod get -u ./...
- 注:
hugo mod get -u
の引数の./...
は例示ではない。そのまま打ち込むこと
この操作によって、go.mod
とgo.sum
ファイルの内容が更新されるので、gitリポジトリにも反映させる。
$ git add go.mod go.sum
$ git commit