Friday, August 2, 2019

Create a web app and deploy code to a staging environment

This sample script creates a web app in App Service with an additional deployment slot called "staging", and then deploys a sample app to the "staging" slot.
If needed, install the Azure PowerShell using the instruction found in the Azure PowerShell guide, and then run Connect-AzAccount to create a connection with Azure.

Sample script

 Note:
This article has been updated to use the new Azure PowerShell Az module. You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. For Az module installation instructions, see Install Azure PowerShell.
Azure PowerShell:-
# Replace the following URL with a public GitHub repo URL $gitrepo="https://github.com/Azure-Samples/app-service-web-dotnet-get-started.git" $webappname="mywebapp$(Get-Random)" $location="West Europe" # Create a resource group. New-AzResourceGroup -Name myResourceGroup -Location $location # Create an App Service plan in Free tier. New-AzAppServicePlan -Name $webappname -Location $location ` -ResourceGroupName myResourceGroup -Tier Free # Create a web app. New-AzWebApp -Name $webappname -Location $location ` -AppServicePlan $webappname -ResourceGroupName myResourceGroup # Upgrade App Service plan to Standard tier (minimum required by deployment slots) Set-AzAppServicePlan -Name $webappname -ResourceGroupName myResourceGroup ` -Tier Standard #Create a deployment slot with the name "staging". New-AzWebAppSlot -Name $webappname -ResourceGroupName myResourceGroup ` -Slot staging # Configure GitHub deployment to the staging slot from your GitHub repo and deploy once. $PropertiesObject = @{ repoUrl = "$gitrepo"; branch = "master"; } Set-AzResource -PropertyObject $PropertiesObject -ResourceGroupName myResourceGroup ` -ResourceType Microsoft.Web/sites/slots/sourcecontrols ` -ResourceName $webappname/staging/web -ApiVersion 2015-08-01 -Force # Swap the verified/warmed up staging slot into production. Switch-AzWebAppSlot -Name $webappname -ResourceGroupName myResourceGroup ` -SourceSlotName staging -DestinationSlotName production

Clean up deployment

After the script sample has been run, the following command can be used to remove the resource group, web app, and all related resources.
PowerShell:-
Remove-AzResourceGroup -Name myResourceGroup -Force

Script explanation

This script uses the following commands. Each command in the table links to command specific documentation.
CommandNotes
New-AzResourceGroupCreates a resource group in which all resources are stored.
New-AzAppServicePlanCreates an App Service plan.
New-AzWebAppCreates a web app.
Set-AzAppServicePlanModifies an App Service plan to change its pricing tier.
New-AzWebAppSlotCreates a deployment slot for a web app.
Set-AzResourceModifies a resource in a resource group.
Switch-AzWebAppSlotSwaps a web app's deployment slot into production.

No comments:

Post a Comment

Lab 09: Publish and subscribe to Event Grid events

  Microsoft Azure user interface Given the dynamic nature of Microsoft cloud tools, you might experience Azure UI changes that occur after t...