Deploy your Angular app to GitHub pages directly from the Angular CLI! 🚀
Table of contents:
- 📖 Changelog
- ⚠️ Prerequisites
- 🚀 Quick Start (local development)
- 🚀 Continuous Delivery
- 📦 Options
- 📁 Configuration File
- 🌍 Environments
- ⁉️ FAQ
A detailed changelog is available in the releases section.
⚠️ BREAKING CHANGE (v1)
Starting with version 1 the option
--configuration was renamed to
BEFORE (does not work):
ng deploy --configuration=test
ng deploy --build-target=test
If you use the old syntax, you will probably receive the following error:
An unhandled exception occurred: Configuration 'test' is not set in the workspace.
🐙 GitHub Actions is now a first citizen alongside Travis CI and CircleCi. The token
GITHUB_TOKEN is now supported. Learn everything you need to know in the following article.
Everything GitHub: Continuous Integration, Deployment and Hosting for your Angular App
In this article we show several tools from the GitHub universe to launch a website with Angular. We will establish a professional pipeline, including version management, continuous deployment and web hosting. Best of all, for public repositories, this will not cost you a single cent! Read more...
This command has the following prerequisites:
- Git 1.9 or higher (execute
git --versionto check your version)
- Angular project created via Angular CLI v9.0.0 or greater (execute
ng updateto upgrade your project if necessary)
- older Angular projects can still use the standalone program. See the documentation at README_standalone.
This quick start assumes that you are starting from scratch. If you already have an existing Angular project on GitHub, skip step 1 and 2.
Install the latest version of the Angular CLI globally and create a new Angular project.npm install -g @angular/cling new your-angular-project --defaultscd your-angular-project
By default the Angular CLI initializes a Git repository for you.
To add a new remote for GitHub, use the
git remote addcommand:git remote add origin https://github.com/<username>/<repositoryname>.git
- Create a new empty GitHub repository first.
<repositoryname>with your username from GitHub and the name of your new repository.
- Please enter the URL
https://github.com/<username>/<repositoryname>.gitinto your browser – you should see your existing repository on GitHub.
- Please double-check that you have the necessary rights to make changes to the given project!
angular-cli-ghpagesto your project.ng add angular-cli-ghpages
Deploy your project to GitHub pages with all default settings. Your project will be automatically built in production mode.ng deploy --base-href=/<repositoryname>/
Which is the same as:ng deploy your-angular-project --base-href=/<repositoryname>/
Please be aware of the
--base-hrefoption. It is necessary when your project will be deployed to a non-root folder. See more details below.
Your project should be available at
Learn more about GitHub pages on the official website.
If you run this command from a CI/CD environment, the deployment will most likely not work out of the box.
For security reasons, those environments usually have read-only privileges or you haven't set up Git correctly.
Therefore you should take a look at "personal access tokens"
GH_TOKEN (which works everywhere) and the "installation access token"
GITHUB_TOKEN (which is exclusively provided by GitHub actions).
In short: a token replaces username and password and is a safer choice because a token can be revoked at any time.
All you need to do is to set an environment variable called
PERSONAL_TOKEN) in your CI/CD environment.
For GitHub Actions, you can also use the
GITHUB_TOKEN which provides more security and requires no additional setup.
All the tokens only work if the remote repository uses the HTTPS scheme.
Tokens are generally not supported for Git over SSH.
If the current working directory is already a git repository, you don't need to specify the repository again. The current remote repository with the name
origin will be used in this case.
You can also override the repository setting using the
If you specify all the three options (
ng deploy --repo=https://github.com/<username>/<repositoryname>.git --name="Your Git Username" --email@example.com
<repositoryname> with your username from GitHub and the name of your repository)
Please do not disable the silent mode if you use tokens, otherwise people could read them in the output logs. If you are sure that your CI/CD provider does not display secrets on the console (this applies to CircleCI / Travis CI and Github Actions), you are welcome to disable silent mode.
ℹ️ Note for GitHub Actions
GITHUB_TOKEN(installation access token) will only trigger a release of a new website if the action runs in a private repository. In a public repo, a commit is generated, but the site does not change. See this GitHub Community post for more info. If your repo is public, you must still use the
GH_TOKEN(personal access token).
ng deploy– The tag
<base href="/">remains unchanged in your
ng deploy --base-href=/the-repositoryname/– The tag
<base href="/the-repositoryname/">is added to your
Specifies the base URL for the application being built.
ng build --base-href=/XXX/
ℹ️ Please read the next lines carefully, or you will get 404 errors in case of a wrong configuration!
A) You don't want to use a custom domain
If you don't want to use an own domain, then your later URL of your hosted Angular project should look like this:
In this case you have to adjust the
ng deploy --base-href=/the-repositoryname/
B) You want to use a custom domain
If you want to use your own domain, then you don't have to adjust
However, it is now necessary to set the
ng deploy --cname=example.org
See the option --cname for more information!
ng deploy– Angular project is build in
ng deploy --build-target=test– Angular project is using the build configuration
test(this configuration must exist in the
If no buildTarget is set, the
production build of the default project will be chosen.
buildTarget simply points to an existing build configuration for your project, as specified in the
configurations section of
Most projects have a default configuration and a production configuration (commonly activated by using the
--prod flag) but it is possible to specify as many build configurations as needed.
This is equivalent as calling the command
ng build --configuration=XXX."
This command has no effect if the option
--no-build is active.
⚠️ BREAKING CHANGE (v1)
This option was called
--configuration in previous versions.
BEFORE (does not work):
ng deploy --configuration=test
ng deploy --build-target=test
ng deploy– Angular project is build in production mode before the deployment
ng deploy --no-build– Angular project is NOT build
Skip build process during deployment.
This can be used when you are sure that you haven't changed anything and want to deploy with the latest artifact.
This command causes the
--configuration setting to have no effect.
- Default: URL of the origin remote of the current dir (assumes a Git repository)
ng deploy --repo=https://github.com/<username>/<repositoryname>.git
This specifies the target repository. If none is given as an option, the repository is discovered from the current working directory.
By default, this command assumes that the current working directory is a Git repository,
and that you want to push changes to the
If instead your files are not in a git repository, or if you want to push to another repository,
you can provide the repository URL in the
Set an environment variable with the name
GITHUB_TOKENand it will be automatically added to the URL, if it uses the HTTPS shema (it must start with
https://github.com). Tokens are generally not supported for Git over SSH (starts with
ng deploy --message="What could possibly go wrong?"
The commit message must be wrapped in quotes if there are any spaces in the text.
Some additional text is always added to the message, if the command runs on Travis CI, Circle CI or GitHub Actions.
ng deploy --branch=master
The name of the branch you'll be pushing to.
The default uses GitHub's
but this can be configured to push to any branch on any remote.
You have to change this to
master if you are pushing to a GitHub organization page (instead of a GitHub user page).
- Default: value of
git config user.nameand
git config user.email
ng deploy --name="Displayed Username" --firstname.lastname@example.org
If you run the command in a repository without
user.email Git config properties
(or on a machine without these global config properties),
you must provide user info before Git allows you to commit.
In this case, provide both
- Default: silent
ng deploy– Logging is in silent mode by default.
ng deploy --no-silent– Logging shows extended information.
Logging is in silent mode by default.
In silent mode, the error messages for git operations are always sanitized.
(The message is always:
'Unspecified error (run without silent option for detail)')
--no-silent option enables detailed error messages and extended console logging.
Keep this untouched if the repository URL or other information passed to git commands is sensitive!
This option should be kept as it is if the repository URL or other information passed to Git commands is sensitive and should not be logged (== you have a public build server and you are using the token feature). By default the silent mode is enabled to avoid sensitive data exposure.
- Default: dotfiles
ng deploy– Dotfiles are included by default.
ng deploy --no-dotfiles– Dotfiles are ignored.
The command includes dotfiles by default (e.g.
.htaccess will be committed).
--no-dotfiles files starting with
. are ignored.
This is super useful if you want to publish a
Create such a file in the root of your pages repo to bypass the Jekyll static site generator on GitHub Pages.
Static content is still delivered – even without Jekyll.
This should only be necessary if your site uses files or directories that start with _underscores since Jekyll considers these to be special resources and does not copy them to the final site.
→ Or just don't use underscores!
undefined(string) – No CNAME file is generated
ng deploy --cname=example.com
A CNAME file will be created enabling you to use a custom domain. More information on GitHub Pages using a custom domain.
ng deploy– Normal behavior: Changes are applied.
ng deploy --dry-run– No changes are applied at all.
Run through without making any changes. This can be very useful because it outputs what would happen without doing anything.
To avoid all these command-line cmd options, you can write down your configuration in the
angular.json file in the
options attribute of your deploy project's architect. Just change the kebab-case to lower camel case. This is the notation of all options in lower camel case:
A list of all avaiable options is also available here.
ng deploy --base-href=https://angular-schule.github.io/angular-cli-ghpages/ --name="Angular Schule Team" --email@example.com
Now you can just run
ng deploy without all the options in the command line! 😄
You can always use the --dry-run option to verify if your configuration is right. The project will build but not deploy.
We have seen
angular-cli-ghpages running on various environments, like Travis CI, CircleCi or Github Actions.
Please share your knowledge by writing an article about how to set up the deployment.
Code released under the MIT license.
This project is made on top of tschaub/gh-pages.
Thank you very much for this great foundation!