02 Github 部署
创建 GitHub Actions 配置文件
自动部署(推荐)
创建文件:\src.github\workflows\deploy.yml
name: Deploy to GitHub Pages
on: push: branches: [main] # 当推送到 main 分支时触发
jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 - name: Install dependencies run: npm ci - name: Build website run: npm run build - name: Deploy to GitHub Pages uses: peaceiris/actions-gh-pages@v3 with: github_token: ${{ secrets.GITHUB_TOKEN }} publish_dir: ./dist
手动部署
# 1. 构建项目npm run build
# 2. 进入 dist 文件夹cd dist
# 3. 初始化 gitgit initgit add .git commit -m "部署文档"
# 4. 推送到 gh-pages 分支git push -f [email protected]:用户名/仓库名.git main:gh-pages
使用自动部署的好处:
- 推送代码后自动构建和部署
- 不需要手动操作
- 更不容易出错