元々バッチを使用して手動でデプロイを行っていたが、自動デプロイする方法がないか調査を行った
その結果を備忘録として残しておく

前提

[構成]

  • Hugo + GitHub Pages(master ブランチが対象)
    ※source ブランチを Hugo で変換して master ブランチに静的サイト用の資産を配置する
  • Hugo テーマは submodule として取り込み

実際のコード

GitHub Actions を使用する
リポジトリ直下に .github/workflows フォルダを作成して配下に yml ファイルを配置することで GitHub Actions が登録される
※ source ブランチの push をトリガーに自動デプロイを行う

  1. source ブランチをチェックアウト

  2. Hugo(Docker) を使用して、静的サイト用の資産を作成

  3. master ブランチに push して GitHub Pages にデプロイする

    name: Deploy
    
    on:
      push:
        branches:
          - source  # Target Branch Name
    
    jobs:
      deploy:
        runs-on: ubuntu-22.04
        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: Deleting old publication
            run: rm -rf public
    
          - name: Setup Hugo
            uses: peaceiris/actions-hugo@v2
            with:
              hugo-version: '0.91.2'
              extended: true
    
          - name: Build
            run: hugo
    
          - name: Deploy
            uses: peaceiris/actions-gh-pages@v3
            with:
              github_token: ${{ secrets.GITHUB_TOKEN }}
              publish_branch: master
              publish_dir: ./public
    

    定期実行する場合は、以下のようにする

    name: Scheduled Post
    
    on:
      schedule:
        - cron: '0 4 * * *' # Run every day at 13:00pm(JST).
    
    jobs:
      deploy:
        runs-on: ubuntu-22.04
        steps:
          - uses: actions/checkout@v3
            with:
              ref: 'source'     # Checkout Branch Name
              submodules: true  # Fetch Hugo themes (true OR recursive)
              fetch-depth: 0    # Fetch all history for .GitInfo and .Lastmod
    
          - name: Deleting old publication
            run: rm -rf public
    
          - name: Setup Hugo
            uses: peaceiris/actions-hugo@v2
            with:
              hugo-version: '0.91.2'
              extended: true
    
          - name: Build
            run: hugo
    
          - name: Deploy
            uses: peaceiris/actions-gh-pages@v3
            with:
              github_token: ${{ secrets.GITHUB_TOKEN }}
              publish_branch: master
              publish_dir: ./public