Deploying to GitHub Pages
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';Docusaurus provides an easy way to publish to GitHub Pages, which comes free with every GitHub repository.
Overview
Section titled “Overview”Usually, there are two repositories (at least two branches) involved in a publishing process: the branch containing the source files, and the branch containing the build output to be served with GitHub Pages. In the following tutorial, they will be referred to as "source" and "deployment", respectively.
Each GitHub repository is associated with a GitHub Pages service. 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/.
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
Section titled “docusaurus.config.js settings”First, modify your docusaurus.config.js and add the following params:
| Name | Description |
|---|---|
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.
Example:
export default { // ... url: 'https://endiliey.github.io', // Your website URL baseUrl: '/', projectName: 'endiliey.github.io', organizationName: 'endiliey', trailingSlash: false, // ...};Environment settings
Section titled “Environment settings”| Name | Description |
|---|---|
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. For your own repositories, this will usually be your GitHub username. Required if not using SSH, and ignored otherwise. |
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:
| Name | Description |
|---|---|
GITHUB_HOST |
The domain name of your GitHub enterprise site. |
GITHUB_PORT |
The port of your GitHub enterprise site. |
Deploy
Section titled “Deploy”Finally, to deploy your site to GitHub Pages, run:
<Tabs>
<TabItem value="bash" label="Bash">GIT_USER=<GITHUB_USERNAME> yarn deploy</TabItem>
<TabItem value="windows" label="Windows">cmd /C "set "GIT_USER=<GITHUB_USERNAME>" && yarn deploy"</TabItem>
<TabItem value="powershell" label="PowerShell">cmd /C 'set "GIT_USER=<GITHUB_USERNAME>" && yarn deploy'</TabItem>
</Tabs>Triggering deployment with GitHub Actions
Section titled “Triggering deployment with 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.
<Tabs>
<TabItem value="same" label="Same">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. We therefore propose to manage them as separate workflows instead.
GitHub action files
Add these two workflow files:
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@v4name: 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 buildname: 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: yarn
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Build website
run: yarn 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@v4name: 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: yarn
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Test build website
run: yarn build</TabItem>
<TabItem value="remote" label="Remote">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
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</TabItem>
</Tabs>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.