627 字
3 分钟
使用 GitHub Actions 构建并部署 Astro 项目
本文基于以下工作流进行构建与部署:
name: Build, Check and Deploy
on:
# 每次推送到 `main` 分支时触发这个"工作流程"
push:
branches: [ main ]
pull_request:
branches: [ main ]
# 允许你在 GitHub 上的 Actions 标签中手动触发此"工作流程"
workflow_dispatch:
# 只需要读取权限
permissions:
contents: read
# 避免同时运行多个相同工作流
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
# 代码质量检查
quality:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Setup Biome
uses: biomejs/setup-biome@f382a98e582959e6aaac8e5f8b17b31749018780 # v2.5.0
with:
version: latest
- name: Format code with Biome
run: biome format --write ./src
continue-on-error: true
- name: Run Biome check
run: biome ci ./src --reporter=github
continue-on-error: true # 添加这一行使格式检查失败不会阻止工作流
# Astro 类型检查 - 多 Node.js 版本矩阵测试
check:
strategy:
matrix:
node: [ 22, 23 ]
# 即使某个版本测试失败,也继续测试其他版本
fail-fast: false
runs-on: ubuntu-latest
needs: quality
name: Astro Check for Node.js ${{ matrix.node }}
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Setup Node.js
uses: actions/setup-node@cdca7365b2dadb8aad0a33bc7601856ffabcc48e # v4.3.0
with:
node-version: ${{ matrix.node }}
- name: Setup pnpm
uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0
with:
run_install: false
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Run Astro Check
run: pnpm astro check
# 构建站点 - 多 Node.js 版本矩阵测试
build:
strategy:
matrix:
node: [ 22, 23 ]
fail-fast: false
runs-on: ubuntu-latest
needs: check
name: Astro Build for Node.js ${{ matrix.node }}
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Setup Node.js
uses: actions/setup-node@cdca7365b2dadb8aad0a33bc7601856ffabcc48e # v4.3.0
with:
node-version: ${{ matrix.node }}
- name: Setup pnpm
uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0
with:
run_install: false
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Build site
run: pnpm astro build
# 将构建产物上传为 artifact,以便部署任务使用
- name: Upload build artifact
uses: actions/upload-artifact@v4
with:
name: dist-node-${{ matrix.node }}
path: dist
retention-days: 1
# 部署到外部仓库 - 仅使用 Node.js 22 的构建结果
deploy:
if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request'
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
# 下载 Node.js 22 版本的构建产物
- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: dist-node-22
path: dist
- name: Deploy to Your GitHub Pages
uses: JamesIves/github-pages-deploy-action@v4
with:
token: ${{ secrets.DEPLOY_KEY }} # 需要在 GitHub 仓库的 Secrets 中设置一个名为 DEPLOY_KEY 的密钥
repository-name: your-username/your-repo-name # 替换为你的 GitHub 用户名和仓库名
branch: main
folder: dist
commit-message: "⭐Site updated: ${{ github.event.head_commit.timestamp }} ${{ github.event.head_commit.message }}"
# single-commit: false
工作流说明
- quality: 使用 Biome 对代码进行检查与格式化
- check: 基于多节点版本进行 Astro 类型检查
- build: 基于多节点版本生成产物并打包为 artifact
- deploy: 从 artifact 下载构建结果,再部署至目标仓库
通过以上步骤,可在推送到指定分支后自动完成检查、构建和部署的流程。
使用 GitHub Actions 构建并部署 Astro 项目
https://newpower.pro/posts/document/github-actions-astro-build/