How to Deploy a Private GitHub Repository with a Custom Domain

Learn how to host a static site from a private GitHub repository using GitHub Pages and map it to your custom domain. Ideal for developers using GitHub Pro or Team plans.


πŸ”’ Step 1: Upgrade to GitHub Pro or Team

GitHub Pages for private repositories requires a paid plan. Visit GitHub Pricing and upgrade to Pro, Team, or Enterprise.

πŸ“ Step 2: Set Up Your Repository

Your private repo should have the site files structured like:

βš™οΈ Step 3: Enable GitHub Pages

In your repo:

  1. Go to Settings > Pages
  2. Select Deploy from a branch
  3. Choose the branch (e.g., main) and folder (/ or /docs)
https://<your-username>.github.io/<repo-name>/

🌐 Step 4: Add Your Custom Domain

Still in the GitHub Pages settings, scroll to the Custom domain section and enter your domain (e.g., www.example.com). This will create a CNAME file in your repository automatically.

🧭 Step 5: Configure DNS

In your domain registrar dashboard (e.g., GoDaddy, Namecheap):

CNAME for Subdomain (like www):
www.example.com β†’ yourusername.github.io
A Records for Root Domain (like example.com):
set AAAA Records

Allow time for DNS propagation (usually 15 mins to a few hours).

πŸ” Step 6: Force HTTPS for Better Security

Once the custom domain is detected and DNS is ready, GitHub will issue a free SSL certificate. Enable "Enforce HTTPS" in the GitHub Pages settings to secure your site.

This step ensures your site loads over https:// with encryption, boosting SEO and security.

πŸ› οΈ Alternative: Deploy Using GitHub Actions

Don’t want to expose your entire private repo? Use GitHub Actions to build and push only your static files to a gh-pages branch:

Workflow Strategy:
  1. Keep full code in main (private)
  2. Build your static site using a CI/CD pipeline (e.g., npm run build)
  3. Deploy the final output (e.g., dist/) to a public gh-pages branch

Here’s a basic GitHub Actions workflow:

name: Deploy Static Site

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Build Site
        run: |
          npm install
          npm run build
      - name: Deploy to gh-pages
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./dist

This keeps your source private while hosting only the output folder.