GitHub Pagesへのデプロイ
Docusaurus provides an easy way to publish to GitHub Pages, which comes free with every GitHub repository.
概要
通常、公開プロセスには2つのリポジトリ (少なくとも2つのブランチ) が関与しています。ソースファイルを含むブランチと、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. This branch will be called the "deployment branch".
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 | The GitHub user or organization that owns the deployment repository. |
projectName | The name of the deployment repository. |
deploymentBranch | The name of the deployment branch. It defaults to 'gh-pages' for non-organization GitHub Pages repos (projectName not ending in .github.io). Otherwise, it needs to be explicit as a config field or environment variable. |
These fields also have their environment variable counterparts which have a higher priority: ORGANIZATION_NAME, PROJECT_NAME, and DEPLOYMENT_BRANCH.
GitHub Pagesでは、DocusaurusのURLにデフォルトで末尾にスラッシュを追加します。 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を使用しない場合に必須で、それ以外の場合は無視されます。 |
GIT_PASS | Personal access token of the git user (specified by GIT_USER), to facilitate non-interactive deployment (e.g. continuous deployment) |
CURRENT_BRANCH | The source 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 enterprise installations should work in the same manner as github.com; you only need to set the organization's GitHub Enterprise host as an environment variable:
| 名前 | 説明 |
|---|---|
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. When GitHub prompts for your password, enter the PAT instead. 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.
Our goal is that:
- 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.
Here are two approaches to deploying your docs with GitHub Actions. Based on the location of your deployment repository, choose the relevant tab below:
- 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 Add these two workflow files: 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. We therefore propose to manage them as separate workflows instead.GitHub action files
A cross-repo publish is more difficult to set up because you need to push to another repo with permission checks. We will be using SSH to do the authentication.
- Generate a new SSH key. Since this SSH key will be used in CI, make sure to not enter any passphrase.
- 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. Copy the file content if the command line doesn't work for you. 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. Copy the file content if the command line doesn't work for you. Save your secret. - Create your documentation workflow in the
.github/workflows/directory. In this example it's thedeploy.ymlfile.
At this point, you should have:
- the source repo with the GitHub workflow set with the private SSH key as the GitHub Secret, and
- your deployment repo set with the public SSH key in GitHub Deploy Keys.
GitHub action file
Please make sure that you replace actions@github.com with your GitHub email and gh-actions with your name.
This file assumes you are using 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?
After pushing to main, if you don't see your site published at the desired location (for example, it says "There isn't a GitHub Pages site here", or it's showing your repo's README.md file), try the following:
- Wait about three minutes and refresh. It may take a few minutes for GitHub pages to pick up the new files.
- Check your repo's landing page for a little green tick next to the last commit's title, indicating the CI has passed. If you see a cross, it means the build or deployment failed, and you should check the log for more debugging information.
- Click on the tick and make sure you see a "Deploy to GitHub Pages" workflow. Names like "pages build and deployment / deploy" are GitHub's default workflows, indicating your custom deployment workflow failed to be triggered at all. 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.
If you are using a custom domain:
- Verify that you have the correct DNS records set up if you're using a custom domain. See GitHub pages documentation on configuring custom domains. Also, please be aware that it may take up to 24 hours for DNS changes to propagate through the internet.