💨

VueのアプリケーションをGithub Pagesにホスティングするまでの流れ(なるべく最小構成)

に公開

目的

簡単なツールをVueで作ってWebからアクセスしたいとき、Github Pagesを利用すると便利なのでその手順をまとめておく

環境

今回はパッケージマネージャーにpnpmを利用する

node 25.2.1
pnpm 10.24.0

手順

Vueのアプリケーション作成

作成コマンド

pnpm create vue@latest

プロンプトに従ってプロジェクト名などを入力 (ここではgh-hosting-demoとしておく)
選択は最小限

┌  Vue.js - The Progressive JavaScript Framework
│
◇  Project name (target directory):
│  gh-hosting-demo
│
◇  Select features to include in your project: (↑/↓ to navigate, space to select, a to toggle all, enter to confirm)
│  TypeScript
│
◇  Select experimental features to include in your project: (↑/↓ to navigate, space to select, a to toggle all, enter to confirm)
│  none
│
◇  Skip all example code and start with a blank Vue project?
│  Yes

起動確認

pnpm install
pnpm dev

ベースディレクトリの設定

Github Pagesはhttps://<Githubユーザー名>.github.io/<リポジトリ名>/というURLで公開されるので、ベースパスを調整しておく

vite.config.ts
import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'

export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
  // リポジトリ名を入れる
  base: '/gh-hosting-demo/',
})

Github Actionsの設定ファイル作成

デプロイ設定を書くディレクトリを作る

mkdir -p .github/workflows

デプロイ設定ファイル

.github/workflows/deploy.yml
name: Deploy to GitHub Pages

on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 10

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: '24'
          cache: 'pnpm'

      - name: Install Dependencies
        run: pnpm install --frozen-lockfile

      - name: Build
        run: pnpm build

      - name: Deploy
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          folder: dist
          branch: gh-pages

これでビルドされた成果物がgh-pagesブランチにデプロイされる

プロジェクトディレクトリをGithubにコミットしておく

Githubの設定

SettingsからCode and automation > Pages
ビルド成果物をデプロイしているgh-pagesブランチを指定してSave

アクセス確認

GithubリポジトリにコードコミットするとGithub Actionsが動いて、Github Pagesとして公開してくれる

https://<Githubユーザー名>.github.io/gh-hosting-demo/ でページが確認できればOK

Discussion