Deploy your Angular app to GitHub pages directly from the Angular CLI!
Table of contents:
A detailed changelog is available in the releases section.
Starting with version 1 the option --configuration
was renamed to --build-target
.
BEFORE (does not work):
ng deploy --configuration=test
NOW:
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_TOKEN
is also 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 --version
to check your version)ng update
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 latest version of the Angular CLI globallyand create a new Angular project.
npm install -g @angular/cli
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 the git remote add
command:
git remote add origin https://github.com/<username>/<repositoryname>.git
Hints:
<username>
and <repositoryname>
with your username from GitHub and the name of your new repository.https://github.com/<username>/<repositoryname>.git
into your browser – you should see your existing repository on GitHub.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 --base-href=/<repositoryname>/
Which is the same as:
ng deploy your-angular-project --base-href=/<repositoryname>/
Please be aware of the --base-href
option. It is necessary when your project will be deployed to a non-root folder. See more details below.
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 "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 GH_TOKEN
(or 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 --repo
option.
If you specify all the three options (--repo
, --name
and --email
), then angular-cli-ghpages will also work in directories that are not under version control at all.
ng deploy --repo=https://github.com/<username>/<repositoryname>.git --name="Your Git Username" --email=your.mail@example.org
(replace <username>
and <repositoryname>
with your username from GitHub and the name of your repository)
⚠️ ImportantPlease 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 ActionsThe
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 theGH_TOKEN
(personal access token).
undefined
(string)ng deploy
– The tag <base href="/">
remains unchanged in your index.html
ng deploy --base-href=/the-repositoryname/
– The tag <base href="/the-repositoryname/">
is added to your index.html
Specifies the base URL for the application being built.Same as ng build --base-href=/XXX/
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!
undefined
(string)ng deploy
– Angular project is built in production
modeng deploy --build-target=test
– Angular project is using the build configuration test
(this configuration must exist in the angular.json
file)If no buildTarget
is set, the production
build of the default project will be chosen.The buildTarget
simply points to an existing build configuration for your project, as specified in the configurations
section of angular.json
.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 to calling the command ng build --configuration=XXX
.This command has no effect if the option --no-build
is active.
This option was called --configuration
in previous versions.
BEFORE (does not work):
ng deploy --configuration=test
NOW:
ng deploy --build-target=test
false
(string)ng deploy
– Angular project is build in production mode before the deploymentng deploy --no-build
– Angular project is NOT buildSkip 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 --build-target
setting to have no effect.
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 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.
ℹ️ HintSet an environment variable with the name
GH_TOKEN
/PERSONAL_TOKEN
orGITHUB_TOKEN
and it will be automatically added to the URL, if it uses the HTTPS shema (it must start withhttps://github.com
).Tokens are generally not supported for Git over SSH (starts withgit@github.com
).
Learn more about "personal access tokens" here (GH_TOKEN
) and about the "installation access token" here (GITHUB_TOKEN
). PERSONAL_TOKEN
is an alias for GH_TOKEN
.
Auto-generated commit [ci skip]
(string)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.
gh-pages
(string)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).
git config user.name
and git config user.email
ng deploy --name="Displayed Username" --email=mail@example.org
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.
true
(boolean)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)'
)
The --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!
⚠️ WARNINGThis 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.
true
(boolean)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!
undefined
(string) – No CNAME file is generatedng 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.
false
(boolean)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.
Example:
ng deploy --base-href=https://angular-schule.github.io/angular-cli-ghpages/ --name="Angular Schule Team" --email=team@angular.schule
becomes
"deploy": {
"builder": "angular-cli-ghpages:deploy",
"options": {
"baseHref": "https://angular-schule.github.io/angular-cli-ghpages/",
"name": "Angular Schule Team",
"email": "team@angular.schule"
}
}
Now you can just run ng deploy
without all the options in the command line!
ℹ️ HintYou 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.
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.
This project is made on top of tschaub/gh-pages.
Thank you very much for this great foundation!
一 、 注册github账号并创建github仓库(repository) 二 、 下载安装git工具(用到git命令),下载安装node.js(需要用到npm) 三 、 创建angular项目,初始化git,安装angular-cli-ghpages,构建部署 1. 创建angular项目 npm install -g @angular/cli ng new 你的angular项目名 --def
GitHub有一个很实用的功能就是GitHub Pages,他相当于为Github项目建立了一个可视化的web页面。angular-cli-ghpages的功能就与这个类似。 首先新建一个Github空项目,比如angular-deploy 然后使用angular-cli工具生成一个新项目 安装angular-cli-ghpages包 npm install angular-cli-ghpages
Angular_PWA使用+Demo 什么是PWA PWA(Progressive Web App)利用TLS,webapp manifests和service workers使应用程序能够安装并离线使用。 换句话说,PWA就像手机上的原生应用程序,但它是使用诸如HTML5,JavaScript和CSS3之类的网络技术构建的。 如果构建正确,PWA与原生应用程序无法区分。 PWA 的主要特点包括下
https://jeneser.github.io/blog/2017/08/08/angular-deploying-app-github-pages/ Published: August 08, 2017 by jeneser Categories: angular 1 Tags: angular 2 course 2 original 4 你将看到:快速部署angular应用到github
angular8 rest by Bruno Krebs 布鲁诺·克雷布斯(Bruno Krebs) 让我们构建具有Angular,持久性和安全性的无服务器REST API (Let’s Build a Serverless REST API with Angular, Persistence, and Security) In this post I’ll show you how you ca
当你准备把 Angular 应用部署到远程服务器上时,有很多可选的部署方式。 ###最简单的部署选项 在完整部署应用之前,你可以先临时用一种技术来测试流程、构建配置和部署行为 从磁盘构建和提供服务 在开发过程中,你通常会使用 ng serve 命令来借助 webpack-dev-server 在本地内存中构建、监控和提供服务。但是,当你打算部署它时,就必须使用 ng build 命令来构建应用并在
使用angular官方提供的npm插件部署项目到GitHub Pages. 步骤: 执行下面命令,安装插件 ng add angular-cli-ghpages 执行下面命令,部署项目 这条命令,会生成一个gh-pages分支,并且将Angular build生成的文件放到这个分支下面,并且将分支上传到GitHub仓库。 ng deploy --base-href=“https://pages.
从头开始创建项目 lint你的代码 运行您的单元测试和端到端测试。 Angular 2 CLI目前只在TypeScript中生成框架,稍后还会有其他版本。
Angular CLI Development tools and libraries specialized for Angular This is the home of the DevKit and the Angular CLI code. You can find the Angular CLI specific READMEhere. Quick Links Gitter Contri
ng lint lints the code in your project using tslint. You can customize the rules for your project by editing tslint.json. You can switch some of these to use your preferred tool by editing the scripts
端到端(e2e)测试
安装 可以使用以下命令安装Angular 2 CLI: