Vue + Vite Github Pages 部屬

手動部屬

可以先參考官方文檔

https://vitejs.dev/guide/static-deploy.html#github-pages

  1. 如果要部屬到 https://{USERNAME}.github.io/

    1. 在vite.config.js 中
    1
    2
    3
    4
    
    export default defineConfig({
      base:"/", // 這行可以不寫
    	...
    	})
    
  2. 如果要部屬到 https://{USERNAME}.github.io/{REPO}/

    1. 在vite.config.js 中
    1
    2
    3
    4
    
    export default defineConfig({
      base:"/{REPO}/",  // 這邊要設定成這樣
    	...
    	})
    

一些問題

  1. 部屬上去發現整頁都是空白的

    1. 使用vue-router可以去檢查一下 router/index.js
    1
    2
    3
    4
    
    const router = createRouter({
      history: createWebHashHistory(import.meta.env.BASE_URL),
      routes,
    });
    

    history 是使用 import.meta.env.BASE_URL 還是 process.env.BASE_URL 使用vite的夥伴們應該會是import.meta.env.BASE_URL 才對

  2. 切換頁面出現 404

    https://jerry-yeh.github.io/vue-3/20210911/13819/

    這邊我從這篇文章學到的

    在vue-router 中分成 Hash Mode 以及 HTML5 Mode

    github page上面採用的是 HTML5 Mode

    在切換頁面的時候github直接就去尋找下一個html在哪裡了

    可是我們編譯出來的vue 只有一個 index.html

    • 路徑

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      
      dist/
      ├── assets
      │   ├── Default.4d1f92cb.css
      │   ├── Default.a1a4c9a7.js
      │   ├── Home.8ec53b9f.js
      │   ├── Home.a16ec1d4.css
      │   ├── SCLog.9efe6819.js
      │   ├── SuperChat.22981faf.js
      │   ├── SuperChat.ee295ced.css
      │   ├── index.1ceeaf22.css
      │   ├── index.78b3cc48.js
      │   ├── materialdesignicons-webfont.48d3eec6.woff
      │   ├── materialdesignicons-webfont.861aea05.eot
      │   ├── materialdesignicons-webfont.bd725a7a.ttf
      │   ├── materialdesignicons-webfont.e52d60f6.woff2
      │   ├── tag.7a6d0029.js
      │   └── webfontloader.b777d690.js
      ├── favicon.ico
      └── index.html
      

    • 解決辦法

      createWebHistory改成createWebHashHistory

      1
      2
      3
      4
      
      const router = createRouter({
        history: createWebHashHistory(import.meta.env.BASE_URL),
        routes,
      });
      

自動部屬

Github Action

https://tzuhui.io/2020/12/11/Vue/Vue-deploy-github-actions/

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# This is a basic workflow to help you get started with Actions
name: deploy

# Controls when the action will run. 
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2
      
      - name: Install & Build # 執行指令
        run: |
          npm install
          npm run build 
          echo > dist/.nojekyll          
      # Runs a single command using the runners shell
      - name: Deploy
        uses: JamesIves/github-pages-deploy-action@releases/v4
        with:
          ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }} # Settings > Secret 建立的 ACCESS_TOKEN,推同個 repo 的話可以不需要
          BRANCH: gh-pages # deploy 到 gh-pages 這個分支
          FOLDER: dist # build 後的資料夾

https://github.com/ricardojlrufino/markdown-tui-editor/blob/master/.github/workflows/deploy_site.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
name: Site CI

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Use Node.js 
      uses: actions/setup-node@v3
      with:
        node-version: 16.x
        cache: 'yarn'
  
    - run: yarn install --frozen-lockfile
    - run: yarn build --base=/${{ github.event.repository.name }}
    - run: | 
        git config user.name github-actions
        git config user.email [email protected]
        touch dist/.nojekyll
        git --work-tree dist add --all
        git commit -m "Automatic Deploy action run by github-actions"
    - run: git push origin HEAD:gh-pages --force
    
    - name: Pushes to another repository
      id: push_directory
      uses: cpina/github-action-push-to-another-repository@main
      env:
        API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}
      with:
        source-directory: dist/
        destination-github-username: 'rjlr-online-tools'
        destination-repository-name: '${{ github.event.repository.name }}'
        user-email: [email protected]
        commit-message: See ORIGIN_COMMIT from $GITHUB_REF
        target-branch: main

有問題可以在下方utterances留言喔

updatedupdated2024-08-192024-08-19
載入評論