跳到主要内容
版本:Canary 🚧

部署到 GitHub Pages

Docusaurus provides an easy way to publish to GitHub Pages, which comes free with every GitHub repository.

概览

通常,发布过程会涉及到两个仓库(至少是两个分支):包含源文件的分支,以及包含将使用 Github Pages 提供(给用户)的构建输出的分支。 In the following tutorial, they will be referred to as "source" and "deployment", respectively.

每个 GitHub 仓库都关联有一个 GitHub Pages 服务。 If the deployment repository is called my-org/my-project (where my-org is the organization name or username), the deployed site will appear at https://my-org.github.io/my-project/. If the deployment repository is called my-org/my-org.github.io (the organization GitHub Pages repo), the site will appear at https://my-org.github.io/.

信息

In case you want to use your custom domain for GitHub Pages, create a CNAME file in the static directory. Anything within the static directory will be copied to the root of the build directory for deployment. When using a custom domain, you should be able to move back from baseUrl: '/projectName/' to baseUrl: '/', and also set your url to your custom domain.

You may refer to GitHub Pages' documentation User, Organization, and Project Pages for more details.

GitHub Pages picks up deploy-ready files (the output from docusaurus build) from the default branch (master / main, usually) or the gh-pages branch, and either from the root or the /docs folder. You can configure that through Settings > Pages in your repository. 这个分支会被称作「部署分支」。

We provide a docusaurus deploy command that helps you deploy your site from the source branch to the deployment branch in one command: clone, build, and commit.

docusaurus.config.js settings

First, modify your docusaurus.config.js and add the following params:

参数描述
organizationName拥有部署仓库的 GitHub 用户或组织。
projectName部署仓库的名字。
deploymentBranch部署分支的名称。 It defaults to 'gh-pages' for non-organization GitHub Pages repos (projectName not ending in .github.io). 否则,它需要通过配置字段或环境变量显式定义。

These fields also have their environment variable counterparts which have a higher priority: ORGANIZATION_NAME, PROJECT_NAME, and DEPLOYMENT_BRANCH.

警告

GitHub Pages 默认为 Docusaurus 网址链接添加末尾斜杠。 It is recommended to set a trailingSlash config (true or false, not undefined).

示例:

docusaurus.config.js
export default {
// ...
url: 'https://endiliey.github.io', // Your website URL
baseUrl: '/',
projectName: 'endiliey.github.io',
organizationName: 'endiliey',
trailingSlash: false,
// ...
};
警告

By default, GitHub Pages runs published files through Jekyll. Since Jekyll will discard any files that begin with _, it is recommended that you disable Jekyll by adding an empty file named .nojekyll file to your static directory.

环境设置

参数描述
USE_SSHSet to true to use SSH instead of the default HTTPS for the connection to the GitHub repo. If the source repo URL is an SSH URL (e.g. git@github.com:facebook/docusaurus.git), USE_SSH is inferred to be true.
GIT_USERThe username for a GitHub account that has push access to the deployment repo. 对于你自己的仓库,这一般会是你自己的 GitHub 用户名。 不使用 SSH 时必填,使用 SSH 时则会被忽略。
GIT_PASSPersonal access token of the git user (specified by GIT_USER), to facilitate non-interactive deployment (e.g. continuous deployment)
CURRENT_BRANCH源分支。 Usually, the branch will be main or master, but it could be any branch except for gh-pages. If nothing is set for this variable, then the current branch from which docusaurus deploy is invoked will be used.
GIT_USER_NAMEThe git config user.name value to use when pushing to the deployment repo
GIT_USER_EMAILThe git config user.email value to use when pushing to the deployment repo

GitHub 企业安装版应该和 github.com 的工作方式一致。你只需要在环境变量中设置组织的 GitHub 企业主机即可。

参数描述
GITHUB_HOST你的 GitHub 企业网站的域名。
GITHUB_PORT你的 GitHub 企业网站的端口。

部署

最后,要把你的网站部署到 GitHub Pages 上,请运行:

