Deploy your Astro Site to GitHub Pages
You can use GitHub Pages to host a static, prerendered Astro website directly from a repository on GitHub.com using GitHub Actions.
How to deploy
Section titled “How to deploy”Astro maintains an official Astro GitHub Action to deploy your project to a GitHub Pages with very little configuration and is the recommended way to deploy to GitHub Pages.
Follow the instructions below to use the GitHub Action to deploy your Astro site to GitHub Pages. This will create a website from your repository at a GitHub URL (e.g. https://<username>.github.io/<my-repo>
). Once deployed, you can optionally configure a custom domain to deploy your GitHub Pages site at your preferred domain (e.g. https://example.com
).
-
Create a new file in your project at
.github/workflows/deploy.yml
and paste in the YAML below.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)# env:# PUBLIC_POKEAPI: 'https://pokeapi.co/api/v2' # Use single quotation marks for the variable value. (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@v4The Astro action can be configured with optional inputs. Provide these by uncommenting the
with:
line and the input you want to use.If your site requires any public environment variables, uncomment the
env:
line and add them there. (See the GitHub documentation on setting secrets for adding private environment variables.)The official Astro action scans for a lockfile to detect your preferred package manager (
npm
,yarn
,pnpm
, orbun
). You should commit your package manager’s automatically generatedpackage-lock.json
,yarn.lock
,pnpm-lock.yaml
, orbun.lockb
file to your repository. -
In your Astro config file, set
site
to the GitHub URL of your deployed site.astro.config.mjs import { defineConfig } from 'astro/config'export default defineConfig({site: 'https://astronaut.github.io',})The value for
site
must be one of the following:- The following URL based on your username:
https://<username>.github.io
- The random URL autogenerated for a GitHub Organization’s private page:
https://<random-string>.pages.github.io/
- The following URL based on your username:
-
In
astro.config.mjs
, configure a value forbase
(usually required).GitHub Pages will publish your website at an address that depends on both your username and your repository name (e.g.
https://<username>/github.io/<my-repo>/
). Set a value forbase
that specifies the repository for your website. This is so that Astro understands your website’s root is/my-repo
, rather than the default/
. You can skip this if your repository name matches the special<username>.github.io
pattern (e.g.https://github.com/username/username.github.io/
)Configure
base
as the repository’s name starting with a forward slash ( e.g./my-repo
):astro.config.mjs import { defineConfig } from 'astro/config'export default defineConfig({site: 'https://astronaut.github.io',base: '/my-repo',})When this value is configured, all of your internal page links must be prefixed with your
base
value:<a href="/my-repo/about">About</a>See more about configuring a
base
value. -
On GitHub, go to your repository’s Settings tab and find the Pages section of the settings.
-
Choose GitHub Actions as the Source of your site.
When you push changes to your Astro project’s repository, the GitHub Action will automatically deploy them for you at your GitHub URL.
Change your GitHub URL to a custom domain
Section titled “Change your GitHub URL to a custom domain”Once your Astro project is deployed to GitHub pages at a GitHub URL following the previous instructions, you can configure a custom domain. This means that users can visit your site at your custom domain https://example.com
instead of https://<username>.github.io
.
-
Add a
./public/CNAME
record to your project.Create the following file in your
public/
folder with a single line of text that specifies your custom domain:public/CNAME sub.example.comThis will deploy your site at your custom domain instead of
user.github.io
. -
In your Astro config, update the value for
site
with your custom domain. Do not set a value forbase
, and remove one if it exists:astro.config.mjs import { defineConfig } from 'astro/config'export default defineConfig({site: 'https://example.com',base: '/my-repo'}) -
If necessary, update all your page internal links to remove the
base
prefix:<a href="/my-repo/about">About</a>
Examples
Section titled “Examples”- Github Pages Deployment starter template
- Starlight Flexoki Theme (production site)
- Expressive Code Color Chips (production site)
- Starlight Markdown Blocks (production site)