部署到 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).
示例:
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_SSH | Set 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_USER | The username for a GitHub account that has push access to the deployment repo. 对于你自己的仓库,这一般会是你自己的 GitHub 用户名。 不使用 SSH 时必填,使用 SSH 时则会被忽略。 |
GIT_PASS | Personal 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_NAME | The git config user.name value to use when pushing to the deployment repo |
GIT_USER_EMAIL | The 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 上,请运行:
- Bash
- Windows
- PowerShell
GIT_USER=<GITHUB_USERNAME> yarn deploy
cmd /C "set "GIT_USER=<GITHUB_USERNAME>" && yarn deploy"
cmd /C 'set "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.
我们的目标是:
- 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 calledtest-deploy. - When a pull request is merged to the
mainbranch or someone pushes to themainbranch directly, it will be built and deployed to GitHub Pages. This job will be calleddeploy.
下面是两种通过 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-pagesbranch.
- Same
- Remote
While you can have both jobs defined in the same workflow file, the original 添加这两个工作流文件: 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.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
跨仓库的发布更难设置,因为你需要经过权限检查才能推送到另一仓库。 我们会使用 SSH 完成身份验证。
- Generate a new SSH key. 因为这个 SSH 密钥会用在 CI 中,所以不能输入任何密码。
- By default, your public key should have been created in
~/.ssh/id_rsa.pub; otherwise, use the name you've provided in the previous step to add your key to GitHub deploy keys. - Copy the key to clipboard with
pbcopy \< ~/.ssh/id_rsa.puband paste it as a deploy key in the deployment repository. 如果命令行不适合,可以手动复制文件内容。 Check the box forAllow write accessbefore saving your deployment key. - You'll need your private key as a GitHub secret to allow Docusaurus to run the deployment for you.
- Copy your private key with
pbcopy \< ~/.ssh/id_rsaand paste a GitHub secret with the nameGH_PAGES_DEPLOYon your source repository. 如果命令行不适合,可以手动复制文件内容。 保存你的 secret。 - Create your documentation workflow in the
.github/workflows/directory. In this example it's thedeploy.ymlfile.
至此,你应该已经:
- 通过把 SSH 私钥作为其 Github Secret,设置了具有 GitHub 工作流的源仓库
- 通过把 SSH 公钥添加到 GitHub 部署密钥中,设置了你的部署仓库
GitHub action file
Please make sure that you replace actions@github.com with your GitHub email and gh-actions with your name.
此文件假定你使用的是 Yarn。 If you use npm, change cache: yarn, yarn install --frozen-lockfile, yarn build to cache: npm, npm ci, npm run build accordingly.
name: Deploy to GitHub Pages
on:
pull_request:
branches: [main]
push:
branches: [main]
permissions:
contents: write
jobs:
test-deploy:
if: github.event_name != 'push'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version: 24
cache: yarn
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Test build website
run: yarn build
deploy:
if: github.event_name != 'pull_request'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version: 24
cache: yarn
- uses: webfactory/ssh-agent@v0.5.0
with:
ssh-private-key: ${{ secrets.GH_PAGES_DEPLOY }}
- name: Deploy to GitHub Pages
env:
USE_SSH: true
run: |
git config --global user.email "actions@github.com"
git config --global user.name "gh-actions"
yarn install --frozen-lockfile
yarn deploy
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/workflowsfolder, and that the trigger condition is set correctly (e.g., if your default branch is "master" instead of "main", you need to change theon.pushproperty). - 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-pagesas the deployment branch.
如果你正在使用自定义域名:
- 如果你正在使用自定义域名,请验证你是否设置了正确的 DNS 记录。 See GitHub pages documentation on configuring custom domains. 此外,请注意,DNS 修改可能需要24小时才能通过互联网传播(注:即 DNS 解析生效)。