Skip to content

部署vitepress

将vitepress部署在gitlab pages

在vitpress中创建三个文件,然后将viepress整体提交到gitlab,启用gitlab pages和gitlab ci自动构建,每次提交就会自动构建部署。

要添加的文件如下:

创建.gitlab-ci.yml,写入。

yml
# The Docker image that will be used to build your app
image: node:lts

cache:
  paths:
    - node_modules/

# Functions that should be executed before the build script is run
before_script:
  - npm add -D vitepress

pages:
  script:
    - sed -i '$d' package.json
    - sed -i 's/}/},/g' package.json
    - sed -i '$ r ./requirments/add.txt' package.json
    - cat package.json
    - npm run docs:build
  artifacts:
    paths:
      # The folder that contains the files to be exposed at the Page URL
      - public
  rules:
    # This ensures that only pushes to the default branch will trigger
    # a pages deploy
    - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH

创建.gitignore文件,写入:

package.json
package-lock.json
node_modules/
docs/.vitepress/cache/

创建requirments文件夹,在文件夹中创建add.txt文件,写入:

  "scripts": {
    "docs:dev": "vitepress dev docs",
    "docs:build": "vitepress build docs",
    "docs:preview": "vitepress preview docs"
  }
}

将vitepress部署在gitHub pages

新建.github\workflows两层文件夹,在workflows下新建action.yml文件,写入:

# 构建 VitePress 站点并将其部署到 GitHub Pages 的示例工作流程
#
name: Deploy VitePress site to Pages

on:
  # 在针对 `main` 分支的推送上运行。如果你
  # 使用 `master` 分支作为默认分支,请将其更改为 `master`
  push:
    branches: [main]

  # 允许你从 Actions 选项卡手动运行此工作流程
  workflow_dispatch:

# 设置 GITHUB_TOKEN 的权限,以允许部署到 GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write

# 只允许同时进行一次部署,跳过正在运行和最新队列之间的运行队列
# 但是,不要取消正在进行的运行,因为我们希望允许这些生产部署完成
concurrency:
  group: pages
  cancel-in-progress: false

jobs:
  # 构建工作
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0 # 如果未启用 lastUpdated,则不需要
      # - uses: pnpm/action-setup@v3 # 如果使用 pnpm,请取消注释
      # - uses: oven-sh/setup-bun@v1 # 如果使用 Bun,请取消注释
      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
      - name: Setup Vitepress
        run: npm add -D vitepress
      - name: Config Vitepress package.json
        run: |
          sed -i '$d' package.json
          sed -i 's/}/},/g' package.json
          sed -i '$ r ./.github/workflows/add.txt' package.json
      - name: Setup Plugin
        run: npm i vitepress-plugin-comment-with-giscus
      - name: Build with VitePress
        run: npm run docs:build
      - name: Upload
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          repository-name: 要托管到的静态仓库
          branch: main
          folder: public

在workflow下新建add.txt文件,写入:

  "scripts": {
    "docs:dev": "vitepress dev docs",
    "docs:build": "vitepress build docs",
    "docs:preview": "vitepress preview docs"
  }
}

https://github.com/settings/personal-access-tokens中为要托管到的静态仓库新建密钥,注意要给密钥相应的权限。

https://github.com/上传的项目地址/settings/secrets/actions中创建名为ACCESS_TOKEN的secret,值为前面生成的密钥。

最后更新于:

未经许可禁止任何形式的转载