はじめに
- この記事では、GitHub ActionsからGitHub Pagesにデプロイする方法についてまとめている
- 今回、主に使用するアクションはGitHub Pages Action
- GitHub Pagesへのデプロイをサポートするサードパーティ製のアクション
- GitHub: https://github.com/peaceiris/actions-gh-pages
- 同様の機能を提供するアクションとしてupload-pages-artifactとdeploy-pagesも存在する
- こちらはGitHub公式のアクションだが、まだドキュメントが少ない印象
- GitHub
準備
- まずはGitHub Pagesのデプロイ先に使用するブランチを作成する
- Code > Branches > New Branch から作成可能
- 今回は
gh-pages
ブランチを作成をした
- 次にGitHub Pagesの公開設定を行う
- Settings > Pages > GitHub Pages > Build and deployment から設定可能
- 「Source」は
Deploy from a branch
を選択 - 「Branch」は
gh-pages
ブランチの/ (root)
を選択- 任意のブランチ(デフォルトブランチも可)の
/ (root)
か/docs
を公開可能
- 任意のブランチ(デフォルトブランチも可)の
設定
- GitHub Actionsのワークフローの設定
.github/workflows/example.yml
name: Publish GitHub Pages
# git pushをトリガにワークフローが実行される
on:
push:
# 書き込み権限の付与
permissions:
contents: write
jobs:
publish-gh-pages:
runs-on:
- ubuntu-latest
steps:
# GitHub Pagesに公開するファイルの作成
# 実際はGitリポジトリをチェックアウトしてそこからビルドすることが多い
- name: Create index.html
run: |
mkdir -p ./dist
echo "hello, world!" > ./dist/index.html
# GitHub Pagesに公開
# デフォルトではgh-pageeブランチにpushする
- name: Push gh-pages branch
uses: peaceiris/actions-gh-pages@v3
with:
# Deploy keysやPersonal access Tokensも使用可能
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./dist
# keep_filesをtrueにすると、git pushの度に一旦全てのファイルが削除される挙動を抑制できる
# keep_files: true
user_name: github-actions
user_email: github-actions@github.com
- 公開されたGitHub Pagesは https://user_name.github.io/repository_name/file_name でアクセス可能