Deploy Angular apps to Azure using the Angular CLI
@azure/ng-deploy helps you deploy your Angular app to Azure Static Hosting using the Angular CLI.
Install the Angular CLI (v8 or greater) and create a new Angular project.npm install -g @angular/cling new hello-world --defaultscd hello-world
ng-deployto your project and create your Azure blob storage resources.ng add @azure/ng-deploy
You may be prompted you to sign in to Azure, providing a link to open in your browser and a code to paste in the login page.
Build your Angular app.ng build --prod
Deploy your project to Azure.ng run hello-world:deploy
You will see output similar to the following. Browse to the link and view your site running in Azure blob storage!
see your deployed site at https://helloworldstatic52.z22.web.core.windows.net/
You will need the Angular CLI, an Angular project, and an Azure Subscription to deploy to Azure. Details of these requirements are in this section.
If you don't have an Azure subscription, create your Azure free account from this link.
Install the Angular CLI (v8 or greater).npm install -g @angular/cli
ng --version, make sure you have angular CLI version v8.0.0 or greater.
If need instructions to update the CLI, follow these upgrade instructions.
Update your project using the command:ng update @angular/cli @angular/core
An Angular App Created by the Angular CLI
You will need an Angular app created and managed by the Angular CLI. For help getting started with a new Angular app, check out the Angular CLI.
A simple app can be created with
ng new hello-world --defaults
Verify you have TypeScript version 3.4.5 or greater in your
package.json file of your angular project
Details of ng-azure-deploy
Add @azure/ng-deploy to your project by running:
ng add @azure/ng-deploy
This command will install the package to your project.
Once done, it will prompt you to sign in to Azure, providing a link to open in your browser and a code to paste in the login page.
After you sign in, it will create the needed resources in your Azure account (resource group and storage account) and configure them for static hosting. To manually configure the resources that will be used, refer to additional options.
Note: If you have several Azure subscriptions you will be asked to choose one.
The command will create the file
azure.json with the deployment configuration and modify
angular.json with the deploy commands.
Note: at the moment, the command will fail if an
azure.json file already exists. Please remove the file before running the command.
You can deploy your application to the selected storage account by running the following command.
ng run <project-name>:deploy
If the build target (
dist/<project-name> folder) is empty, the project will be built with the production option (similar to running
ng build --prod).
You may be asked to sign in to Azure again. Then, the project will be deployed to the storage account specified in
azure.json. The link to the deployed app will be presented.
To clear the cached credentials run:
ng run <project-name>:azureLogout
This command is available only after signing in to Azure.
This project collects usage data and sends it to Microsoft to help improve our products and services.
Read Microsoft's privacy statement to learn more.
To turn off telemetry, add the telemetry flag (
-t) with the
false value when running
ng add, like this:
ng add ng-deploy-azure --telemetry=false
ng add ng-deploy-azure -t=false
To manually select and/or create the resources needed for deployment,
ng add @azure/ng-deploy --manual
You will be prompted to select or create the resource group and the storage account in which the app will be deployed. If you choose to create a resource group you will be asked to select the geographical location.
You can pass the names of the resources you'd like to use when running the command.
Resources that don't already exist will be created.
--manual you will be prompted to select the remaining configuration options.
Otherwise, defaults will be used.
The available options are:
-i) - subscription ID under which to select and/or create new resources
-n) - subscription name under which to select and/or create new resources
-g) - name of the Azure Resource Group to deploy to
-a) - name of the Azure Storage Account to deploy to
-l) - location where to create storage account e.g.
-t) - see Data/Telemetry
ng add @azure/ng-deploy -m -l="East US" -a=myangularapp
When creating a new storage account, the provided name will be validated.
The requirements for these names are:
- between 3 and 24 characters
- lower case letters and numbers only
- unique across Azure
If the validation fails, the tool will suggest a valid name. You will be able to select it or try another one.
When deploying from a CI environement, we switch to a non-interactive login process that requires you to provide [Service Principal][principal-service] crendetials as environment variables. A [Service Principal][principal-service] is an application within [Azure Active Directory][active-directory] that we can use to perform unattended resource and service level operations.
In orther to create and get the [Service Principal][principal-service] application credentials, you can either use the [Azure Portal][principal-service-portal] or use the [Azure CLI][azure-cli].
We recommend using the Azure CLI and running the following command:
AZURE_SUBSCRIPTION_ID="<a valid subscription ID>"SP_NAME='<a principal service name>'az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/$AZURE_SUBSCRIPTION_ID" --name="$SP_NAME"
This command will output the following values:
You can use the Azure CLI to test that these values work and you can logging in:
az login --service-principal -u $CLIENT_ID -p $CLIENT_SECRET --tenant $TENANT_ID
Configuring the environment variables
We will need to set the following environment variables BEFORE adding
@azure/ng-deploy or running the deploy command:
CI: this must be set to
1. This will enable the CI mode.
CLIENT_ID: is the
CLIENT_SECRET: is the
TENANT_ID: is the
AZURE_SUBSCRIPTION_ID: is your valid subscription ID.
Here is a simple shell example:
export CI=1export CLIENT_ID='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'export CLIENT_SECRET='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'export TENANT_ID='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'export AZURE_SUBSCRIPTION_ID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"ng run <project-name>:deploy
For security reasons, we highly recommend to create and provide these environment variables through a different method, eg. [Github Secrets][github-secrets] or [Azure DevOps Secrets][azure-devops-secrets].
Security issues and bugs should be reported privately, via email, to the Microsoft Security Response Center (MSRC) at firstname.lastname@example.org. You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Further information, including the MSRC PGP key, can be found in the Security TechCenter.
Please refer to CONTRIBUTING for CLA guidance.
Minko Gechev for guiding us through the new Angular CLI Architect API, which enables adding commands.
John Papa for guiding through and supporting the development, publish and release.
- Learn more about Azure Static Hosting in this blog post announcing Static websites on Azure Storage
- Install this VS Code extension for Azure Storage
- Follow this tutorial to deploy a static website to Azure