Skip to content

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.

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).

  1. Create a new file in your project at .github/workflows/deploy.yml and paste in the YAML below.

    deploy.yml
    name: Deploy to GitHub Pages
    on:
    # Trigger the workflow every time you push to the `main` branch
    # Using a different branch name? Replace `main` with your branch’s name
    push:
    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 deployment
    permissions:
    contents: read
    pages: write
    id-token: write
    jobs:
    build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout your repository using git
    uses: actions/checkout@v4
    - name: Install, build, and upload your site
    uses: 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: 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

    The 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.)

  2. 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/
  3. In astro.config.mjs, configure a value for base (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 for base 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',
    })
  4. On GitHub, go to your repository’s Settings tab and find the Pages section of the settings.

  5. 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.

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.

  1. Configure DNS for your domain provider.

  2. 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.com

    This will deploy your site at your custom domain instead of user.github.io.

  3. In your Astro config, update the value for site with your custom domain. Do not set a value for base, and remove one if it exists:

    astro.config.mjs
    import { defineConfig } from 'astro/config'
    export default defineConfig({
    site: 'https://example.com',
    base: '/my-repo'
    })
  4. If necessary, update all your page internal links to remove the base prefix:

    <a href="/my-repo/about">About</a>

More Deployment Guides

Contribute Community Sponsor