将你的 Astro 网站部署到 GitHub Pages
你可以使用 GitHub Pages 直接从 GitHub.com 上的仓库托管 Astro 网站。
如何部署
标题为“如何部署”的部分你可以使用 GitHub Actions 自动构建和部署你的 Astro 网站到 GitHub Pages。为此,你的源代码必须托管在 GitHub 上。
Astro 维护了官方的 withastro/action
,只需很少的配置即可部署你的项目。请按照以下说明将你的 Astro 网站部署到 GitHub pages,如果需要更多信息,请参阅该包的 README。
为 GitHub Pages 配置 Astro
标题为“为 GitHub Pages 配置 Astro”的部分部署到 github.io
URL
标题为“部署到 github.io URL”的部分在 astro.config.mjs
中设置 site
和 base
选项。
import { defineConfig } from 'astro/config'
export default defineConfig({ site: 'https://astronaut.github.io', base: 'my-repo',})
site
标题为“site”的部分site
的值必须是以下之一
- 基于你用户名的 URL:
https://<username>.github.io
- 为 GitHub 组织的私有页面 自动生成的随机 URL:
https://<random-string>.pages.github.io/
base
标题为“base”的部分可能需要为 base
设置一个值,这样 Astro 就会将你的仓库名称(例如 /my-repo
)视为你网站的根目录。
如果出现以下情况,请不要设置 base
参数
- 你的页面从根文件夹提供服务。
- 你的仓库位于
https://github.com/<USERNAME>/<USERNAME>.github.io
。
base
的值应该是你的仓库名称,并以正斜杠开头,例如 /my-blog
。这样 Astro 就能理解你网站的根目录是 /my-repo
,而不是默认的 /
。
使用带有自定义域名的 GitHub Pages
标题为“使用带有自定义域名的 GitHub Pages”的部分你可以通过在项目中添加 ./public/CNAME
文件来设置自定义域名
sub.mydomain.com
这会将你的网站部署到你的自定义域名,而不是 user.github.io
。不要忘记还要为你的域名提供商配置 DNS。
要配置 Astro 以使用带有自定义域名的 GitHub Pages,请将你的域名设置为 site
的值。不要为 base
设置值
import { defineConfig } from 'astro/config'
export default defineConfig({ site: 'https://example.com',})
配置 GitHub Action
标题为“配置 GitHub Action”的部分-
在你的项目中于
.github/workflows/deploy.yml
位置创建一个新文件,并粘贴以下 YAML 内容。deploy.yml name: Deploy to GitHub Pageson:# Trigger the workflow every time you push to the `main` branch# Using a different branch name? Replace `main` with your branch’s namepush: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 deploymentpermissions:contents: readpages: writeid-token: writejobs:build:runs-on: ubuntu-lateststeps:- name: Checkout your repository using gituses: actions/checkout@v4- name: Install, build, and upload your siteuses: 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: buildruns-on: ubuntu-latestenvironment:name: github-pagesurl: ${{ steps.deployment.outputs.page_url }}steps:- name: Deploy to GitHub Pagesid: deploymentuses: actions/deploy-pages@v4Astro action 接受一些可选的输入。你可以通过取消注释
with:
行以及你想要使用的输入来提供它们。官方的 Astro action 会扫描锁定文件以检测你首选的包管理器(
npm
、yarn
、pnpm
或bun
)。你应该将包管理器自动生成的package-lock.json
、yarn.lock
、pnpm-lock.yaml
或bun.lockb
文件提交到你的仓库中。 -
(可选)如果你在本地开发或预览构建时向 Astro 项目传递环境变量,你需要在
deploy.yml
文件中定义任何公共变量,以便在部署到 GitHub Pages 时处理它们。(有关添加私有环境变量,请参阅 GitHub 关于设置机密的文档。)deploy.yml jobs:build:runs-on: ubuntu-lateststeps:- name: Checkout your repository using gituses: actions/checkout@v4- name: Install, build, and upload your siteuses: withastro/action@v3env:# Use single quotation marks for the variable valuePUBLIC_EVM_WALLET_ADDRESS: '0x4bFc229A40d41698154336aFF864f61083E76659' -
在 GitHub 上,转到你的仓库的 Settings 选项卡,然后找到设置中的 Pages 部分。
-
选择 GitHub Actions 作为你网站的 Source。
-
提交新的工作流文件并将其推送到 GitHub。
你的网站现在应该已经发布了!当你将更改推送到你的 Astro 项目仓库时,GitHub Action 会自动为你部署它们。