Lesson Goal
By the end of this lesson, the student will be able to deploy an Angular project on GitHub Pages.
1. Level 0 Understanding
Angular is used to build web applications. But GitHub Pages does not run Angular source code directly. GitHub Pages can only host static files like HTML, CSS, and JavaScript.
| Thing | Meaning |
|---|---|
| Angular | Framework for building web apps. |
| GitHub | Place where our code is stored. |
| GitHub Pages | Free hosting for static websites. |
| Build | Converts Angular project into HTML, CSS, and JavaScript files. |
| Deploy | Put the final website online. |
2. Install Required Tools
Before starting, install Node.js, npm, Git, and Angular CLI.
Check Node, npm, and Git
node -v
npm -v
git --version
Install Angular CLI
npm install -g @angular/cli
Check Angular CLI
ng version
3. Create a New Angular App
We will create a sample app called:
programmers-picnic-angular-demo
Create Project
ng new programmers-picnic-angular-demo
When Angular asks questions, choose simple options:
| Question | Recommended Answer |
|---|---|
| Stylesheet format | CSS |
| Server-Side Rendering | No |
Go Inside Project
cd programmers-picnic-angular-demo
Run Locally
ng serve
Open this in the browser:
http://localhost:4200
4. Edit the Angular Home Page
Open this file:
src/app/app.html
Replace the content with this:
<main class="page">
<section class="hero">
<h1>Programmers Picnic Angular Demo</h1>
<p>Built by Champak Roy</p>
<p>This Angular project is running on GitHub Pages.</p>
</section>
</main>
Now open:
src/app/app.css
Add this CSS:
.page {
min-height: 100vh;
display: grid;
place-items: center;
background: #fff7ed;
color: #241407;
font-family: Arial, sans-serif;
}
.hero {
max-width: 720px;
padding: 48px;
border-radius: 28px;
background: white;
box-shadow: 0 20px 50px rgba(120, 53, 15, 0.15);
text-align: center;
border: 1px solid #fed7aa;
}
.hero h1 {
color: #d97706;
font-size: 42px;
margin-bottom: 12px;
}
.hero p {
font-size: 20px;
}
5. Push the Angular Project to GitHub
Create a new GitHub repository with this name:
programmers-picnic-angular-demo
Then run these commands inside your Angular project folder:
git init
git add .
git commit -m "Initial Angular project"
git branch -M main
git remote add origin https://github.com/YOUR_GITHUB_USERNAME/programmers-picnic-angular-demo.git
git push -u origin main
6. Understand the Most Important Part: Base Href
A normal website may run from the root path:
/
But a GitHub Pages project site usually runs from a repository folder:
/programmers-picnic-angular-demo/
Correct build command:
ng build --configuration production --base-href /programmers-picnic-angular-demo/
7. Deploy Angular to GitHub Pages
The easiest beginner method is to use angular-cli-ghpages.
Add Deployment Package
ng add angular-cli-ghpages
Deploy
ng deploy --base-href=/programmers-picnic-angular-demo/
Now open your GitHub repository and go to:
Settings → Pages
Choose:
Source: Deploy from a branch
Branch: gh-pages
Folder: / root
Your final site will be:
https://YOUR_GITHUB_USERNAME.github.io/programmers-picnic-angular-demo/
8. Add Useful npm Scripts
Open:
package.json
Inside the scripts section, add:
"build:github": "ng build --configuration production --base-href /programmers-picnic-angular-demo/",
"deploy:github": "ng deploy --base-href=/programmers-picnic-angular-demo/"
Now you can deploy using:
npm run deploy:github
9. Fix Angular Routing on GitHub Pages
If you use Angular routes like /about or /courses, the app may show 404 when the browser is refreshed.
Beginner-friendly fix: use hash routing.
Your URLs will look like this:
https://username.github.io/repo/#/about
Open:
src/app/app.config.ts
Use this router setup:
import { ApplicationConfig } from '@angular/core';
import { provideRouter, withHashLocation } from '@angular/router';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes, withHashLocation())
]
};
10. Common Errors and Fixes
| Problem | Reason | Fix |
|---|---|---|
| Blank page | Wrong base href | Use --base-href=/repo-name/ |
| CSS missing | Files are loading from the wrong path | Rebuild and redeploy with correct base href |
| Images missing | Image path starts with slash |
Use assets/logo.png, not
/assets/logo.png
|
| 404 on refresh | Angular route not found by GitHub Pages | Use hash routing |
| Site not updated | GitHub Pages needs time or cache is old | Wait a little, hard refresh, or redeploy |
11. Final Deployment Checklist
12. Student Assignment
Create and deploy an Angular app called:
student-dashboard-angular
The app should contain:
Expected Final URL
https://YOUR_GITHUB_USERNAME.github.io/student-dashboard-angular/
Final Command Summary
ng new programmers-picnic-angular-demo
cd programmers-picnic-angular-demo
ng serve
git init
git add .
git commit -m "Initial Angular project"
git branch -M main
git remote add origin https://github.com/YOUR_GITHUB_USERNAME/programmers-picnic-angular-demo.git
git push -u origin main
ng add angular-cli-ghpages
ng deploy --base-href=/programmers-picnic-angular-demo/
ng deploy --base-href=/YOUR_REPOSITORY_NAME/
13. Change GitHub Pages URL to Custom Domain
Till now, our Angular app was planned for a GitHub Pages URL like this:
https://YOUR_GITHUB_USERNAME.github.io/programmers-picnic-angular-demo/
Now we want to run the same Angular app on our own subdomain:
https://angular.learnwithchampak.live/
/repo-name/. But when Angular runs from a custom
domain root, it needs base href /.
Old GitHub Pages URL
https://YOUR_GITHUB_USERNAME.github.io/programmers-picnic-angular-demo/
For this old URL, the deploy command was:
ng deploy --base-href=/programmers-picnic-angular-demo/
New Custom Domain URL
https://angular.learnwithchampak.live/
For this new custom domain, use this deploy command:
ng deploy --base-href=/
https://angular.learnwithchampak.live/, the
correct base href is only /.
Step 1: Create CNAME File in Angular Project
In your Angular project, create a file named:
public/CNAME
Inside this file, write only the custom domain:
angular.learnwithchampak.live
Step 2: Save and Push the CNAME File
git add .
git commit -m "Add custom domain"
git push
Step 3: Deploy Angular Again
Because the app will now open from the root of the custom domain, deploy with:
ng deploy --base-href=/
Step 4: Add the Custom Domain in GitHub
Open your GitHub repository and go to:
Settings → Pages → Custom domain
Enter this domain:
angular.learnwithchampak.live
Then click Save.
Step 5: Add DNS Record
Go to the DNS manager for learnwithchampak.live. Add this record:
| Field | Value |
|---|---|
| Type | CNAME |
| Name / Host | angular |
| Value / Points to | YOUR_GITHUB_USERNAME.github.io |
Example:
Type: CNAME
Name: angular
Value: programmer-s-picnic.github.io
https:// in the DNS value. Do not add
the repository name in the DNS value.
Step 6: Wait and Test
DNS changes may take some time. After that, open:
https://angular.learnwithchampak.live/
Step 7: Enable HTTPS
After GitHub verifies the DNS, go to:
Settings → Pages
Enable:
Enforce HTTPS
Final Command Summary
# Create CNAME file
echo angular.learnwithchampak.live > public/CNAME
# Save the change
git add .
git commit -m "Add custom domain"
git push
# Deploy for custom domain root
ng deploy --base-href=/
Common Mistakes
| Mistake | Why it is wrong | Correct version |
|---|---|---|
ng deploy
--base-href=/programmers-picnic-angular-demo/
|
This is for GitHub repo URL, not custom domain root. | ng deploy --base-href=/ |
DNS value:
https://programmer-s-picnic.github.io
|
DNS values should not include https://.
|
programmer-s-picnic.github.io |
DNS value:
programmer-s-picnic.github.io/programmers-picnic-angular-demo
|
CNAME should point to GitHub Pages default domain only. | programmer-s-picnic.github.io |
CNAME content:
https://angular.learnwithchampak.live/
|
CNAME file should contain only the domain name. | angular.learnwithchampak.live |