6 步搭建 GitHub Page 个人网站

0. 目录

1. 前言

上一篇 文档我们实现了一个简单的 Vue Playground。

2. 前置条件

本文默认您有下述前置条件:

  • Git 环境
  • GitHub 账户
  • 能提交代码到 GitHub 上。

3. 构建网站

3.1 创建空 Vue Playground 仓库

登录 GitHub 个人账户后,新增空白 Vue Playground 仓库,命名成 vue-playground。

3.2 修改仓库配置

需要修改仓库 page 构建的方式为 GitHub Actions。

3.3 建立本地项目与 GitHub 仓库链接。

注意: 下述命令中的username 需要替换成您个人的账户名称。

shell 复制代码
git init
git remote add origin https://github.com/[username]/vue-playground.git
git branch -M main

3.4 创建部署配置文件

根目录下创建 .github/workflows/deploy.yml。 复制下面配置。

yml 复制代码
name: Deploy Vue Playground 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:
    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: 20
          cache: npm # 或 pnpm / yarn
      - name: Setup Pages
        uses: actions/configure-pages@v4
      - name: Install dependencies
        run: npm ci # 或 pnpm install / yarn install / bun install
      - name: Build
        run: npm run build
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: dist

  # 部署工作
  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    needs: build
    runs-on: ubuntu-latest
    name: Deploy
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

3.5 修改 vite.config.js配置。

GitHub Page 是纯静态页面,需要设置 base 为绝对路径。

javascript 复制代码
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  // 注意: username 为您的个人用户名的全小写。
  base: "https://[username].github.io/vue-playground/",
  plugins: [vue()],
})

3.6 提交代码并触发 GitHub Action 构建

shell 复制代码
git add .
git commit -m "初始化并构建vue-playground"
git push -u origin main
相关推荐
莫晓燚30 分钟前
Ansible 13 个常用模块---企业实战
git·github
程序员柒叔2 小时前
Diagram Design 解构:45 种图型如何用语义路由与 SVG 质量门交付
人工智能·github·agent·skills
逛逛GitHub2 小时前
9 个神级 GitHub 项目助你蹭上抖音、小红书 Vibe 知识大赏热点。
github
m4Rk_2 小时前
【论文阅读】Agent 记忆机制(119):EvoLife——让具身 Agent 把主动问到的答案沉淀为可复用的空间经验
论文阅读·人工智能·学习·开源·github
小郎君6585 小时前
Vue 多标签页的加载态设计:为什么我没有用 Suspense 实现 PageLoading
vue.js·算法
yilaisai5 小时前
前端开发遇到的一些细节问题整理
vue.js
华玥作者6 小时前
Vue 3 + TypeScript + uni-app 组件库推荐:hy-app(华玥组件库)
vue.js·typescript·uni-app
后台模板学习6 小时前
GitHub Actions 与 Jenkins 在日志监
运维·github·jenkins
miofly6 小时前
GitHub 今日推荐|auto-ontology:给 Agent 装上企业数据的语义索引
python·开源·github
阿琳a_6 小时前
b-map-viewer:面向 Vue 3 的地图可视化 SDK,从接入到展示再到交互分析
前端·javascript·vue.js·gis·数据可视化·地图