From b90b253785e71a6748e17ec89c587f15e6c48d58 Mon Sep 17 00:00:00 2001 From: zihluwang Date: Mon, 15 Sep 2025 20:25:29 +0800 Subject: [PATCH] ci: add GitHub Pages deployment workflow Add GitHub Actions workflow for automatic deployment to GitHub Pages. Includes build and deploy jobs with pnpm setup and caching. Also update package.json with deploy scripts and vite.config.ts with base URL configuration for production. --- .github/workflows/deploy.yml | 79 ++++++++++++++++++++++++++++++++++++ package.json | 4 +- public/.nojekyll | 0 vite.config.ts | 1 + 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/deploy.yml create mode 100644 public/.nojekyll diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..11d6ad9 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,79 @@ +name: Deploy to GitHub Pages + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages +permissions: + contents: read + pages: write + id-token: write + +# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. +# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. +concurrency: + group: "pages" + cancel-in-progress: false + +jobs: + # Build job + build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: '20' + + - name: Setup pnpm + uses: pnpm/action-setup@v4 + with: + version: latest + + - name: Get pnpm store directory + shell: bash + run: | + echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV + + - name: Setup pnpm cache + uses: actions/cache@v4 + with: + path: ${{ env.STORE_PATH }} + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} + restore-keys: | + ${{ runner.os }}-pnpm-store- + + - name: Install dependencies + run: pnpm install --frozen-lockfile + + - name: Build + run: pnpm build + + - name: Setup Pages + uses: actions/configure-pages@v5 + + - name: Upload artifact + uses: actions/upload-pages-artifact@v3 + with: + path: './dist' + + # Deployment job + 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 \ No newline at end of file diff --git a/package.json b/package.json index 84a93fc..9bf00af 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,9 @@ "dev": "vite", "build": "tsc -b && vite build", "lint": "eslint .", - "preview": "vite preview" + "preview": "vite preview", + "deploy": "pnpm build && gh-pages -d dist", + "predeploy": "pnpm build" }, "dependencies": { "@reduxjs/toolkit": "^2.9.0", diff --git a/public/.nojekyll b/public/.nojekyll new file mode 100644 index 0000000..e69de29 diff --git a/vite.config.ts b/vite.config.ts index dfad972..aaeb578 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -6,6 +6,7 @@ import tailwindcss from "@tailwindcss/vite" // https://vite.dev/config/ export default defineConfig({ plugins: [react(), tailwindcss()], + base: process.env.NODE_ENV === 'production' ? '/react-template/' : '/', resolve: { alias: { "@": fileURLToPath(new URL("./src", import.meta.url)),