VitePress部署GitHubPage失败?可以看看这个

435 阅读1分钟
name: Deploy
on:
  workflow_dispatch: {}
  push:
    branches:
      - main
jobs:
  deploy:
    runs-on: ubuntu-latest
    permissions:
      pages: write
      id-token: write
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - uses: actions/setup-node@v3
        with:
          node-version: 16
          cache: npm
      - run: npm ci
      - name: Build
        run: npm run docs:build
      - uses: actions/configure-pages@v2
      - uses: actions/upload-pages-artifact@v1
        with:
          path: docs/.vitepress/dist
      - name: Deploy
        id: deployment
        uses: actions/deploy-pages@v1

这是官网给出的github action文件,实际运行会遇见几个问题。

  1. 你需要用npm,如果你用的pnpm没有package-lock文件会导致报错,所以如果你知道咋改yaml,就可以用pnpm。
  2. 需要去githubpage设置里开启workflow的写权限
  3. 主要npm run build的输出路径,我在本地去掉了一层多余的docs,所以对应的path字段里也要改。
  4. base路径,其实就是需要写上你的项目名字。 后面跑去vue仓库看了下咋配pnpm,用上了,首先package.json记得加上
 "packageManager": "pnpm@8.4.0",

然后,把yml改成:

name: Deploy
on:
 workflow_dispatch: {}
 push:
   branches:
     - main
jobs:
 deploy:
   runs-on: ubuntu-latest
   permissions:
     pages: write
     id-token: write
   environment:
     name: github-pages
     url: ${{ steps.deployment.outputs.page_url }}
   steps:
     - uses: actions/checkout@v3
       with:
         fetch-depth: 0
     - name: Install pnpm
       uses: pnpm/action-setup@v2

     - name: Set node version to 18
       uses: actions/setup-node@v3
       with:
         node-version: 18
         cache: "pnpm"
     - run: pnpm install
     - name: Build
       run: pnpm run docs:build
     - uses: actions/configure-pages@v2
     - uses: actions/upload-pages-artifact@v1
       with:
         path: .vitepress/dist
     - name: Deploy
       id: deployment
       uses: actions/deploy-pages@v1

最终结果:SnLan's blog | SnLan's blog (bigsnowballhehe.github.io)
非常简略敷衍的博客,主要是用下vitepress玩玩。

如果用githubPage部署其他SPA项目的话,则需要考虑用hash路由,不然刷新就404,虽然有些方法是用404页面去搞操作,但是挺麻烦也不太好用。