LoginSignup
1
1

More than 1 year has passed since last update.

GitHub ActionsからGitHub Pagesにデプロイする

Posted at

はじめに

準備

  • まずは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
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1