Learn GitHub
$ gh ghautomation.ghpages

Hosting Sites with GitHub Pages

4 learning objectives · 5 key terms · 2 commands · 1 diagrams · 5 quiz questions

$objectives

Hosting Sites with GitHub Pages

Automation & Security

Module 4 of 5 · lesson 17 in this module

What you'll be able to do

  • Publish a static site from a repository with GitHub Pages
  • Choose between branch-based and Actions-based publishing
  • Point a custom domain at a Pages site with HTTPS enforced
  • Preview documentation sites from pull requests safely
$concept

The idea, in plain language

GitHub Pages turns a repository into a hosted static website for free: documentation, portfolios, project pages and blogs served over HTTPS with no server to run. It matters because the site lives next to the code it documents — versioned together, reviewed together, deployed by merging.

Two publishing paths exist. Branch-based publishing serves a docs/ folder or a dedicated branch directly and suits plain HTML sites. Actions-based publishing builds first (Jekyll, Hugo, Vite, Docusaurus) and deploys the artifact, which suits anything needing a build step. Custom domains attach with a CNAME file plus DNS records, and GitHub provisions the certificate automatically — the whole setup is version-controlled text, so the site configuration itself gets reviewed like code.

$key terms

Key terms

GitHub Pages
Free static-site hosting served straight from a repository.
docs/ folder vs branch source
The two branch-publishing modes: a folder on the default branch, or a dedicated branch like gh-pages.
Actions deployment
Building the site in a workflow, then deploying the artifact — required for any build step.
CNAME file
A file containing the custom domain, stored in the published source so the mapping is versioned.
Enforce HTTPS
The Pages setting that serves only over TLS once the certificate is provisioned.
$steps

How it works, step by step

  1. 1
    Choose the source

    For plain HTML, publish a docs/ folder or a gh-pages branch. For anything built (Jekyll, Vite, Docusaurus), choose GitHub Actions as the source.

    choosebash
    # plain site: put index.html in docs/
    # built site: keep sources on main, build in Actions
  2. 2
    Enable in settings

    Repository Settings, then Pages: pick the branch and folder (or the Actions workflow). The site URL appears once the first deployment finishes.

    enablebash
    Settings > Pages > Build and deployment
    Source: Deploy from a branch  # or: GitHub Actions
  3. 3
    Deploy with Actions (built sites)

    Use the official configure-pages, upload-pages-artifact and deploy-pages actions in sequence. Every push to main rebuilds and redeploys automatically.

    workflowbash
    jobs:
      deploy:
        steps:
          - uses: actions/checkout@v4
          - uses: actions/configure-pages@v5
          - run: npm ci && npm run build
          - uses: actions/upload-pages-artifact@v3
            with: { path: ./dist }
          - uses: actions/deploy-pages@v4
  4. 4
    Attach a custom domain

    Add a CNAME file with the domain, point DNS at GitHub (A records or CNAME per the docs), then tick Enforce HTTPS once the certificate provisions.

    domainbash
    echo "docs.example.com" > docs/CNAME
    # DNS: 185.199.108.153 (+ .109/.110/.111) as A records
$commands

Commands worth memorising

Every block below is copy-ready — use the Copy button, then paste it into your terminal. Expected output is shown underneath so you know what “working” looks like.

check the published URLbash
gh api repos/:owner/:repo/pages --jq .html_url
Output
https://ada.github.io/cookbook/

The API reports the live URL, which is handy in scripts that verify deployments.

trigger a rebuildbash
gh workflow run pages-build-deployment
Output
✓ Created workflow_dispatch event

Manually re-runs the Pages deployment without pushing an empty commit.

$diagrams

Workflow diagrams

Diagram 1Two paths to a live site

Branch mode serves files directly; Actions mode builds first, then serves the artifact.

Reading the diagram
  1. Push to the source branch or folder.
  2. GitHub builds (Actions mode) and publishes.
  3. The site serves over HTTPS, optionally on your domain.
$example

Worked example

A docs site for a small library, published from a docs/ folder with a custom domain.

docs site, custom domainbash
echo "docs.cookbook.dev" > docs/CNAME
git add docs/CNAME && git commit -m "Map custom domain" && git push
# Settings > Pages shows https://docs.cookbook.dev once DNS + certificate settle
Output
Site live on the custom domain with HTTPS enforced — configuration stored as ordinary versioned files.
1The CNAME file declares the domain in version control, reviewable like any other change.
2Pushing triggers publication; DNS plus certificate provisioning completes the mapping.
3Enforce HTTPS turns on once ready, closing the plaintext window permanently.
$use cases

Where you'll use it

▸Project documentation living beside the code it describes.
▸Portfolios and blogs with zero hosting cost or servers.
▸Previewing docs changes per pull request before merging.
▸Custom-domain project sites with automatic certificates.
$mistakes

Common mistakes

  • Committing build output by hand instead of letting Actions build it.
  • Forgetting the CNAME file, so a rebuild drops the custom domain mapping.
  • Skipping Enforce HTTPS and serving mixed content warnings.
  • Putting secrets in a public Pages repository because the site needed them at build time.
$best practices

Best practices

  • Build in Actions; publish artifacts, never hand-committed output.
  • Store the CNAME file in the repository so the mapping is reviewed.
  • Enforce HTTPS as soon as the certificate provisions.
  • Keep build-time secrets in Actions secrets, never in the published output.
$exercise

Hands-on exercise

Ship a docs page

Publish a one-page site with a custom-domain-ready setup.

  1. Create a repository with a docs/index.html page.
  2. Enable Pages from the docs/ folder and open the live URL.
  3. Convert to Actions deployment with a build step, and confirm it redeploys on push.
  4. Add a CNAME file for a domain you control (or a placeholder) and verify it persists across rebuilds.
  5. Turn on Enforce HTTPS and confirm the site loads securely.

💡 Hint: The environment tab of the repository shows each Pages deployment with its status and URL.

Done when: A live HTTPS page, rebuilt by Actions, with its domain mapping stored in the repository.

$quiz

Checkpoint quiz

0 of 5 answered · 0 correct

  1. Multiple choiceQ1

    When should you choose Actions-based publishing over branch-based publishing?

  2. Multiple choiceQ2

    What does the CNAME file do?

  3. True or falseQ3

    True or false: every push to the publishing source redeploys a GitHub Pages site automatically.

  4. Multiple choiceQ4

    Where should build-time secrets for a Pages site live?

  5. TroubleshootingQ5

    A custom domain stopped working after a rebuild. What most likely happened?

$summary

Module summary

  • GitHub Pages serves static sites free, straight from a repository.
  • Branch mode serves files directly; Actions mode builds first, then deploys.
  • Every push to the source redeploys automatically.
  • Custom domains map via a versioned CNAME file plus DNS, with automatic HTTPS.
  • Build in Actions and keep secrets out of published output.

Next up: Sites are live; next is making the delivery pipeline itself smarter, with matrices, reusable workflows and hardened identity.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Put the GitHub course to work: open one of the company-wise repositories and follow the workflow you just learned, branch to release.