Skip to content

Commit 7bded47

Browse files
author
durunsong
committed
github pages自动部署测试
1 parent 5ce73d4 commit 7bded47

4 files changed

Lines changed: 26 additions & 30 deletions

File tree

.env.github

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 生产环境自定义的环境变量(命名必须以 VITE_ 开头)
2+
3+
## 路由模式 hash 或 html5
4+
VITE_ROUTER_HISTORY = 'hash'
5+
6+
## 打包路径(就是网站前缀,例如部署到 https://durunsong.github.io/kilyicms/ 域名下,就需要填写 /kilyicms/)
7+
VITE_PUBLIC_PATH = '/kilyicms/'
8+
9+
# 生产环境
10+
VITE_MODE = 'production'
11+
12+
## 后端接口公共路径(如果解决跨域问题采用 CORS 就需要写全路径)
13+
## 变量必须以 VITE_ 为前缀才能暴露给外部读取
14+
VITE_BASE_API = 'https://kilyicms-server.vercel.app'

.github/workflows/static.yml

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,42 @@
1-
# GitHub Actions 工作流程文件
2-
name: 自动部署为 GitPage 静态网站
1+
name: 自动部署为 GitHub Pages
32

4-
# 当代码推送到指定的分支(例如 main 分支)时,触发此工作流程
53
on:
64
push:
75
branches:
8-
- main # 当推送到 main 分支时触发
6+
- main
97

10-
# 设置 GITHUB _ TOKEN 的权限以允许部署到 GitHub 页面
118
permissions:
12-
contents: read # 仅允许工作流写入 `main` 分支内容。最小化权限以确保安全
9+
contents: write
1310
pages: write
1411
id-token: write
1512

1613
jobs:
1714
deploy:
18-
name: 自动部署为 GitPage 静态网站
19-
# 定义工作运行的环境,这里使用的是最新的 Ubuntu 操作系统
2015
runs-on: ubuntu-latest
21-
22-
# 设置全局环境变量,强制所有 JavaScript Actions 使用 Node.js 20
2316
env:
2417
FORCE_JAVASCRIPT_ACTIONS_TO_NODE20: true
2518

2619
steps:
27-
# Step 1: 检出仓库代码
28-
# 使用 actions/checkout@v4 检出代码库的内容到工作环境中
2920
- name: 1、读取仓库内容
3021
uses: actions/checkout@v4
3122

32-
# Step 2: 设置 Node.js 环境
33-
# 使用 actions/setup-node@v4 安装并配置指定版本的 Node.js 环境
3423
- name: 2、设置 Node.js 环境
3524
uses: actions/setup-node@v4
3625
with:
37-
node-version: 20 # 使用的 Node.js 版本
26+
node-version: 20
3827

39-
# Step 3: 安装 pnpm
40-
# 安装 pnpm,这是一个快速、节省磁盘空间的包管理工具
4128
- name: 3、安装 pnpm
4229
run: npm install -g pnpm
4330

44-
# Step 4: 安装依赖
45-
# 使用 pnpm 安装项目所需的依赖项
46-
- name: 4、安装项目所需的依赖项
31+
- name: 4、安装依赖
4732
run: pnpm install --frozen-lockfile
4833

49-
# Step 5: 构建项目
50-
# 使用 pnpm 运行构建脚本,生成生产环境的构建文件
51-
- name: 5、构建项目
52-
run: pnpm run build
34+
- name: 5、构建项目(使用 .env.github)
35+
run: pnpm run build:github
5336

54-
# Step 6: 部署到 GitHub Pages
55-
# 使用 peaceiris/actions-gh-pages@v4 部署构建文件到 GitHub Pages
5637
- name: 6、部署到 GitHub Pages
5738
uses: peaceiris/actions-gh-pages@v4
5839
with:
59-
github_token: ${{ secrets.TOKEN }} # 使用 GitHub 提供的密钥进行身份验证
60-
publish_dir: dist # 指定构建输出目录,默认为 Vite 项目的 dist 目录
61-
force_orphan: true # 是否使用孤立分支,避免与历史记录冲突
40+
github_token: ${{ secrets.GITHUB_TOKEN }}
41+
publish_dir: dist
42+
force_orphan: true

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"scripts": {
1616
"dev": "vite",
1717
"build:prod": "vue-tsc --noEmit && vite build",
18+
"build:github": "vite build --mode github",
1819
"preview:prod": "pnpm build:prod && vite preview",
1920
"lint:eslint": "eslint --cache --max-warnings 0 \"{src,types}/**/*.{vue,js,jsx,ts,tsx}\" --fix",
2021
"lint:prettier": "prettier --write \"{src,types}/**/*.{vue,js,jsx,ts,tsx,json,css,less,scss,html,md}\"",

vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default ({ mode }: ConfigEnv): UserConfigExport => {
1313
const { VITE_PUBLIC_PATH } = viteEnv;
1414
return {
1515
/** 打包时根据实际情况修改 base */
16-
base: VITE_PUBLIC_PATH,
16+
base: VITE_PUBLIC_PATH || "/",
1717
resolve: {
1818
alias: {
1919
/** @ 符号指向 src 目录 */

0 commit comments

Comments
 (0)