Hosting Sites with GitHub Pages
4 learning objectives · 5 key terms · 2 commands · 1 diagrams · 5 quiz questions
Hosting Sites with GitHub Pages
Automation & SecurityWhat 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
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
- 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.
How it works, step by step
- 1Choose 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
- 2Enable in settings
Repository Settings, then Pages: pick the branch and folder (or the Actions workflow). The site URL appears once the first deployment finishes.
enablebashSettings > Pages > Build and deployment Source: Deploy from a branch # or: GitHub Actions
- 3Deploy 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.
workflowbashjobs: 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
- 4Attach 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.
domainbashecho "docs.example.com" > docs/CNAME # DNS: 185.199.108.153 (+ .109/.110/.111) as A records
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.
gh api repos/:owner/:repo/pages --jq .html_url
https://ada.github.io/cookbook/
The API reports the live URL, which is handy in scripts that verify deployments.
gh workflow run pages-build-deployment
✓ Created workflow_dispatch event
Manually re-runs the Pages deployment without pushing an empty commit.
Workflow diagrams
| Approach | Best for | Build step |
|---|---|---|
| docs/ folder | Plain HTML/CSS/JS sites | None — served as-is |
| gh-pages branch | Generated output committed separately | External, then committed |
| GitHub Actions | Jekyll, Hugo, Vite, Docusaurus | In the workflow, then deployed |
Branch mode serves files directly; Actions mode builds first, then serves the artifact.
- Push to the source branch or folder.
- GitHub builds (Actions mode) and publishes.
- The site serves over HTTPS, optionally on your domain.
Worked example
A docs site for a small library, published from a docs/ folder with a custom domain.
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
Site live on the custom domain with HTTPS enforced — configuration stored as ordinary versioned files.
Where you'll use it
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
- 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.
Hands-on exercise
Ship a docs page
Publish a one-page site with a custom-domain-ready setup.
- Create a repository with a docs/index.html page.
- Enable Pages from the docs/ folder and open the live URL.
- Convert to Actions deployment with a build step, and confirm it redeploys on push.
- Add a CNAME file for a domain you control (or a placeholder) and verify it persists across rebuilds.
- 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.
Checkpoint quiz
0 of 5 answered · 0 correct
- Multiple choiceQ1
When should you choose Actions-based publishing over branch-based publishing?
- Multiple choiceQ2
What does the CNAME file do?
- True or falseQ3
True or false: every push to the publishing source redeploys a GitHub Pages site automatically.
- Multiple choiceQ4
Where should build-time secrets for a Pages site live?
- TroubleshootingQ5
A custom domain stopped working after a rebuild. What most likely happened?
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.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
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.