📘

Hugo、Firebase、GitHub Actionsでブログを自動デプロイする

2022/08/09に公開

Hugoのセットアップと記事作成

brew install hugo
hugo new site hugo_blog --force
cd hugo_blog
git submodule add https://github.com/chipzoller/hugo-clarity themes/clarity
echo theme = \"clarity\" >> config.toml
hugo new posts/my-first-post.md
hugo

Firebase CLIをインストール

npm install -g firebase-tools

Firebase Consoleでプロジェクトを作成しておく

Firebase CLIでGitHub Actionsでデプロイする設定ファイルを生成する

firebase init hosting
? What do you want to use as your public directory? public
? Configure as a single-page app (rewrite all urls to /index.html)? No
? Set up automatic builds and deploys with GitHub? Yes
? File public/404.html already exists. Overwrite? No
? File public/index.html already exists. Overwrite? No

? For which GitHub repository would you like to set up a GitHub workflow? (format: user/repository) ohnishi/hugo_blog
? Set up the workflow to run a build script before every deploy? No
? Set up automatic deployment to your site's live channel when a PR is merged? Yes
? What is the name of the GitHub branch associated with your site's live channel? main

HogoのビルドタスクをGitHub Actionsのワークフローに追加する

firebase-hosting-merge.yml

# This file was auto-generated by the Firebase CLI
# https://github.com/firebase/firebase-tools

name: Deploy to Firebase Hosting on merge
'on':
  push:
    branches:
      - main
jobs:
  build_and_deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
          fetch-depth: 0
      - uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: 'latest'
      - run: hugo --minify
      - uses: FirebaseExtended/action-hosting-deploy@v0
        with:
          repoToken: '${{ secrets.GITHUB_TOKEN }}'
          firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT_HUGO_BLOG }}'
          channelId: live
          projectId: hugo_blog

firebase-hosting-pull-request.yml

# This file was auto-generated by the Firebase CLI
# https://github.com/firebase/firebase-tools

name: Deploy to Firebase Hosting on PR
'on': pull_request
jobs:
  build_and_preview:
    if: '${{ github.event.pull_request.head.repo.full_name == github.repository }}'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
          fetch-depth: 0
      - uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: 'latest'
      - run: hugo --minify --environment preview
      - uses: FirebaseExtended/action-hosting-deploy@v0
        with:
          repoToken: '${{ secrets.GITHUB_TOKEN }}'
          firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT_HUGO_BLOG }}'
          projectId: hugo_blog

参考記事:
https://sh0e1.com/posts/2020/10/29/ci-cd-using-github-actions-with-hugo-firebae-hosting/
https://github.com/marketplace/actions/hugo-and-firebase

Discussion