Github Actions から Github Pages にデプロイ
現時点で一番スター数が多そうな以下のアクションを使うこととする
gh-pages ブランチがない場合
master ブランチの特定ディレクトリ配下を Pages にすることもできるが、今回はリポジトリ上のデータから HTML を生成して成果物を Pages にデプロイしたいのでクリーンな状態の gh-pages ブランチを用意する
code:console
git checkout master
git checkout --orphan gh-pages
git rm --cached $(git ls-files)
echo foo > index.html
git add index.html
git commit -am "first page"
git push origin gh-pages
Deploy key の登録
$ ssh-keygen -t rsa -b 4096 -C "$(git config user.email)" -f gh-pages -N ""
公開鍵はリポジトリ設定の Deploy keys で Allow write access にチェックを入れて登録
秘密鍵はリポジトリ設定の Secrets に登録
今回は ACTIONS_DEPLOY_KEY という名前で登録
Workflow の作成
以下の workflow で public 配下がデプロイできるので、適当に成果物を public 配下に入れればOK
code:deploy.yml
- name: Deploy
uses: peaceiris/actions-gh-pages@v2
env:
ACTIONS_DEPLOY_KEY: ${{ secrets.ACTIONS_DEPLOY_KEY }}
PUBLISH_BRANCH: gh-pages
PUBLISH_DIR: ./public
ブランチによってデプロイ先を分けたい場合
例えば master ブランチは Github Pages に、 dev ブランチは確認用に Netlify にみたいな場合は if と github.ref を使ってジョブの実行を管理すればOK code:deploy.yml
on:
push:
branches:
- master
- dev
jobs:
dev_deploy:
if: github.ref == 'refs/heads/dev'
...
master_deploy:
if: github.ref == 'refs/heads/master'