🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Deployment••By Komal Nakrani

Deploy Astro to GitHub Pages: Step-by-Step Guide

Deploy Astro to GitHub Pages with the official withastro/action workflow: set site and base, fix broken asset paths, add a custom domain, and debug failed builds.

#Astro#GitHub Pages#Deployment#GitHub Actions

To deploy Astro to GitHub Pages, set site (and base for a project repo) in astro.config.mjs, add a GitHub Actions workflow that runs the official withastro/action, and switch the repository’s Pages source to “GitHub Actions”. Every push to main then builds the static dist/ folder and publishes it for free.

This guide walks through the whole Astro GitHub Pages setup for an Astro 7 project, including the base path issue that breaks most first deploys.

Key Takeaways

  • GitHub Pages only serves static files, so keep output: 'static' (the default) and no adapter.
  • User sites (username.github.io) need only site. Project sites (username.github.io/repo) also need base: '/repo'.
  • Use withastro/action for the build step and actions/deploy-pages for the deploy step.
  • Prefix internal links and public/ assets with import.meta.env.BASE_URL when base is set.
  • A custom domain removes the base problem entirely: add public/CNAME and drop base.

What you need before you start

GitHub Pages is a static host. It serves HTML, CSS, JavaScript and images from a branch or an Actions artifact, and nothing else runs on the server. That suits Astro well, because a default Astro build is plain files in dist/.

You need:

  1. An Astro project in a GitHub repository (public, or private on a paid plan that includes Pages).
  2. A lockfile committed (package-lock.json, pnpm-lock.yaml, yarn.lock or bun.lockb). The action uses it to pick the package manager.
  3. npm run build passing locally. If it fails on your machine, it fails in Actions.

If your project uses on-demand pages, server islands, actions or sessions, GitHub Pages can’t run them. Those features need an adapter and a server runtime; see the Astro deployment guide for hosts that support them.

Step 1: Set site and base in astro.config.mjs

Astro needs to know the final URL so it can build canonical links, the sitemap and correct asset paths. Which values you use depends on the repository name.

Repository Final URL site base
username.github.io https://username.github.io/ 'https://username.github.io' not set
my-site https://username.github.io/my-site/ 'https://username.github.io' '/my-site'
any, with custom domain https://www.example.com/ 'https://www.example.com' not set

For a project repository:

// astro.config.mjs
import { defineConfig } from 'astro/config';

export default defineConfig({
  site: 'https://username.github.io',
  base: '/my-site',
  trailingSlash: 'always',
});

site is the origin only. Don’t put the repository path in it; that’s what base is for. Setting trailingSlash is optional, but GitHub Pages serves /about/ from about/index.html, so 'always' keeps your links and canonical URLs consistent. The trailing slash tip explains the trade-offs.

This is where most Astro GitHub Pages deploys break. With base: '/my-site', Astro prefixes the CSS and JavaScript it generates, but it does not rewrite hard-coded links in your templates. A link to /about/ points at username.github.io/about/, which is a different site.

Build links from import.meta.env.BASE_URL instead:

---
// src/components/Nav.astro
const base = import.meta.env.BASE_URL.replace(/\/$/, '');
const links = [
  { href: '/', label: 'Home' },
  { href: '/blog/', label: 'Blog' },
  { href: '/about/', label: 'About' },
];
---
<nav>
  {links.map((link) => <a href={`${base}${link.href}`}>{link.label}</a>)}
</nav>

The same applies to files in public/. An image at public/logo.svg lives at /my-site/logo.svg after deploy:

---
// src/components/Logo.astro
const base = import.meta.env.BASE_URL.replace(/\/$/, '');
---
<img src={`${base}/logo.svg`} alt="Example Studio" width="120" height="32" />

Images imported from src/ and rendered with <Image /> don’t need this. Astro emits them under /_astro/ with the base already applied. If you’re starting from a theme, search the project for href="/ and src="/ and convert each one; it takes a few minutes and saves a broken first deploy.

Step 3: Add the GitHub Actions workflow

Create the workflow file. It has two jobs: build runs withastro/action, which installs dependencies, runs the build and uploads dist/ as a Pages artifact, and deploy publishes that artifact with actions/deploy-pages.

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

on:
  push:
    branches: [main]
  workflow_dispatch:

permissions:
  contents: read
  pages: write
  id-token: write

concurrency:
  group: pages
  cancel-in-progress: false

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Build with Astro
        uses: withastro/action@v3
        with:
          node-version: 22

  deploy:
    needs: build
    runs-on: ubuntu-latest
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

Action versions move. Before you commit, open the withastro/action README and use the major version it currently shows for both actions. Set node-version explicitly so the build doesn’t change when the action’s default does; Astro 7 needs a current Node.js LTS.

The action takes a few optional inputs:

Input Use it when
path The Astro project lives in a subfolder (a monorepo or a site/ folder)
node-version You want to pin the Node.js version used for the build
package-manager Lockfile detection picks the wrong one, or you want to pin a version like pnpm@9

