Deploy your Angular app to GitHub pages directly from the Angular CLI! ๐
Table of contents:
- ๐ Changelog
โ ๏ธ Prerequisites- ๐ Quick Start (local development)
- ๐ Continuous Delivery
- ๐ฆ Options
- ๐ Next milestones
โ๏ธ FAQ
A detailed changelog is available in the releases section.
In the past this project was a standalone program. This is still possible: See the documentation at README_standalone.
This command has the following prerequisites:
- Git 1.9 or higher (execute
git --version
to check your version) - Angular project created via Angular CLI v8.3.0-next.0 or greater (execute
ng update @angular/[email protected] @angular/[email protected]
to upgrade your project if necessary)
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 next version of the Angular CLI (v8.3.0-next.0 or greater) globally and create a new Angular project.
npm install -g @angular/cli@next ng new your-angular-project --defaults cd your-angular-project
-
By default the Angular CLI initializes a Git repository for you.
To add a new remote for GitHub, use thegit remote add
command:git remote add origin https://github.com/<username>/<repositoryname>.git
Hints:
- Create a new empty GitHub repository first.
- Replace
<username>
and<repositoryname>
with your username from GitHub and the name of your new repository. - Please enter the URL
https://github.com/<username>/<repositoryname>.git
into 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!
-
Add
angular-cli-ghpages
to 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
Which is the same as:
ng run your-angular-project:deploy
-
Your project should be available at
https://<username>.github.io/<repositoryname>
.
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 GitHub tokens. In short: a GitHub 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 GH_TOKEN
in your CI/CD environment.
You should also set the URL to the repository using the --repo
option.
The URL must use the HTTPS scheme.
ng deploy --repo=https://github.com/<username>/<repositoryname>.git --name="Your Git Username" [email protected]
(replace <username>
and <repositoryname>
with your username from GitHub and the name of your repository)
Please do NOT disable the silent mode if you have any credentials in the repository URL! You have to treat the GH_TOKEN as secure as a password!
- optional
- Default:
undefined
(string) - Example:
ng deploy
โ The tag<base href="/">
remains unchanged in yourindex.html
ng deploy --base-href=/the-repositoryname/
โ The tag<base href="/the-repositoryname/">
is added to yourindex.html
Specifies the base URL for the application being built.
Same as ng build --base-href=/XXX/
โน๏ธ Please read the next lines carefully, or you will get 404 errors in case of a wrong configuration!
If you don't want to use an own domain, then your later URL of your hosted Angular project should look like this:
https://your-username.github.io/the-repositoryname
.
In this case you have to adjust the --base-href
accordingly:
ng deploy --base-href=/the-repositoryname/
If you want to use your own domain, then you don't have to adjust --base-href
.
However, it is now necessary to set the --cname
parameter!
ng deploy --cname=example.org
See the option --cname for more information!
- optional
- Default: URL of the origin remote of the current dir (assumes a Git repository)
- Example:
ng deploy --repo=https://github.com/<username>/<repositoryname>.git
By default, this command assumes that the current working directory is a Git repository,
and that you want to push changes to the origin
remote.
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 repo
option.
Hint:
Set an environment variable with the name GH_TOKEN
and it will be automatically added to the URL.
(https://github.com/<username>/<repositoryname>.git
is changed to https://[email protected]/<username>/<repositoryname>.git
if there is an environment variable GH_TOKEN
with the value XXX
.
Learn more about GitHub tokens here.)
- optional
- Default:
production
(string) - Example:
ng deploy
โ Angular project is build in production modeng deploy --configuration=qs
โ Angular project is using the configurationqs
(this configuration must exist in theangular.json
file)
A named build target, as specified in the configurations
section of angular.json
.
Each named target is accompanied by a configuration of option defaults for that target.
Same as ng build --configuration=XXX
.
- optional
- Default:
Auto-generated commit
(string) - Example:
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 handy additional text is always added,
if the environment variable TRAVIS
exists (for Travis CI) or
if the environment variable CIRCLECI
exists (for Circle CI).
- optional
- Default:
gh-pages
(string) - Example:
ng deploy --branch=master
The name of the branch you'll be pushing to.
The default uses GitHub's gh-pages
branch,
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).
- optional
- Default: value of
git config user.name
andgit config user.email
- Example:
ng deploy --name="Displayed Username" [email protected]
If you run the command in a repository without user.name
or 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 name
and email
string values to identify the committer.
- optional
- Default: silent
true
(boolean) - Example:
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, log messages are suppressed and error messages are sanitized.
The --no-silent
option enables extended console logging.
Keep this untouched if the repository URL or other information passed to git commands is sensitive!
โ ๏ธ WARNING: 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 theGH_TOKEN
feature). By default the silent mode is enabled to avoid sensitive data exposure.
- optional
- Default: dotfiles
true
(boolean) - Example:
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).
With --no-dotfiles
files starting with .
are ignored.
Hint:
This is super useful if you want to publish a .nojekyll
file.
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!
- optional
- Default:
undefined
(string) โ No CNAME file is generated - Example:
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.
- optional
- Default:
false
(boolean) - Example:
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.
We are glad that we have an integration into the CLI again. However, we are looking forward to the following features:
- an interactive command-line prompt that guides you through the available options
- a configuration file (
angular-cli-ghpages.json
) to avoid all these command-line cmd options - your feature that's not on the list yet?
We look forward to any help. PRs are welcome! ๐
Before posting any issue, please read the FAQ first. See the contributors documentation at README_contributors if you want to debug and test this project.
Code released under the MIT license.
ยฉ 2019 https://angular.schule
This project is made on top of tschaub/gh-pages.
Thank you very much for this great foundation!