Setting Up GitHub Pages with GitHub Actions
This article demonstrates how to create a GitHub repository with GitHub Pages enabled, from a basic hello world example to automated deployment using GitHub Actions. We'll cover manual setup steps and show how to automate the deployment process.
High-Level Steps
Setting up GitHub Pages involves:
- Create GitHub Repository - Create a new repository on GitHub
- Create Hello World Content - Add basic HTML content to serve
- Enable GitHub Pages - Configure Pages in repository settings
- Set Up GitHub Actions - Automate deployment with a workflow
- Verify Deployment - Confirm your site is live
Prerequisites
- GitHub account
- Git installed locally
- Basic knowledge of HTML
- Text editor or IDE
Use Case: Simple Hello World Site
We'll create a basic static website that displays "Hello World" and can be extended with additional content. This approach works for any static HTML site, documentation, or simple web applications.
Step-by-Step Implementation
Step 1: Create GitHub Repository
- Navigate to GitHub and click the "+" icon in the top right
- Select "New repository"
-
Configure the repository:
-
Repository name:
github-pages-demo(or your preferred name) - Description: "A simple hello world site on GitHub Pages"
- Visibility: Public (required for free GitHub Pages)
- Initialize with README: ✅ (checked)
-
Repository name:
- Click "Create repository"
Key points:
- Repository name will be part of your site URL:
https://username.github.io/repository-name - Public repositories get free GitHub Pages hosting
- Private repositories require GitHub Pro or higher for Pages
Step 2: Clone Repository Locally
Clone your repository to your local machine:
git clone https://github.com/your-username/github-pages-demo.git
cd github-pages-demo
Replace your-username with your actual GitHub username.
Step 3: Create Hello World Content
Create a basic index.html file in the repository root:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World - GitHub Pages</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.container {
text-align: center;
padding: 2rem;
}
h1 {
font-size: 3rem;
margin-bottom: 1rem;
}
p {
font-size: 1.2rem;
opacity: 0.9;
}
</style>
</head>
<body>
<div class="container">
<h1>Hello World!</h1>
<p>Welcome to my GitHub Pages site</p>
</div>
</body>
</html>
What this creates:
- A simple HTML page with "Hello World" message
- Basic styling for a clean appearance
- Responsive design that works on mobile devices
Alternative: You can create any static HTML content, CSS files, JavaScript files, or other static assets in your repository.
Step 4: Create GitHub Actions Workflow
Create the workflow directory and file:
mkdir -p .github/workflows
Create .github/workflows/pages.yml:
name: Deploy to GitHub Pages
on:
push:
branches:
- main
workflow_dispatch:
permissions:
contents: read
pages: write
id-token: write
concurrency:
group: "pages"
cancel-in-progress: false
jobs:
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Pages
uses: actions/configure-pages@v4
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: '.'
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
Workflow explanation:
-
Triggers: Runs on push to
mainbranch or manual trigger - Permissions: Required permissions for Pages deployment
- Concurrency: Prevents multiple deployments from running simultaneously
-
Steps:
- Checkout repository code
- Configure Pages settings
- Upload all files as artifact
- Deploy to GitHub Pages
Key points:
- The workflow automatically deploys when you push to
main - Uses the official GitHub Actions for Pages (
actions/deploy-pages@v4) - No build step needed for simple HTML (add build steps if using frameworks)
- Artifact path
'.'uploads the entire repository root
Step 5: Enable GitHub Pages
Important: You must enable GitHub Pages and select "GitHub Actions" as the source before pushing your code. If you push the workflow file first, the workflow will not run because the github-pages environment doesn't exist yet. Enabling Pages first creates the necessary environment for the workflow to execute successfully.
Step 5.1: Navigate to Settings
- Go to your repository on GitHub
- Click on "Settings" in the top navigation bar
- Scroll down to "Pages" in the left sidebar
Step 5.2: Configure Source
- Under "Build and deployment", find "Source"
- Select "GitHub Actions"
- The selection is automatically saved
What this does:
- Enables GitHub Pages for your repository
- Configures Pages to use GitHub Actions for deployment
- Creates the
github-pagesenvironment needed for the workflow - The workflow will handle all deployments automatically
Step 6: Commit and Push
Commit both files and push to GitHub:
git add index.html .github/workflows/pages.yml
git commit -m "feat: add hello world page and GitHub Pages workflow"
git push origin main
Note: We're pushing directly to main for simplicity in this tutorial. In a production environment, best practice is to:
- Create a branch (e.g.,
git checkout -b feature/add-pages-setup) - Create a pull request for review
- Merge after approval
Key points:
- The
mainbranch (ormaster) is typically used for GitHub Pages - All files in the repository root or a specific branch/folder can be served
- GitHub Pages serves static files only (HTML, CSS, JavaScript, images, etc.)
- The workflow will trigger automatically on push
Step 7: Wait for Deployment and Access Your Site
Step 7.1: Wait for Deployment
- GitHub Actions will detect the workflow file and run automatically
- GitHub will build and deploy your site
- This may take a few minutes
- You'll see a green checkmark when deployment is complete
- Check the Actions tab to monitor the workflow progress
Step 7.2: Access Your Site
Your site will be available at:
https://your-username.github.io/github-pages-demo
Replace your-username and github-pages-demo with your actual values.
Step 7.3: Add Website to Repository About Section (Optional)
To make your GitHub Pages site more accessible and visible to visitors, you can add the website URL to your repository's About section:
- On your repository page, click the gear icon (⚙️) next to the "About" section
- In the "Edit repository details" modal, find the "Website" field
- Enter your GitHub Pages URL:
https://your-username.github.io/github-pages-demo - Check the box "Use your GitHub Pages website" (this will automatically populate the URL)
- Click "Save changes"
What this does:
- Makes your site URL visible in the repository's About section
- Provides a direct link for visitors to access your site
- Improves discoverability and accessibility of your GitHub Pages site
Note: Future pushes to main will automatically trigger the workflow and deploy updates to your site.
Complete Example
Here's a complete repository structure:
github-pages-demo/
├── .github/
│ └── workflows/
│ └── pages.yml
├── index.html
└── README.md
index.html (as shown in Step 3)
pages.yml (as shown in Step 6)
Repository Structure Summary
After setup, you'll have:
| File/Folder | Purpose |
|---|---|
.github/workflows/pages.yml |
GitHub Actions deployment workflow |
index.html |
Main page content |
README.md |
Repository documentation |
Important Notes
GitHub Pages Limitations
- Static content only: No server-side processing (PHP, Python, etc.)
- File size limits: 1GB repository size, 100MB per file
- Bandwidth: 100GB per month for free accounts
- Build time: 10 builds per hour limit
Custom Domain Support
- You can use a custom domain with GitHub Pages
- Add a
CNAMEfile with your domain name - Configure DNS records to point to GitHub
- See GitHub Pages documentation for details
- For a detailed walkthrough using AWS Route53, see Setting Up Custom Domain for GitHub Pages with Route53
Branch and Folder Options
Source options:
- Deploy from a branch: Serves files directly from a branch
- GitHub Actions: Uses workflow to build and deploy (recommended)
Folder options:
-
/ (root): Serves files from repository root -
/docs: Serves files fromdocsfolder - Custom folder: Any folder in your repository
Security Considerations
- Public repositories: Content is publicly accessible
- Private repositories: Requires GitHub Pro for Pages
- Secrets: Don't commit API keys or sensitive data
- HTTPS: Automatically enabled for all Pages sites
Troubleshooting
Site Not Loading
Issue: Site returns 404 or doesn't load
- Check deployment status: Go to repository → Actions tab
- Verify workflow ran: Ensure the workflow completed successfully
- Check Pages settings: Verify "GitHub Actions" is selected as source
- Wait a few minutes: Initial deployment can take 5-10 minutes
- Clear browser cache: Try incognito mode or different browser
Solution:
# Check if workflow file exists
ls -la .github/workflows/pages.yml
# Verify file content is correct
cat .github/workflows/pages.yml
Workflow Fails
Issue: GitHub Actions workflow fails to deploy
- Check Actions tab: Review error messages in workflow logs
- Verify permissions: Ensure Pages permissions are enabled
- Check file paths: Ensure artifact path matches your content location
- Review workflow syntax: Validate YAML syntax
Common errors:
- Missing permissions: Add
pages: writeandid-token: write - Wrong artifact path: Use
'.'for root or correct folder path - Branch name mismatch: Ensure workflow triggers on correct branch
Custom Domain Issues
Issue: Custom domain not working
- Verify CNAME file: Must contain only the domain name
- Check DNS records: Ensure A/CNAME records are correct
- Wait for propagation: DNS changes can take up to 48 hours
- Check GitHub settings: Domain must be added in Pages settings
Verification
After deployment, verify your site:
-
Visit your site URL:
https://your-username.github.io/github-pages-demo - Check Actions tab: Verify workflow completed successfully
-
Test updates: Make a change, push to
main, verify it appears on site
Working example: See a live example at https://jdevto.github.io/github-pages-demo/
Verify deployment:
# Check if site is accessible
curl -I https://your-username.github.io/github-pages-demo
# Should return HTTP 200 status
# Example with working site:
curl -I https://jdevto.github.io/github-pages-demo/
Next Steps
Once your basic site is working, you can:
- Add more pages: Create additional HTML files
-
Add CSS styling: Create a
styles.cssfile - Add JavaScript: Create interactive features
- Use a static site generator: Jekyll, Hugo, MkDocs, etc.
- Add a custom domain: Point your own domain to GitHub Pages
- Set up CI/CD: Add tests and build steps to your workflow
Conclusion
Setting up GitHub Pages with GitHub Actions provides a simple, automated way to host static websites. By following these steps, you can:
- Create a basic hello world site
- Enable GitHub Pages manually or via Actions
- Automate deployments with GitHub Actions
- Extend your site with additional content
The combination of GitHub Pages and GitHub Actions makes it easy to deploy and maintain static websites with minimal configuration.
Top comments (0)