Automate tasks with Azure CLI
Automating Azure tasks is a common requirement for continuous deployment to hosting environments. Azure CLI is the recommended choice for JavaScript developers managing tasks and deploying from any location.
Learn common task commands for JavaScript developers.
Automation with Azure CLI
To automate the Azure CLI, the CLI must be installed in the environment. Common methods are:
Using the example commands
- Replace variables in brackets,
<...>
, with your own values. - Your GitHub repository value for
<MY_GITHUB_DEFAULT_BRANCH_NAME>
is specific to the repo used. Currently, the typical values aremain
, ordefault
. Older repositories may usemaster
.
Authentication with managed identity for automated tasks with Azure CLI
For automation, authentication with az login to the Azure CLI with managed identity.
az login --identity
Authentication with service principal for automated tasks with Azure CLI
After the Service Principal is created, login with a user's Service Principal.
read -sp "Azure password: " AZ_PASS && echo && \
az login --service-principal \
-u <MY-SP-APP-URL> \
-p $AZ_PASS \
--tenant <MY-TENANT>
Authentication with user credentials for automated tasks with Azure CLI
Use the following command to authenticate with User credentials.
az login -u <MY_AZURE_USERNAME> -p <MY_AZURE_PASSWORD>
Create resource group for resources
A resource group is a logical collection of your Azure resources. The logical grouping is based on services you need in a specific region for a project. Learn about naming conventions. Use the az group create command to create your resource group before you create Azure service resources.
az group create \
--name <MY-AZURE-RESOURCE_GROUP_NAME> \
--location <AZURE_REGION_LOCATION>
Create Azure Static web app
Use the az staticwebapp create command to create a new static web app.
az staticwebapp create \
--name <MY_AZURE_WEB_APP_NAME> \
--resource-group <MY-AZURE-RESOURCE_GROUP_NAME> \
--source https://github.com/<MY_GITHUB_ACCOUNT_NAME>/<MY_AZURE_WEB_APP_NAME> \
--location <AZURE_REGION_LOCATION> \
--branch <MY_GITHUB_DEFAULT_BRANCH_NAME> \
--app-artifact-location "<MY_WEB_APP_BUILD_DIRECTORY_NAME>" \
--token <MY_GITHUB_PERSONAL_ACCESS_TOKEN>
Deploy Azure Static web app
To deploy your app, push to the remote GitHub branch set during resource creation in the previous set.
git push <REMOTE_NAME> <MY_GITHUB_DEFAULT_BRANCH_NAME>
An example of this command is:
git push origin main
Delete static web app
Use the az staticwebapp delete command to delete your static web app.
az staticwebapp delete && \
--name <MY_AZURE_WEB_APP_NAME> && \
--resource-group <MY-AZURE-RESOURCE_GROUP_NAME>
Create Azure Function app
A consumption-based function app needs both the function app and a storage resource.
Create the storage resource with az storage account create:
az storage account create \ --name <MY-AZURE-STORAGE> \ --location <AZURE_REGION_LOCATION> \ --resource-group <MY-AZURE-RESOURCE_GROUP_NAME> \ --sku Standard_LRS
Create the function app resource with az functionapp create:
az functionapp create \ --name <MY-AZURE-FUNCTION> \ --storage-account <MY-AZURE-STORAGE> \ --consumption-plan-location <AZURE_REGION_LOCATION> \ --resource-group <MY-AZURE-RESOURCE_GROUP_NAME> \ --functions-version 2
Create Azure Function API endpoint
There isn't an Azure CLI command to create an Azure Function API endpoint for your local development project. The Visual Studio Code Azure Function extension is the recommended way to create a local Azure Function project and add API endpoints to the project.
Create Azure Function deployment slot
Creating a deployment slot then swapping allows you to quickly revert a deployment. Create a deployment slow with the az functionapp deployment slot create command.
az functionapp deployment slot create \
--name <MY-AZURE-FUNCTION> \
--resource-group <MY-AZURE-RESOURCE_GROUP_NAME> \
--slot <MY-SLOT-NAME> \
--configuration-source <MY-PRODUCTION-SLOT>
Deploy Azure Function from git
Manage deployment from git or Mercurial repositories with az functionapp deployment source config. Select one repository type for the --repository-type
setting from the choices: externalgit, git, github, localgit, mercurial.
az functionapp deployment source config --repo-url \
--branch <MY-REPO-BRANCH> \
--git-token <MY-GIT-TOKEN> \
--name <MY-AZURE-FUNCTION> \
--repository-type <MY-REPO-TYPE> \
--resource-group <MY-AZURE-RESOURCE_GROUP_NAME> \
--slot <MY-SLOT-NAME> \
Swap Azure Function slots
Use the az functionapp deployment slot swap command to swap slots. Slot action choices are: preview, reset, swap
az functionapp deployment slot swap \
--resource-group <MY-AZURE-RESOURCE_GROUP_NAME> \
--name <MY-AZURE-FUNCTION> \
--slot <MY-SLOT-NAME> \
--action <YOUR-ACTION> \
--target-slot <MY-OTHER-SLOT-NAME>
Delete Azure Function
Use the [az functionapp delete] command to delete your function app.
az functionapp delete \
--resource-group <MY-AZURE-RESOURCE_GROUP_NAME> \
--name <MY-AZURE-FUNCTION>
Next steps
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for