跳转到内容

将你的 Astro 网站部署到 GitHub Pages

你可以使用 GitHub Pages 直接从 GitHub.com 上的仓库托管 Astro 网站。

你可以使用 GitHub Actions 自动构建和部署你的 Astro 网站到 GitHub Pages。为此,你的源代码必须托管在 GitHub 上。

Astro 维护了官方的 withastro/action,只需很少的配置即可部署你的项目。请按照以下说明将你的 Astro 网站部署到 GitHub pages,如果需要更多信息,请参阅该包的 README

astro.config.mjs 中设置 sitebase 选项。

astro.config.mjs
import { defineConfig } from 'astro/config'
export default defineConfig({
site: 'https://astronaut.github.io',
base: 'my-repo',
})

site 的值必须是以下之一

  • 基于你用户名的 URL:https://<username>.github.io
  • GitHub 组织的私有页面 自动生成的随机 URL:https://<random-string>.pages.github.io/

可能需要为 base 设置一个值,这样 Astro 就会将你的仓库名称(例如 /my-repo)视为你网站的根目录。

base 的值应该是你的仓库名称,并以正斜杠开头,例如 /my-blog。这样 Astro 就能理解你网站的根目录是 /my-repo,而不是默认的 /

要配置 Astro 以使用带有自定义域名的 GitHub Pages,请将你的域名设置为 site 的值。不要为 base 设置值

astro.config.mjs
import { defineConfig } from 'astro/config'
export default defineConfig({
site: 'https://example.com',
})
  1. 在你的项目中于 .github/workflows/deploy.yml 位置创建一个新文件,并粘贴以下 YAML 内容。

    deploy.yml
    name: Deploy to GitHub Pages
    on:
    # Trigger the workflow every time you push to the `main` branch
    # Using a different branch name? Replace `main` with your branch’s name
    push:
    branches: [ main ]
    # Allows you to run this workflow manually from the Actions tab on GitHub.
    workflow_dispatch:
    # Allow this job to clone the repo and create a page deployment
    permissions:
    contents: read
    pages: write
    id-token: write
    jobs:
    build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout your repository using git
    uses: actions/checkout@v4
    - name: Install, build, and upload your site
    uses: withastro/action@v3
    # with:
    # path: . # The root location of your Astro project inside the repository. (optional)
    # node-version: 20 # The specific version of Node that should be used to build your site. Defaults to 20. (optional)
    # package-manager: pnpm@latest # The Node package manager that should be used to install dependencies and build your site. Automatically detected based on your lockfile. (optional)
    deploy:
    needs: build
    runs-on: ubuntu-latest
    environment:
    name: github-pages
    url: ${{ steps.deployment.outputs.page_url }}
    steps:
    - name: Deploy to GitHub Pages
    id: deployment
    uses: actions/deploy-pages@v4
  2. (可选)如果你在本地开发或预览构建时向 Astro 项目传递环境变量,你需要在 deploy.yml 文件中定义任何公共变量,以便在部署到 GitHub Pages 时处理它们。(有关添加私有环境变量,请参阅 GitHub 关于设置机密的文档。)

    deploy.yml
    jobs:
    build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout your repository using git
    uses: actions/checkout@v4
    - name: Install, build, and upload your site
    uses: withastro/action@v3
    env:
    # Use single quotation marks for the variable value
    PUBLIC_EVM_WALLET_ADDRESS: '0x4bFc229A40d41698154336aFF864f61083E76659'
  3. 在 GitHub 上,转到你的仓库的 Settings 选项卡,然后找到设置中的 Pages 部分。

  4. 选择 GitHub Actions 作为你网站的 Source

  5. 提交新的工作流文件并将其推送到 GitHub。

你的网站现在应该已经发布了!当你将更改推送到你的 Astro 项目仓库时,GitHub Action 会自动为你部署它们。

更多部署指南

贡献 社区 赞助