Logo

dev-resources.site

for different kinds of informations.

Hosting static websites on GitHub pages

Published at
2/3/2023
Categories
githubpages
githubactions
hosting
Author
zsevic
Categories
3 categories in total
githubpages
open
githubactions
open
hosting
open
Author
6 person written this
zsevic
open
Hosting static websites on GitHub pages

This post covers instructions for deploying to GitHub pages and domain setup.

GitHub Pages

GitHub provides a way to host a static website (e.g., Gatsby blog) for every repository on the gh-pages branch, which contains an index.html file. To make it work, create a public or private repository <GITHUB_USERNAME>.github.io. Every other repository will be available at the <GITHUB_USERNAME>.github.io/<REPOSITORY_NAME> URL.

GitHub Page themes

GitHub supports themes for GitHub pages. Specify the theme name in the _config.yml file.

# _config.yml
theme: jekyll-theme-midnight
Enter fullscreen mode Exit fullscreen mode

Fill out the README.md file with some content.

Pushing the changes to the gh-pages branch will trigger the deployment pipeline.

DNS setup

A purchased domain is needed. You can buy it at Namecheap website. Create a CNAME file with the domain name inside the repository.

sevic.dev
Enter fullscreen mode Exit fullscreen mode

After purchasing the domain, go to Account → Dashboard → Manage → Advanced DNS page on the Namecheap website. Create 4 A records with @ host to point to GitHub servers IP addresses (185.199.108.153, 185.199.109.153, 185.199.110.153 and 185.199.111.153) and one CNAME record with www host and <GITHUB_USERNAME>.github.io value.

Enforce HTTPS connection on the Settings → Pages page inside the GitHub repository.

Other public and private repositories will be available on the <DOMAIN>/<REPOSITORY_NAME> URL.

Continuous deployment

Deployment to GitHub pages can be automated with GitHub actions when the changes are pushed to the specific branch. A GitHub token is required. Create it with repo permissions at Tokens page. Set it as a GitHub action secret inside the project repository on Settings → Secrets and variables → Actions page.

Add the following configuration (which will build and deploy the project to the gh-pages branch) to .github/workflows/config.yml file.

name: Build and Deploy

on:
  push:
    branches:
      - master

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout 🛎️
        uses: actions/checkout@v4

      - name: Configure Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Install and Build 🔧
        run: |
          npm ci
          npm run build
      - name: Deploy 🚀
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          token: ${{ secrets.GH_TOKEN }}
          BRANCH: gh-pages # The branch the action should deploy to
          FOLDER: public # The folder the action should deploy
          CLEAN: true # Automatically remove deleted files from the deploy branch
Enter fullscreen mode Exit fullscreen mode

Notes

If you need to use sensitive environment variables, avoid committing them inside the repository and use an alternative for hosting, such as Vercel or Netlify.

Course

Build your SaaS in 2 weeks - Start Now

githubpages Article's
30 articles in total
Favicon
London Perl Mongers on GitHub Pages
Favicon
Create Your Professional Resume Website with GitHub and Quarto
Favicon
Did You Know You Can Use GitHub to Host Your Site for Free?
Favicon
A refresher on GitHub Pages
Favicon
How to Deploy a React App to GitHub Pages
Favicon
How to Deploy an Express.js App on GitHub Pages Using GitHub Actions
Favicon
Create your portfolio with GitHub Pages
Favicon
Deploying React Apps to Github Pages with Github Actions
Favicon
Publicando suas paginas Flutter no Github Pages utilizando GitHub Action
Favicon
From Localhost to Live: Easy Deployment With Github Pages or Netlify
Favicon
Jekyll Docker
Favicon
Guia de Análise de Tráfego para GitHub Pages: Integração com Ferramentas de Analytics
Favicon
Um Guia Simples para Levar Seu Projeto Angular para o GitHub Pages
Favicon
How to: Deploy Angular Website to Github Pages
Favicon
Using GitHub Pages and Miss Hosting for DNS and Hosting
Favicon
Deploying Your Vite Apps to Github Pages is a Breeze with Vite Github Pages Deployer
Favicon
piratematt.com v3—Overview & Getting Started
Favicon
Host React app in GitHub pages
Favicon
404 There isn't a GitHub Pages site here.
Favicon
Deploy your React app using GitHub pages
Favicon
Why your GitHub action is not triggered by other actions
Favicon
Deploying GitHub Pages sites with GitHub Workflows
Favicon
This blog changes its theme every day
Favicon
Como criar um blog com Jekyll no GitHub Pages?
Favicon
Create a free static website using Github Pages and Jekyll
Favicon
Github Pages Tips if your img doesn't appear 🖼️
Favicon
Hosting static websites on GitHub pages
Favicon
Updating GitHub Pages using GitHub Actions
Favicon
What I learned from self-hosting a Supabase Svelte project: Part 2
Favicon
Using GitHub Pages and GitHub Actions to Deploy a React App ✨

Featured ones: