|
| 1 | +name: Deploy Vite App to GitHub Pages |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main # Change this if your default branch is not `main` |
| 7 | + |
| 8 | +jobs: |
| 9 | + deploy: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + |
| 12 | + steps: |
| 13 | + # Step 1: Check out the repository |
| 14 | + - name: Checkout repository |
| 15 | + uses: actions/checkout@v3 |
| 16 | + |
| 17 | + # Step 2: Set up Node.js |
| 18 | + - name: Setup Node.js |
| 19 | + uses: actions/setup-node@v3 |
| 20 | + with: |
| 21 | + node-version: 18 |
| 22 | + |
| 23 | + # Step 3: Install dependencies |
| 24 | + - name: Install dependencies |
| 25 | + run: npm install |
| 26 | + |
| 27 | + # Step 4: Build the project |
| 28 | + - name: Build project |
| 29 | + run: npm run build |
| 30 | + |
| 31 | + # Step 5: Adjust paths for GitHub Pages |
| 32 | + - name: Fix paths for GitHub Pages |
| 33 | + run: | |
| 34 | + node <<EOF |
| 35 | + const fs = require('fs'); |
| 36 | + const path = require('path'); |
| 37 | +
|
| 38 | + const repoName = process.env.GITHUB_REPOSITORY.split('/')[1]; |
| 39 | + const distDir = path.resolve('dist'); |
| 40 | + const indexFile = path.join(distDir, 'index.html'); |
| 41 | +
|
| 42 | + let html = fs.readFileSync(indexFile, 'utf-8'); |
| 43 | + html = html.replace(/src="\//g, `src="/${repoName}/`); |
| 44 | + html = html.replace(/href="\//g, `href="/${repoName}/`); |
| 45 | +
|
| 46 | + fs.writeFileSync(indexFile, html); |
| 47 | + console.log('Paths updated for GitHub Pages.'); |
| 48 | + EOF |
| 49 | +
|
| 50 | + # Step 6: Deploy to GitHub Pages |
| 51 | + - name: Deploy to GitHub Pages |
| 52 | + uses: peaceiris/actions-gh-pages@v3 |
| 53 | + with: |
| 54 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 55 | + publish_dir: dist |
0 commit comments