Github Actionsによってデプロイまでの煩雑な処理が自動化できるようになった昨今なので、今回は静的サイトジェネレーターの一つであるhugoで、デフォルトブランチとして設定したmainブランチへのプッシュがあった際に./public下のファイルをmasterブランチにDeployする手法をまとめたいと思います。(<username.github.io>としたリポジトリの場合そのままmasterブランチがGithub Pagesとして公開できます。)
すでに便利なCIがあるのでこちらactions-hugoを利用していきます。
hugoのプロジェクトルートに
$ cd <hugo-project-root> $ git switch main $ mkdir -p .github/workflows/ $ touch .github/workflows/gh-pages.yml
を設置します。
そしてこのgh-pages.ymlにActionsによって自動化したい処理の内容とフックのタイミングについて記述していきます。
<gh-pages.yml>
name: github pages
on:
push:
branches:
- main # Set a branch to deploy
jobs:
deploy:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
with:
submodules: true # Fetch Hugo themes (true OR recursive)
fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod
- name: Setup Hugo
uses: peaceiris/actions-hugo@v2
with:
hugo-version: '0.75.1'
extended: true
- name: Build
run: hugo --minify
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_branch: master
publish_dir: ./public
これでmainブランチをプッシュするとそれに同期してpublicがmasterブランチにコミットされるかと思います。
これらを活用すればphpunitなどを利用したテストもmasterにマージされる際にGithub上で回すなどしてより手軽で堅牢な開発体制が整うかと思います。