GIT_USER=<GITHUB_USERNAME> yarn deploy
警告

Beginning in August 2021, GitHub requires every command-line sign-in to use the personal access token instead of the password. 当 GitHub 提示你输入密码时,请输入个人访问令牌。 See the GitHub documentation for more information.

Alternatively, you can use SSH (USE_SSH=true) to log in.

触发 GitHub Actions 部署

GitHub Actions allow you to automate, customize, and execute your software development workflows right in your repository.

The workflow examples below assume your website source resides in the main branch of your repository (the source branch is main), and your publishing source is configured for publishing with a custom GitHub Actions Workflow.

我们的目标是:

  1. When a new pull request is made to main, there's an action that ensures the site builds successfully, without actually deploying. This job will be called test-deploy.
  2. When a pull request is merged to the main branch or someone pushes to the main branch directly, it will be built and deployed to GitHub Pages. This job will be called deploy.

下面是两种通过 GitHub Actions 部署文档的方法。 根据你的部署仓库的位置,选择下面相应的选项卡:

  • Source repo and deployment repo are the same repository.
  • The deployment repo is a remote repository, different from the source. Instructions for this scenario assume publishing source is the gh-pages branch.

While you can have both jobs defined in the same workflow file, the original deploy workflow will always be listed as skipped in the PR check suite status, which is not indicative of the actual status and provides no value to the review process. 所以,我们建议把它们作为单独的工作流来管理。

GitHub action files

添加这两个工作流文件:

Tweak the parameters for your setup

If your Docusaurus project is not at the root of your repo, you may need to configure a default working directory, and adjust the paths accordingly.

.github/workflows/deploy.yml
name: Deploy to GitHub Pages

on:
push:
branches:
- main
# Review gh actions docs if you want to further define triggers, paths, etc
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#on

jobs:
build:
name: Build Docusaurus
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version: 24
cache: npm

- name: Install dependencies
run: npm ci
- name: Build website
run: npm run build

- name: Upload Build Artifact
uses: actions/upload-pages-artifact@v3
with:
path: build

deploy:
name: Deploy to GitHub Pages
needs: build

# Grant GITHUB_TOKEN the permissions required to make a Pages deployment
permissions:
pages: write # to deploy to Pages
id-token: write # to verify the deployment originates from an appropriate source

# Deploy to the github-pages environment
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}

runs-on: ubuntu-latest
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
.github/workflows/test-deploy.yml
name: Test deployment

on:
pull_request:
branches:
- main
# Review gh actions docs if you want to further define triggers, paths, etc
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#on

jobs:
test-deploy:
name: Test deployment
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version: 24
cache: npm

- name: Install dependencies
run: npm ci
- name: Test build website
run: npm run build
Site not deployed properly?

在推送到 main 分支后,如果你没有看到你的站点被发布到想要的位置(比方说,访问后发现“这里没有 Github Pages 站点”,或是显示你仓库里的 README.md 文件),尝试以下步骤:

  • 等3分钟,然后刷新一下。 GitHub 页面可能需要几分钟时间才能接收到新文件。
  • 检查你仓库的入口页上次提交的标题旁是否有个绿色的小对勾。如果有,那就表明 CI 通过了。 如果你看到的是一个叉,那就说明构建或者部署失败了,你应该检查日志以获取更多调试信息。
  • 点击对勾,确保你看到的是“部署到 Github Pages”工作流。 如果你看到的名称是类似“pages build and deployment / deploy”(即“页面构建和部署”)这样的,这就表明你的自定义部署工作流根本无法触发。 Make sure the YAML files are placed under the .github/workflows folder, and that the trigger condition is set correctly (e.g., if your default branch is "master" instead of "main", you need to change the on.push property).
  • Under your repo's Settings > Pages, make sure the "Source" (which is the source for the deployment files, not "source" as in our terminology) is set to "gh-pages" + "/ (root)", since we are using gh-pages as the deployment branch.

如果你正在使用自定义域名: