Setting Up GitHub Pages with GitHub Actions
Source: Dev.to
High‑Level Steps
- 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 corner.
- Select “New repository.”
Configure the repository:
| Setting | Value |
|---|---|
| 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) |
Click “Create repository.”
Key points
- Repository name becomes part of your site URL:
https://<username>.github.io/ - Public repositories get free GitHub Pages hosting.
- Private repositories require GitHub Pro (or higher) for Pages.
Step 2: Clone Repository Locally
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">
<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 a “Hello World” message.
- Basic styling for a clean appearance.
- Responsive design that works on mobile devices.
Alternative: Add any static HTML, CSS, JavaScript, or other assets you need.
Step 4: Create GitHub Actions Workflow
-
Create the workflow directory:
mkdir -p .github/workflows -
Add the workflow file
.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 pushes to
mainor when manually triggered. - Permissions – Required for Pages deployment.
- Concurrency – Prevents multiple deployments from overlapping.
- Steps
- Checkout repository code.
- Configure Pages settings.
- Upload all repository files as an artifact.
- Deploy the artifact 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 is needed for simple HTML (add build steps if using a framework).
path: '.'uploads the entire repository root.
Step 5: Enable GitHub Pages
Important: Enable GitHub Pages before pushing the workflow file. If the workflow runs first, the
github-pagesenvironment won’t exist, causing the job to fail.
Step 5.1: Navigate to Settings
- Go to your repository on GitHub.
- Click “Settings” in the top navigation bar.
- In the left sidebar, select “Pages.”
Step 5.2: Configure Source
Under “Build and deployment,” locate “Source.”
- Choose “GitHub Actions.”
- The selection is saved automatically.
What this does
- Enables GitHub Pages for the repository.
- Configures Pages to use the GitHub Actions workflow for deployment.
- Creates the
github-pagesenvironment required by the workflow.
Verify Deployment
-
Commit and push your changes (including
index.htmland the workflow file) to themainbranch:git add . git commit -m "Add site content and Pages workflow" git push origin main -
Navigate to the Actions tab to watch the workflow run.
-
Once the workflow completes, go to Settings → Pages to find the live site URL (e.g.,
https://<username>.github.io/github-pages-demo). -
Open the URL in a browser – you should see the “Hello World!” page.
Recap
- Created a public repository.
- Added a simple static site (
index.html). - Set up a GitHub Actions workflow to deploy the site automatically.
- Enabled GitHub Pages with GitHub Actions as the source.
- Verified that the site is live.
You now have a fully automated GitHub Pages site that updates on every push to main. Feel free to expand the site with additional pages, assets, or a static site generator—just adjust the workflow accordingly.
Step 6: Commit and Push
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
main(ormaster) branch 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
7.1 Wait for Deployment
- GitHub Actions detects the workflow file and runs automatically.
- GitHub builds and deploys your site (may take a few minutes).
- A green check‑mark indicates the deployment is complete.
- Monitor progress in the Actions tab.
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.
7.3 Add Website to Repository About Section (Optional)
- On the repository page, click the gear icon (⚙️) next to the About section.
- In the Edit repository details modal, locate the Website field.
- Enter your GitHub Pages URL (e.g.,
https://your-username.github.io/github-pages-demo). - Check “Use your GitHub Pages website” – this auto‑populates the URL.
- Click Save changes.
What this does
- Shows the site URL in the repository’s About section.
- Provides a direct link for visitors.
- Improves discoverability of your GitHub Pages site.
Note: Future pushes to
mainwill automatically trigger the workflow and deploy updates.
Complete Example
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 4.
Repository Structure Summary
| 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 – 1 GB repository size, 100 MB per file.
- Bandwidth – 100 GB per month for free accounts.
- Build time – 10 builds per hour limit.
Custom Domain Support
- Add a
CNAMEfile containing your domain name. - Configure DNS records to point to GitHub.
- See the official GitHub Pages documentation for details.
For a walkthrough using AWS Route 53, see Setting Up Custom Domain for GitHub Pages with Route53.
Branch and Folder Options
| Source option | Description |
|---|---|
| Deploy from a branch | Serves files directly from a branch. |
| GitHub Actions | Uses a workflow to build and deploy (recommended). |
| Folder option | Description |
|---|---|
/ (root) | Serves files from the repository root. |
/docs | Serves files from a docs folder. |
| custom | Any folder you specify in the settings. |
Security Considerations
- Public repositories – content is publicly accessible.
- Private repositories – requires a GitHub Pro plan for Pages.
- Secrets – never commit API keys or other sensitive data.
- HTTPS – automatically enabled for all Pages sites.
Troubleshooting
Site Not Loading
Symptoms: 404 error or site does not load.
Steps to resolve
- Check deployment status – go to Repository → Actions tab.
- Verify workflow ran – ensure it completed successfully.
- Check Pages settings – confirm “GitHub Actions” is selected as the source.
- Wait a few minutes – initial deployment can take 5‑10 minutes.
- Clear browser cache – try incognito mode or a different browser.
Useful commands
# Verify workflow file exists
ls -la .github/workflows/pages.yml
# View its contents
cat .github/workflows/pages.yml
Workflow Fails
Symptoms: GitHub Actions workflow errors.
Resolution checklist
- Review error messages in the Actions tab logs.
- Ensure Pages permissions are enabled (
pages: write,id-token: write). - Verify artifact paths match your content location (
'.'for root, or correct folder). - Confirm the workflow triggers on the correct branch.
- Validate YAML syntax (use an online linter if needed).
Common errors
- Missing permissions: add
pages: writeandid-token: write. - Wrong artifact path: adjust to
'.'or the appropriate folder. - Branch name mismatch: ensure the
on:trigger matches your branch.
Custom Domain Issues
Symptoms: Custom domain not working.
Resolution steps
- Check
CNAMEfile – it must contain only the domain name (no extra whitespace). - Verify DNS records – A/CNAME records should point to GitHub’s servers.
- Allow propagation – DNS changes can take up to 48 hours.
- Confirm GitHub settings – the domain must be added under Pages settings.
Verification
After deployment, verify your site:
- Visit the site URL.
- Ensure the “Hello World!” content displays correctly.
- Check the Actions tab for a successful run.