🙆‍♀️

Vite + react で Github Pages にデプロイする方法

に公開

はじめに

vite + react を GithubPages でデプロイをする方法をまとめます

Github Pages の設定

以下の写真の通りにすれば良いはず

vite config の設定

vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  base: process.env.GITHUB_PAGES ? "レポジトリ名" : "./",
})

Github Acticons の yml ファイル

main.yml
name: deploy

on:
  push:
    branches: ["main"]
  workflow_dispatch:

permissions:
  contents: read
  pages: write
  id-token: write

concurrency:
  group: "pages"
  cancel-in-progress: true

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 10
      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 20
          # yarnのキャッシュを効かすために設定 (npmのかたはnpmで)
          cache: pnpm
      - name: pnpm install
        run: pnpm install
      - name: pnpm build
        run: pnpm build
        # GIthubページは https://USERNAME.github.io/REPOSITORY_NAME/
        # となるので、envを追加してviteのベースパスを変えるように設定します。
        env:
          GITHUB_PAGES: true
      - name: Upload Pages artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: ./dist

  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

終わりに

まだ不十分なところがあると思いますので随時追記します

Discussion