From 167a42a3c64f820083553ee4a67efcc91029c0b0 Mon Sep 17 00:00:00 2001 From: hayyi2 Date: Sun, 19 Nov 2023 22:23:45 +0700 Subject: [PATCH] github action build & deploy gh-pages --- .github/workflows/build-and-deploy.yml | 41 ++++++++++++++++++++++++++ src/Router.tsx | 4 ++- src/vite-env.d.ts | 4 +++ vite.config.ts | 24 +++++++++++---- 4 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/build-and-deploy.yml diff --git a/.github/workflows/build-and-deploy.yml b/.github/workflows/build-and-deploy.yml new file mode 100644 index 0000000..d5a044b --- /dev/null +++ b/.github/workflows/build-and-deploy.yml @@ -0,0 +1,41 @@ +name: Build & Deploy + +on: + workflow_dispatch: +# on: +# push: +# branches: ["main"] + +jobs: + deploy: + runs-on: ubuntu-latest + + permissions: + contents: write + + strategy: + matrix: + node-version: ["20.9.0"] + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + cache: "npm" + + - name: Install packages + run: npm i + + - name: Run npm build + run: npm run build + + - name: Deploy to gh-pages + uses: peaceiris/actions-gh-pages@v3 + with: + publish_dir: dist + publish_branch: gh-pages + github_token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/src/Router.tsx b/src/Router.tsx index ded06b8..a8c8798 100644 --- a/src/Router.tsx +++ b/src/Router.tsx @@ -24,4 +24,6 @@ export const router = createBrowserRouter([ path: "*", element: , }, -]) +], { + basename: global.basename +}) diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index 11f02fe..c476cbc 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -1 +1,5 @@ /// + +declare const global: { + basename: string +} diff --git a/vite.config.ts b/vite.config.ts index 03df0c3..886f11c 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -2,11 +2,23 @@ import path from "path" import react from "@vitejs/plugin-react" import { defineConfig } from "vite" -export default defineConfig({ - plugins: [react()], - resolve: { - alias: { - "@": path.resolve(__dirname, "./src"), +const basenameProd = '/react-shadcn-starter' + +export default defineConfig(({ command }) => { + const isProd = command === 'build' + + return { + base: isProd ? basenameProd : '', + plugins: [react()], + resolve: { + alias: { + "@": path.resolve(__dirname, "./src"), + }, + }, + define: { + global: { + basename: isProd ? basenameProd : '', + }, }, - }, + } }) \ No newline at end of file