Step 4: Turn on Pages in the repository

  1. Open the repository on GitHub and go to Settings > Pages.
  2. Under Build and deployment, set Source to GitHub Actions.
  3. Push the workflow to main, or run it from the Actions tab with Run workflow.

The first run takes a minute or two. When the deploy job finishes, its summary shows the live URL. Open it and click through a few pages, checking that styles load and internal links stay inside /my-site/.

Using environment variables in the build

GitHub Pages has no runtime, so every variable must be available at build time. Store values in Settings > Secrets and variables > Actions, then expose them to the build step:

# .github/workflows/deploy.yml (build step only)
      - name: Build with Astro
        uses: withastro/action@v3
        with:
          node-version: 22
        env:
          PUBLIC_ANALYTICS_ID: ${{ vars.PUBLIC_ANALYTICS_ID }}
          CMS_TOKEN: ${{ secrets.CMS_TOKEN }}

Anything read during the build ends up baked into the HTML, so a secret used only to fetch content is fine, but never print a secret into a page. The environment variables guide covers astro:env and how to keep secrets out of client code.

Adding a custom domain to Astro on GitHub Pages

A custom domain is the cleanest fix for base path issues, because the site moves to the root of its own origin.

  1. Create public/CNAME containing only the domain, for example www.example.com. Astro copies it into dist/ on every build, so the setting survives deploys.
  2. Update the config: set site: 'https://www.example.com' and remove base.
  3. At your DNS provider, add a CNAME record for www pointing to username.github.io. For an apex domain, add the A records listed in GitHub’s Pages documentation.
  4. In Settings > Pages, enter the domain and tick Enforce HTTPS once the certificate is issued.

Once base is gone, the BASE_URL prefix becomes / and the link code from Step 2 keeps working unchanged. The custom domain tip covers DNS for other hosts too.

Troubleshooting failed Astro GitHub Pages deploys

Symptom Likely cause Fix
Page loads with no styles base missing on a project repo Add base: '/repo-name'
Links go to username.github.io/about/ Hard-coded root links Prefix with import.meta.env.BASE_URL
Images from public/ are 404 Same as above Prefix public/ paths with the base
Workflow fails at “Deploy” with a permissions error Missing pages: write or id-token: write Copy the permissions block exactly
Workflow never runs Workflow isn’t on the default branch, or branch name differs Match branches: to your default branch
Build fails with “Cannot use server-side rendering without an adapter” A page sets prerender = false Remove on-demand pages or move to a host with an adapter
Custom domain resets after deploy CNAME set only in Settings Add public/CNAME
404 page is GitHub’s default No 404.html in the build Add src/pages/404.astro

GitHub Pages serves 404.html from the root of the site for any missing path, so an Astro src/pages/404.astro page works as your error page with no extra configuration. The custom 404 page tip shows a minimal one.

Is GitHub Pages the right host for your Astro site?

GitHub Pages is free, it sits next to your code, and it needs no extra account. It’s a good fit for documentation, portfolios, personal sites and open-source project pages. Its limits are real, though: no server functions, no redirects beyond what you generate as HTML, no custom response headers, and soft limits on site size and bandwidth listed in GitHub’s Pages documentation.

If you need preview deployments per pull request, header control, or on-demand rendering later, Cloudflare Pages, Netlify and Vercel all host static Astro sites on free tiers and add those features. The static hosting docs compare the setup for each. Every Komal theme builds to static HTML, so any of the Astro themes can be deployed to GitHub Pages with the workflow above.

For the official reference, see Astro’s GitHub Pages deployment guide.

FAQ

Is GitHub Pages free for Astro sites?

Yes, for public repositories on any GitHub plan. Private repositories can use Pages on paid plans. You pay nothing for hosting or bandwidth within GitHub’s usage limits, and the Actions minutes used by the build are free for public repositories.

Why does my Astro site on GitHub Pages have no CSS?

Almost always because the repository is a project site (username.github.io/repo) and base isn’t set. Astro writes asset URLs from the root, so the browser requests them from the wrong path. Set base: '/repo' in astro.config.mjs and redeploy.

Can I use SSR or Astro Actions on GitHub Pages?

No. GitHub Pages serves static files only, so anything that needs an adapter (on-demand pages, server islands, actions, sessions) won’t run. Use Cloudflare, Netlify, Vercel or a Node.js server for those, or keep the site fully static.

Do I need the gh-pages branch to deploy Astro?

No. The withastro/action workflow uploads the build as an artifact and actions/deploy-pages publishes it, so there’s no gh-pages branch to maintain. Set the Pages source to “GitHub Actions” in repository settings.

How do I deploy an Astro site in a subfolder of the repository?

Pass the folder to the action with path: ./site (or wherever the project lives). The action runs install and build in that folder and uploads its dist/ directory.