{"id":4171,"date":"2023-11-04T23:14:06","date_gmt":"2023-11-04T23:14:06","guid":{"rendered":"http:\/\/localhost:10003\/how-to-deploy-an-angular-app-to-github-pages\/"},"modified":"2023-11-05T05:47:58","modified_gmt":"2023-11-05T05:47:58","slug":"how-to-deploy-an-angular-app-to-github-pages","status":"publish","type":"post","link":"http:\/\/localhost:10003\/how-to-deploy-an-angular-app-to-github-pages\/","title":{"rendered":"How to Deploy an Angular App to GitHub Pages"},"content":{"rendered":"
In this tutorial, we will learn how to deploy an Angular application to GitHub Pages. GitHub Pages is a static site hosting service provided by GitHub which allows you to host your website directly from your GitHub repository. Deploying your Angular app to GitHub Pages is a convenient way to make your application accessible to the public.<\/p>\n
By the end of this tutorial, you will have a fully deployed Angular app on GitHub Pages that you can share with others.<\/p>\n
Before we begin, make sure you have the following prerequisites:<\/p>\n
npm install -g @angular\/cli<\/code><\/li>\n<\/ol>\nStep 1: Create a New Angular App<\/h2>\n
First, we need to create a new Angular app. Open your terminal and run the following command to create a new Angular app:<\/p>\n
ng new my-angular-app\n<\/code><\/pre>\nThis command will create a new directory called my-angular-app<\/code> with the basic structure of an Angular app.<\/p>\nNavigate to the app’s directory by running:<\/p>\n
cd my-angular-app\n<\/code><\/pre>\nStep 2: Build the Angular App<\/h2>\n
Next, we need to build our Angular app for production. GitHub Pages only supports static sites, so we need to ensure that our app is bundled and optimized for deployment.<\/p>\n
To build the app, run the following command:<\/p>\n
ng build --prod\n<\/code><\/pre>\nThis command will compile and bundle your Angular application into the dist<\/code> directory.<\/p>\nStep 3: Create a GitHub Repository<\/h2>\n
In order to deploy the app to GitHub Pages, we need to create a new repository on GitHub.<\/p>\n
\n- Go to GitHub<\/a> and sign in to your account (or create a new account if you don’t have one).<\/li>\n
- Click on the New repository<\/strong> button to create a new repository.<\/li>\n
- Choose a name for your repository (e.g.,
my-angular-app<\/code>) and optionally provide a description.<\/li>\n- Make sure to keep the repository public since GitHub Pages only works with public repositories.<\/li>\n
- Click the Create repository<\/strong> button to create the repository.<\/li>\n<\/ol>\n
Step 4: Connect the Local App to the GitHub Repository<\/h2>\n
To connect the local app to the GitHub repository, we need to add the remote repository URL to the app’s configuration.<\/p>\n
\n- Open the
.git\/config<\/code> file in your app’s root directory.<\/li>\n- Add the following lines to the file, replacing
username<\/code> with your GitHub username and repository-name<\/code> with the name of your GitHub repository:<\/li>\n<\/ol>\n[remote \"origin\"]\n url = https:\/\/github.com\/username\/repository-name.git\n fetch = +refs\/heads\/*:refs\/remotes\/origin\/*\n<\/code><\/pre>\nSave the file and close it.<\/p>\n
Step 5: Deploy the App to GitHub Pages<\/h2>\n
We are now ready to deploy our Angular app to GitHub Pages.<\/p>\n
\n- Open your terminal and navigate to your app’s root directory.<\/li>\n
- Run the following command to deploy the app:
\nngh --dir=dist\/my-angular-app<\/code><\/li>\n<\/ol>\nThis command will build your app and deploy the contents of the dist\/my-angular-app<\/code> directory to the gh-pages<\/code> branch of your GitHub repository.<\/p>\nStep 6: Configure GitHub Pages<\/h2>\n
In order to access your deployed app on GitHub Pages, we need to configure the settings of your repository.<\/p>\n
\n- Go to your GitHub repository page.<\/li>\n
- Click on the Settings<\/strong> tab.<\/li>\n
- Scroll down to the GitHub Pages<\/strong> section.<\/li>\n
- Under the Source<\/strong> dropdown, select the
gh-pages<\/code> branch.<\/li>\n- Click on the Save<\/strong> button.<\/li>\n<\/ol>\n
After saving, GitHub will provide you with the URL where your app is deployed. It may take a few minutes for the changes to propagate.<\/p>\n
Step 7: Access Your Deployed App<\/h2>\n
Congratulations! You have successfully deployed your Angular app to GitHub Pages. To access your deployed app, simply navigate to the URL provided by GitHub in the previous step.<\/p>\n
Note that it may take a few minutes for the changes to propagate, so if you don’t see your app immediately, wait for a while and try again.<\/p>\n
Updating and Redeploying Your App<\/h2>\n
If you make changes to your app and want to update the deployed version on GitHub Pages, follow these steps:<\/p>\n
\n- Build your app by running
ng build --prod<\/code> in the app’s root directory.<\/li>\n- Deploy the updated version by running
ngh --dir=dist\/my-angular-app<\/code> in the app’s root directory.<\/li>\n- Wait for the changes to propagate and access your updated app using the same URL provided by GitHub.<\/li>\n<\/ol>\n
Repeat these steps whenever you need to update your deployed app.<\/p>\n
Conclusion<\/h2>\n
In this tutorial, we have learned how to deploy an Angular app to GitHub Pages. We covered the necessary steps to create a GitHub repository, connect it to your local app, and deploy the app using the Angular CLI and the ngh<\/code> tool. We also learned how to configure GitHub Pages to make our app accessible to the public.<\/p>\nNow you can easily share your Angular app with others by simply deploying it to GitHub Pages. Happy deploying!<\/p>\n","protected":false},"excerpt":{"rendered":"
In this tutorial, we will learn how to deploy an Angular application to GitHub Pages. GitHub Pages is a static site hosting service provided by GitHub which allows you to host your website directly from your GitHub repository. Deploying your Angular app to GitHub Pages is a convenient way to Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[1547,1546,1545,1543,1542,1544,1541],"yoast_head":"\nHow to Deploy an Angular App to GitHub Pages - Pantherax Blogs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n