GitHub Actions で Github Pages を更新する方法
github actions でビルドした内容で github pages で公開しているサイトを更新する方法をメモ。
環境+要件
案1: github-push-action
github token を使って簡単に git push してくれる action。簡単に使えるし単機能でよいが、以下の理由で今回は使えなかった。
So while you can push to the `gh-pages` branch using the `GITHUB_TOKEN`, it won't spawn a GitHub Pages build.
https://github.community/t5/GitHub-Actions/Github-action-not-triggering-gh-pages-upon-push/m-p/26869/highlight/true#M301
You'll need to create a personal access token and supply it to your GitHub Action as a secret.
↑のスレッドの内容(でかつ私の環境で再現した事項)をまとめると、
GitHub Actions から `GITHUB_TOKEN` で push するとパブリックリポジトリの GitHub Pages のビルドが動かない
案2: actions-gh-pages
案1 の引用先で薦められてた github pages 専用 action。
なにやらいろいろ出来るようだけどブランチ全体のコンテンツを上書きしてしまうので使えなかった。
採用案
自力で personal access token を使って push すればよい。
.. jobs: .. publish: name: publish to github pages runs-on: ubuntu-18.04 needs: build # ビルドの後に実行 # master への push の時のみ動作 if: github.event_name == 'push' && github.ref == 'refs/heads/master' steps: - uses: actions/checkout@v1 with: ref: gh-pages fetch-depth: 1 - uses: actions/download-artifact@v1 with: name: artifact - name: Commit artifact run: | git config user.email "admin@example.com" git config user.name "Github Action Bot" git add . # artifact を追加(実際はコンテンツの一部を上書き) if ! (git diff --quiet && git diff --staged --quiet); then git commit -m "Add: changes ($GITHUB_SHA)" # ハッシュをメモってコミット fi - name: Push to gh-pages branch env: PERSONAL_TOKEN: ${{ secrets.PERSONAL_TOKEN }} # personal access token を使う PUBLISH_BRANCH: gh-pages run: | git push https://<githubユーザ>:${PERSONAL_TOKEN}@github.com/<githubユーザ>/<リポジトリ>