🎓 A1Learner
🔍
0 of 50 lessons completed

GitHub Actions: Deploy to GitHub Pages

GitHub Pages is free hosting for a static site, served straight from a repository. A deploy is the act of publishing a built site so readers can open it in a browser.

Build the site

The demo build writes the site files into the dist folder.

- run: npm run build

Upload the built site

This action packs the dist folder as the Pages artifact.

- uses: actions/upload-pages-artifact@v5
  with:
    path: dist

Publish the artifact

The deploy step publishes the artifact and reports the site address.

- id: deployment
  uses: actions/deploy-pages@v5

Example

name: Deploy to GitHub Pages
on:
  push:
    branches: [main]
permissions:
  contents: read
  pages: write
  id-token: write
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with:
          node-version: '24'
      - run: npm test
      - run: npm run build
      - uses: actions/configure-pages@v6
      - uses: actions/upload-pages-artifact@v5
        with:
          path: dist
  deploy:
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - id: deployment
        uses: actions/deploy-pages@v5

Key points

  • Use GitHub Pages to publish a static site, which is a site of plain files with no server code.
  • Build the site first, then upload the built folder as the Pages artifact.
  • The deploy job needs the pages write permission and the id-token write permission.
  • The build job uploads the site, and the deploy job publishes it.
  • The deploy runs on merge to main, so the live site matches the main branch.
  • The deployed site address comes from the deploy step output named page_url.

In this example, the build job checks out the code and sets up Node 24. It runs the tests, then builds the site into the dist folder. It configures Pages and uploads dist as the Pages artifact. The deploy job needs the build job, so it runs after it. It publishes the artifact, and the site address comes from the deploy step output.

Note

GitHub Pages must be switched on in repo Settings under Pages, with GitHub Actions as the source. The coordinator switches it on at verification time.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

Which two actions publish the demo site to GitHub Pages?