Skapa en webbapp och distribuera kod till en mellanlagringsmiljö

Det här exempelskriptet skapar en webbapp i App Service med ytterligare ett distributionsfack som kallas för ”mellanlagring”, och distribuerar sedan en exempelapp till ”mellanlagringsplatsen”.

Om det behövs installerar du Azure PowerShell med hjälp av instruktionen i Azure PowerShell-guiden och kör Connect-AzAccount sedan för att skapa en anslutning till Azure.

Exempelskript

Anteckning

Vi rekommenderar att du använder Azure Az PowerShell-modulen för att interagera med Azure. Se Installera Azure PowerShell för att komma igång. Information om hur du migrerar till Az PowerShell-modulen finns i artikeln om att migrera Azure PowerShell från AzureRM till Az.

# 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";
    isManualIntegration = "true"; #remove isManualIntegration for continuous deployment from a GitHub repo you own
}
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

Rensa distribution

När skriptet har körts kan följande kommando användas för att ta bort resursgruppen, webbappen och alla relaterade resurser.

Remove-AzResourceGroup -Name myResourceGroup -Force

Förklaring av skript

Det här skriptet använder följande kommandon. Varje kommando i tabellen länkar till kommandospecifik dokumentation.

Kommando Kommentarer
New-AzResourceGroup Skapar en resursgrupp där alla resurser lagras.
New-AzAppServicePlan Skapar en App Service-plan.
New-AzWebApp Skapar en webbapp.
Set-AzAppServicePlan Ändrar en App Service-plan för att ändra prisnivån.
New-AzWebAppSlot Skapar ett distributionsfack för en webbapp.
Set-AzResource Ändrar en resurs i en resursgrupp.
Switch-AzWebAppSlot Byter ut en webbapps distributionsfack till produktion.

Nästa steg

Mer information om Azure PowerShell-modulen finns i Azure PowerShell-dokumentationen.

Ytterligare Azure PowerShell exempel för Azure App Service Web Apps finns i de Azure PowerShell exemplen.