Deploying to GitHub Pages with Actions
GitHub Pages is the simplest free host for a static site whose code already lives on GitHub. For years it meant pushing built files to a gh-pages branch, or letting Pages run Jekyll for you. Today the recommended path is a GitHub Actions workflow that builds the site with any generator and publishes the output directly as a Pages artifact, with no branch of built files and no Jekyll involved.
This guide sets up that workflow for any static site generator, handles the base path problem for project sites, adds a custom domain, and is honest about what Pages cannot do so you know when to pick a different host. It is part of GitHub Actions for Automated SSG Builds.
Prerequisites
- A static site in a GitHub repository that builds to a folder with one command.
- Repository admin access to change the Pages settings.
- Optional: a custom domain you control.
Enable Actions as the Source
In the repository, open Settings → Pages and set Source to GitHub Actions. This tells Pages to publish whatever a workflow deploys, rather than a branch. It also creates a github-pages environment that the deploy job will use.
The Workflow
name: Deploy to 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:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version-file: .nvmrc, cache: npm }
- id: pages
uses: actions/configure-pages@v5
- run: npm ci
- run: npm run build
env:
BASE_PATH: ${{ steps.pages.outputs.base_path }}
- uses: actions/upload-pages-artifact@v3
with:
path: dist
deploy:
needs: build
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- id: deployment
uses: actions/deploy-pages@v4
What each part does:
pages: writeandid-token: writelet the deploy action authenticate to Pages with a short-lived OIDC token. No personal access token or deploy key is involved.concurrencywithcancel-in-progress: falsequeues deployments rather than cancelling one mid-publish.configure-pagesoutputs the site'sbase_path— empty for a custom domain or a user site,/repofor a project site — which the build can use.upload-pages-artifactpackages the folder as a tarball artifact in the format Pages expects.deploy-pagespublishes it and reports the URL on the workflow run.
The Base Path Problem
A project site is served from https://user.github.io/repo/, not from the root. Every root-relative URL the generator emits — /assets/app.css, /docs/install/ — then points outside the site and returns 404. This is the most common reason a Pages deployment "works" but shows an unstyled page with broken links.
Tell the generator about the subpath. The configure-pages output makes it automatic:
- Astro:
base: process.env.BASE_PATH || '/'andsite: 'https://user.github.io'inastro.config.mjs. Useimport.meta.env.BASE_URLwhen building links in components. - Hugo:
hugo --baseURL "${{ steps.pages.outputs.base_url }}/", and userelURLorabsURLin templates. - Eleventy: set
pathPrefixfrom the environment variable and use theurlfilter on links. - Next.js static export:
basePathandassetPrefixinnext.config.js. - Docusaurus, VitePress, Starlight: each has a
baseUrlorbaseoption for the same purpose.
Content links written by hand in Markdown also need the prefix, which is why many teams prefer a custom domain: the site is then served from / and the problem disappears.
To catch this before publishing, run a link check against the build output with the base path applied; a missing prefix shows up as hundreds of broken internal links in one run.
Custom Domains
Add the domain in Settings → Pages → Custom domain. For a subdomain like docs.example.com, create a CNAME record to user.github.io. For an apex domain, create A records to GitHub's Pages IP addresses (listed in GitHub's documentation) and AAAA records for IPv6. Tick Enforce HTTPS once the certificate is issued, usually within an hour.
With Actions deployments, the custom domain is stored in the repository settings, so you do not need a CNAME file in the build output. Verify the domain at the organisation or account level as well, which prevents someone else from claiming it for their Pages site if your configuration is ever removed.
What GitHub Pages Cannot Do
Pages is deliberately simple, and some of its limits matter for production sites:
- No custom headers. You cannot set a Content Security Policy, HSTS with preload, or
Cache-Control. A<meta http-equiv>tag covers some CSP directives but not all. - Fixed caching. Responses carry
Cache-Control: max-age=600. Content-hashed assets that could be cached for a year are revalidated every ten minutes, which costs repeat visitors a round trip per asset. - No server-side redirects. Moved pages need HTML files with a meta refresh, which is slower and weaker for search engines. Jekyll's redirect plugin generates these for you; other generators need a small script.
- No pull request previews. Pages has one production site per repository. Previews need another host or a separate repository.
- Limits. Sites should stay under 1 GB, each deployment times out after 10 minutes, and there is a soft bandwidth limit of 100 GB a month.
For open-source project documentation these are acceptable trade-offs for free, zero-maintenance hosting. For a company's product site, the missing headers and caching usually justify a CDN-backed host; see Netlify vs Vercel Deployment Strategies and Cloudflare Pages Edge Caching Setup.
Adding Checks Before Deploy
The build job is the place for quality gates. Add a link check on the built output before upload-pages-artifact, so a broken build never publishes; see Checking Links in Pull Requests. For pull requests, run the same build and checks in a separate workflow triggered on pull_request without the deploy job, so contributors get feedback even though there is no preview site.
Measured Impact
An open-source project with a 600-page Hugo documentation site moved from a workflow that force-pushed built files to a gh-pages branch to the Actions deployment above. The repository shrank from 2.1 GB to 180 MB after the old branch was deleted, clone times for contributors fell accordingly, and deploys dropped from about four minutes to two because the push of thousands of changed files was replaced by a single artifact upload. The project also moved to a custom domain, which removed 340 hand-written links that had needed the /repo/ prefix.
Pitfalls & Rollback
- Source still set to a branch. The workflow succeeds but nothing changes on the site; check Settings → Pages.
- Missing base path. Unstyled pages and 404s on a project site; configure the generator's base.
- Jekyll processing. With Actions deployment, Pages does not run Jekyll, so folders starting with
_are served as-is; no.nojekyllfile is needed. - Environment protection rules. A rule that restricts
github-pagesto certain branches blocks deploys from others. - Rollback: re-run the deploy job of an earlier successful workflow run; it republishes that run's artifact while it is retained.
Conclusion
Deploying to GitHub Pages with Actions works with any static site generator: set the source to GitHub Actions, build the site, upload it with upload-pages-artifact and publish with deploy-pages using OIDC. Configure the base path for project sites or use a custom domain, add checks before the upload, and keep Pages' limits in mind — no headers, fixed caching, no redirects and no previews. For documentation and open-source projects it is hard to beat; for production product sites, a CDN-backed host usually fits better.
FAQ
Do I still need a gh-pages branch to deploy to GitHub Pages?
No. With the source set to GitHub Actions, a workflow uploads the built site as a Pages artifact with actions/upload-pages-artifact and publishes it with actions/deploy-pages. No branch holds the built files, which keeps repository history clean.
Why are my CSS and links broken on username.github.io/repo?
Project sites are served from a subpath, so root-relative links such as /assets/app.css point to the wrong place. Configure the generator's base path, for example base in Astro or baseURL in Hugo, to /repo/, or use a custom domain so the site is served from the root.
Can I set custom headers or redirects on GitHub Pages?
No. GitHub Pages does not support custom response headers, server-side redirects or rewrite rules, and cache lifetimes are fixed at about ten minutes. Sites that need security headers, long-lived asset caching or many redirects should use a CDN in front or a different host.
Are there limits on GitHub Pages sites?
Published sites should be under 1 GB, deployments time out after 10 minutes, and there is a soft bandwidth limit of 100 GB per month. Pages is intended for project sites and documentation, not commercial high-traffic sites.
Related
- Parent: GitHub Actions for Automated SSG Builds — every workflow pattern.
- Building Astro Sites with GitHub Actions — the build job in more depth.
- GitHub Actions for Hugo Deployments — Hugo-specific setup.
- Upgrading Jekyll and Ruby Versions Safely — moving off the built-in Jekyll build.
- Cloudflare Pages Edge Caching Setup — when you need headers and caching.