Deploy to GitHub Pages
Deploy Clarity documentation to GitHub Pages for free hosting
Deploy to GitHub Pages
Deploy your Clarity documentation to GitHub Pages for free, automated hosting.
Prerequisites
- GitHub account
- Your Clarity repository pushed to GitHub
- Node.js 18+ and pnpm installed locally
Step 1: Configure Environment Variables
Local Configuration
Create or edit .env in your project root:
# Site Configuration
PUBLIC_SITE_NAME=My Documentation
PUBLIC_SITE_DESCRIPTION=Documentation for my project
PUBLIC_SITE_URL=https://yourusername.github.io/repo-name
PUBLIC_BASE_PATH=/repo-name/
# GitHub Integration
CLARITY_GITHUB_REPO=yourusername/repo-name
CLARITY_GITHUB_BRANCH=main
CLARITY_DOCS_PATH=src/content/docs
Important: Replace:
yourusernamewith your GitHub usernamerepo-namewith your repository name
GitHub Repository Variables
- Go to your repository on GitHub
- Navigate to Settings → Secrets and variables → Actions → Variables
- Click New repository variable
- Add these variables:
| Name | Value | Example |
|---|---|---|
CLARITY_GITHUB_REPO | username/repo | alex-migwi/clarity |
CLARITY_GITHUB_BRANCH | main or master | main |
CLARITY_DOCS_PATH | src/content/docs | src/content/docs |
Step 2: Configure Astro for GitHub Pages
Edit astro.config.mjs to set the correct base path:
import { defineConfig } from 'astro/config';
export default defineConfig({
site: 'https://yourusername.github.io',
base: '/repo-name', // Must match your repository name
// ... rest of config
});
Step 3: Set Up GitHub Actions
The repository already includes a GitHub Actions workflow at .github/workflows/docs-deploy.yml. Verify it contains:
name: Deploy Clarity Docs
on:
push:
branches: [main]
workflow_dispatch:
permissions:
contents: read
pages: write
id-token: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 8
- name: Install dependencies
run: pnpm install
- name: Build site
env:
CLARITY_GITHUB_REPO: ${{ vars.CLARITY_GITHUB_REPO }}
CLARITY_GITHUB_BRANCH: ${{ vars.CLARITY_GITHUB_BRANCH }}
CLARITY_DOCS_PATH: ${{ vars.CLARITY_DOCS_PATH }}
run: pnpm build
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: ./dist
deploy:
needs: build
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
Step 4: Enable GitHub Pages
- Go to your repository Settings
- Scroll to Pages section (left sidebar)
- Under Source, select:
- Source: GitHub Actions
- Click Save
Step 5: Deploy
Push your changes to trigger deployment:
git add .
git commit -m "Configure for GitHub Pages deployment"
git push origin main
GitHub Actions will automatically:
- Build your site
- Deploy to GitHub Pages
- Make it available at
https://yourusername.github.io/repo-name
Step 6: Verify Deployment
- Go to Actions tab in your repository
- Watch the deployment workflow run
- Once complete (green checkmark), visit your site:
https://yourusername.github.io/repo-name
Troubleshooting
404 Page Not Found
Cause: Base path mismatch
Solution: Ensure these match:
astro.config.mjs:base: '/repo-name'.env:PUBLIC_BASE_PATH=/repo-name/- URL:
https://yourusername.github.io/repo-name
Build Fails
Check:
- Repository variables are set correctly
- Branch name matches (
mainormaster) - No syntax errors in your markdown files
View logs:
- Go to Actions tab → Click failed workflow → View logs
Assets Not Loading
Cause: Incorrect base path in asset references
Solution: All links must include the base path:
// Good
<img src={`${base}logo.svg`} />
// Bad
<img src="/logo.svg" />
Search Not Working
Cause: Search index not generated
Solution: Ensure pnpm build runs successfully locally:
pnpm build
# Should create dist/search-index.json
Custom Domain
To use a custom domain with GitHub Pages:
-
Add CNAME file: Create
public/CNAMEwith your domain:docs.yourdomain.com -
Configure DNS: Add these records at your domain provider:
Type: CNAME Name: docs Value: yourusername.github.io -
Update configuration:
// astro.config.mjs site: 'https://docs.yourdomain.com', base: '/', // Root domain, no subdirectory -
Update .env:
PUBLIC_SITE_URL=https://docs.yourdomain.com PUBLIC_BASE_PATH=/ -
Enable in GitHub:
- Settings → Pages → Custom domain
- Enter:
docs.yourdomain.com - Wait for DNS check (can take 24-48 hours)
- Enable Enforce HTTPS
Updating Your Site
Every push to main automatically rebuilds and deploys:
# Make changes to your docs
git add .
git commit -m "Update documentation"
git push origin main
# GitHub Actions handles the rest!
Next Steps
- Configure Clarity for your needs
- Write documentation content
- Set up authentication (optional)
- Explore components for rich content
Need Help?
- Check GitHub Actions logs
- Review GitHub Pages documentation
- Open an issue in the Clarity repository
Was this page helpful?
Thank you for your feedback!