项目 04 · 真页面,自动部署
Vite + TypeScript + Tailwind。GitHub Actions 自动构建、自动部署。每次 push 都更新线上版本。
怎么算"成"?
有一个真上线的网址;每次 push 后线上自动更新;同事/同学打开后能直接用。
步骤 1 · 初始化项目
npm create vite@latest my-site -- --template vanilla-ts cd my-site npm install npm run dev # 本地预览
步骤 2 · 写真页面
不要 hello world。写一个你真要用的页面 —— 一个学习卡片应用、一个个人小工具、一个游戏的关卡查看器。具体到家人/同学会用的程度。
步骤 3 · 配自动部署
# .github/workflows/deploy.yml
name: Deploy
on: { push: { branches: [main] } }
permissions: { contents: read, pages: write, id-token: write }
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: '20' }
- run: npm ci && npm run build
- uses: actions/upload-pages-artifact@v3
with: { path: dist }
deploy:
needs: build
runs-on: ubuntu-latest
environment: github-pages
steps:
- uses: actions/deploy-pages@v4
步骤 4 · 启用 GitHub Pages
仓库 Settings → Pages → Source: GitHub Actions。等几分钟,你的网址就有了:https://yourname.github.io/my-site/