Refactor frontend build process in makefile and add fly deployment co… #5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See https://fly.io/docs/app-guides/continuous-deployment-with-github-actions/ | |
# 工作流名称 | |
name: Fly Deploy | |
# 触发条件:当 main 分支收到推送时 | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
deploy: | |
name: Deploy app | |
# 指定运行环境为最新版本的 Ubuntu | |
runs-on: ubuntu-latest | |
# 确保同一时间只运行一个部署任务,避免并发冲突 | |
concurrency: deploy-group | |
steps: | |
# 步骤1: 检出代码 | |
- uses: actions/checkout@v4 | |
# 步骤2: 设置 Node.js 环境 | |
- uses: actions/setup-node@v4 | |
with: | |
node-version: "20" # 使用 Node.js 20.x 版本 | |
# 步骤3: 安装 pnpm 包管理器 | |
- uses: pnpm/action-setup@v3 | |
with: | |
version: 9 # 使用 pnpm 9.x 版本 | |
# 步骤4: 配置 pnpm 缓存 | |
- name: Get pnpm store directory | |
id: pnpm-cache | |
shell: bash | |
run: | | |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_OUTPUT | |
# 步骤5: 设置依赖缓存 | |
- uses: actions/cache@v4 | |
name: Setup pnpm cache | |
with: | |
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }} | |
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} | |
restore-keys: | | |
${{ runner.os }}-pnpm-store- | |
# 步骤6: 构建前端应用 | |
# 这一步很重要,因为后端需要嵌入构建后的静态文件 | |
- name: Build Frontend | |
run: | | |
cd svelte | |
pnpm install --no-frozen-lockfile | |
pnpm run build | |
# 在部署前创建 .env 文件 | |
- name: Create .env file | |
run: | | |
cat << EOF > .env | |
FRONTEND_STATIC_PATH=${{ secrets.FRONTEND_STATIC_PATH }} | |
PG_HOST=${{ secrets.PG_HOST }} | |
PG_PORT=${{ secrets.PG_PORT }} | |
PG_USER=${{ secrets.PG_USER }} | |
PG_PASS=${{ secrets.PG_PASS }} | |
PG_BASE=${{ secrets.PG_BASE }} | |
PG_SSLMODE=${{ secrets.PG_SSLMODE }} | |
REDIS_HOST=${{ secrets.REDIS_HOST }} | |
REDIS_PORT=${{ secrets.REDIS_PORT }} | |
REDIS_PASS=${{ secrets.REDIS_PASS }} | |
REDIS_BASE=${{ secrets.REDIS_BASE }} | |
JWT_SECRET=${{ secrets.JWT_SECRET }} | |
CSRF_SECRET=${{ secrets.CSRF_SECRET }} | |
EOF | |
# 步骤7: 设置 Fly.io CLI | |
- uses: superfly/flyctl-actions/setup-flyctl@master | |
# 步骤8: 部署到 Fly.io | |
# --remote-only 表示在 Fly.io 的服务器上构建,而不是本地构建 | |
- run: flyctl deploy --remote-only | |
env: | |
# 使用 GitHub Secrets 中存储的 Fly.io API 令牌 | |
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} |