部署方法
vue项目部署到github pages上
主要结合了vite官方文档中生产部署这一节
文档中介绍的比较粗略,我打算把部署过程详细介绍:
1. 在 vite.config.js
中设置正确的 base
。
如果你要部署在 `https://<USERNAME>.github.io/` 上,你可以省略 `base` 使其默认为 `'/'`。
如果你要部署在 `https://<USERNAME>.github.io/<REPO>/` 上,例如你的仓库地址为 `https://github.com/<USERNAME>/<REPO>`,那么请设置 `base` 为 `'/<REPO>/'`。
例如我的项目名为为littleRabbit,就在配置文件中配置如下,配置完记得git push一下哦
2. 进入仓库 settings 页面的 GitHub Pages 配置,选择部署来源为“GitHub Actions”
3. 接下来,会让你选择创建一个workflow文件,直接
选择自己创建
然后命名为main.yml
之后将vite生产部署文档中的代码粘贴到其中即可
# 将静态内容部署到 GitHub Pages 的简易工作流程
name: Deploy static content to Pages
on:
# 仅在推送到默认分支时运行。
push:
branches: ['main']
# 这个选项可以使你手动在 Action tab 页面触发工作流
workflow_dispatch:
# 设置 GITHUB_TOKEN 的权限,以允许部署到 GitHub Pages。
permissions:
contents: read
pages: write
id-token: write
# 允许一个并发的部署
concurrency:
group: 'pages'
cancel-in-progress: true
jobs:
# 单次部署的工作描述
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Node
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'npm'
- name: Install dependencies
run: npm install
- name: Build
run: npm run build
- name: Setup Pages
uses: actions/configure-pages@v3
- name: Upload artifact
uses: actions/upload-pages-artifact@v1
with:
# Upload dist repository
path: './dist'
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
提交代码commit changes, 记得本地也可以pull更新代码
之后切换到actions页面,会看到正在部署中,等待片刻即部署成功!!
点击链接即可访问,如果都做的没错,刚开始也可能会404,等待一会就好了!
🦀🦀