Success!
This site is now live on GitHub Pages! Follow the steps below to deploy your own Express.js application to GitHub Pages.
The Challenge
GitHub Pages is designed for static sites, but our starter kit is a dynamic Express.js application. We solved this by creating a build process that:
- Converts dynamic EJS templates to static HTML
- Fixes path issues for GitHub Pages subdirectories
- Automatically deploys via GitHub Actions
Step 1: Project Setup
First, make sure your project has the necessary build scripts and files:
Required Scripts
Add these scripts to your package.json
:
{
"scripts": {
"clean": "node scripts/clean-docs.js",
"copy-icons-docs": "node scripts/copy-icons.js --target=docs",
"copy-static-assets": "node scripts/copy-static-assets.js",
"generate-static-site": "node scripts/generate-static-site.js",
"build-css": "sass --load-path=node_modules scss/main.scss docs/css/styles.css",
"prebuild": "npm run clean",
"build": "npm-run-all copy-icons-docs copy-static-assets build-css generate-static-site"
}
}
Required Build Scripts
Create these files in your scripts/
directory:
clean-docs.js
- Cleans the output directorycopy-icons.js
- Copies Bootstrap Iconscopy-static-assets.js
- Copies JS, manifest, etc.generate-static-site.js
- Converts EJS to HTML
Step 2: Build Process
Our build process solves several key challenges:
Path Conversion
Converts absolute paths like /css/styles.css
to relative paths like ./css/styles.css
for GitHub Pages compatibility.
Static Generation
Renders EJS templates to static HTML files with proper directory structure for each page.
Run the Build
npm run build
This command will:
- Clean the
/docs
directory - Copy Bootstrap Icons to
/docs/fonts/
- Copy static assets to
/docs/
- Compile SASS to
/docs/css/
- Generate static HTML files with correct paths
Step 3: GitHub Actions Setup
Create .github/workflows/deploy.yml
for automatic deployment:
name: Build and Deploy to GitHub Pages
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "18"
cache: "npm"
- name: Install dependencies
run: npm install
- name: Build for GitHub Pages
run: npm run build
- name: Deploy to GitHub Pages
if: github.ref == 'refs/heads/main'
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./docs
force_orphan: true
Step 4: GitHub Pages Configuration
Configure your GitHub repository for Pages deployment:
-
Go to your repository on GitHub
Navigate to your GitHub repository in a web browser.
-
Access Settings
Click on the "Settings" tab in your repository.
-
Find Pages Section
Scroll down and click on "Pages" in the left sidebar.
-
Configure Source
Under "Source", select "Deploy from a branch".
-
Select Branch and Folder
Choose "main" branch and "/docs" folder, then click "Save".
Step 5: Deploy Your Site
Now you're ready to deploy!
Automatic Deployment
Every time you push to the main branch, GitHub Actions will automatically:
- Build your static site
- Deploy it to GitHub Pages
- Make it live at
https://username.github.io/repository-name
Manual Deployment
You can also deploy manually:
# Build the site locally
npm run build
# Commit and push the changes
git add .
git commit -m "Deploy to GitHub Pages"
git push origin main
Troubleshooting Common Issues
Problem: CSS, JS, or other assets not loading (404 errors).
Solution: Our build process automatically converts absolute paths to relative paths. Make sure you're running npm run build
and not manually copying files.
Check: Generated HTML should have paths like ./css/styles.css
not /css/styles.css
.
Problem: GitHub Actions build fails.
Solutions:
- Check that all dependencies are listed in
package.json
- Ensure Node.js version compatibility (we use Node 18)
- Verify all build script files exist in
scripts/
directory - Check the Actions tab in GitHub for detailed error logs
Problem: Your site shows a 404 page.
Solutions:
- Make sure you selected "/docs" folder in GitHub Pages settings
- Verify that
docs/index.html
exists after build - Check that your repository is public (or you have GitHub Pro for private repos)
- Wait a few minutes after deployment for changes to propagate
Success! Your Site is Live
Once everything is set up, your site will be available at:
https://username.github.io/repository-name
Every push to your main branch will automatically update your live site!
Quick Checklist
Timeline
- Setup: ~10 minutes
- First build: ~2-5 minutes
- Future deploys: ~1-2 minutes