Skip to content

Instantly share code, notes, and snippets.

@chamindac
Last active March 31, 2020 06:10
Show Gist options
  • Save chamindac/531ab9c1faaa0369b28da0387391295f to your computer and use it in GitHub Desktop.
Save chamindac/531ab9c1faaa0369b28da0387391295f to your computer and use it in GitHub Desktop.
This script can be used to create an Azure web app and add the .NET core 3.0 extensions in an Azure DevOps release pipeline. See https://chamindac.blogspot.com/2019/08/creating-azure-web-app-supporting-net.html
param (
[Parameter(Mandatory=$true)]
[string] $azureAppId,
[Parameter(Mandatory=$true)]
[string] $azureAppPwd,
[Parameter(Mandatory=$true)]
[string] $azureTenant,
[Parameter(Mandatory=$true)]
[string] $resourceGroupName,
[Parameter(Mandatory=$true)]
[string] $location,
[Parameter(Mandatory=$true)]
[string] $webAppPlanName,
[Parameter(Mandatory=$true)]
[string] $webAppPlanSKU,
[Parameter(Mandatory=$true)]
[string] $webAppName,
[Parameter(Mandatory=$true)]
[string] $core3Extension,
[string[]] $webAppDeploymentSlotNames = @(),
[string] $azureRestApiVersion = '2018-11-01'
)
Try {
az login --service-principal -u $azureAppId --password $azureAppPwd --tenant $azureTenant
if ($LastExitCode -ne 0) {
throw $Error[1]
}
# Create if resource group not found
if ((az group exists -n $resourceGroupName) -ne 'true')
{
Write-Host ("Resource group " + $resourceGroupName + " not found. Creating it...")
az group create -n $resourceGroupName -l $location
if ($LastExitCode -ne 0) {
throw $Error[1]
}
Write-Host ("Resource group " + $resourceGroupName + " created.")
}
else
{
Write-Host ("Resource group " + $resourceGroupName + " is found.")
}
# Create Web App Plan if not exisit
$webAppPlans = az appservice plan list -g $resourceGroupName | ConvertFrom-Json
if (($webAppPlans.Count -gt 0) -and ($webAppPlans.name.Contains($webAppPlanName)))
{
# update app service plan pricing
Write-Host ("Web App Plan " + $webAppPlanName + " is found. Updating pricing..." )
az appservice plan update -n $webAppPlanName -g $resourceGroupName --sku $webAppPlanSKU
if ($LastExitCode -ne 0) {
throw $Error[1]
}
Write-Host ("Done.")
}
else
{
Write-Host ("Web App Plan " + $webAppPlanName + " not found. Creating it...")
az appservice plan create -n $webAppPlanName -g $resourceGroupName --sku $webAppPlanSKU
if ($LastExitCode -ne 0) {
throw $Error[1]
}
Write-Host ("Web App Plan " + $webAppPlanName + " created.")
}
# create if web app does not exist
$webApps = az webapp list -g $resourceGroupName | ConvertFrom-Json
if (($webApps.Count -gt 0) -and ($webApps.name.Contains($webAppName)))
{
Write-Host ("Web App " + $webAppName + " is found." )
}
else
{
$webAppPricePlan = az appservice plan show -n $webAppPlanName -g $resourceGroupName | ConvertFrom-Json
Write-Host ("Web App " + $webAppName + " is not found. Creating it...")
az webapp create -n $webAppName -g $resourceGroupName -p ($webAppPricePlan.id)
if ($LastExitCode -ne 0) {
throw $Error[1]
}
Write-Host ("Web App " + $webAppName + " is created.")
}
# Check if Site Extension added for ASP.NET Core and install it if not
$siteExtensions = Get-AzureRmResource -ResourceType "Microsoft.Web/sites/siteextensions" `
-ResourceGroupName $resourceGroupName -Name $webAppName -ApiVersion $azureRestApiVersion
if (($siteExtensions -eq $null) -or (-not $siteExtensions.Properties.id.Contains($core3Extension)))
{
Write-Host ("Site extension " + $core3Extension + " is not added to web app " + $webAppName + ". Adding...")
$siteExtensions = New-AzureRmResource -ResourceType "Microsoft.Web/sites/siteextensions" `
-ResourceGroupName $resourceGroupName -Name ("$webAppName/$core3Extension") -ApiVersion $azureRestApiVersion -Force
if ($LastExitCode -ne 0) {
throw $Error[1]
}
Write-Host ("Done.")
}
else
{
Write-Host ("Site extension " + $core3Extension + " is already added to web app " + $webAppName + ".")
}
foreach ($webAppDeploymentSlotName in $webAppDeploymentSlotNames)
{
if (-not ([string]::IsNullOrWhiteSpace($webAppDeploymentSlotName)))
{
# Check if any depyment slots exists
$slots = az webapp deployment slot list -g $resourceGroupName -n $webAppName | ConvertFrom-Json
if (($slots.Count -gt 0) -and ($slots.name.Contains($webAppDeploymentSlotName)))
{
Write-Host ("Web App " + $webAppName + " slot " + $webAppDeploymentSlotName + " is found." )
}
else
{
Write-Host ("Web App " + $webAppName + " slot " + $webAppDeploymentSlotName + " is not found. Creating it...")
az webapp deployment slot create -s $webAppDeploymentSlotName -n $webAppName -g $resourceGroupName `
--configuration-source $webAppName
if ($LastExitCode -ne 0) {
throw $Error[1]
}
Write-Host ("Web App " + $webAppName + " slot " + $webAppDeploymentSlotName + " is created.")
}
# Check if slot Extension added for ASP.NET Core and install it if not
$slotExtensions = Get-AzureRmResource -ResourceType "Microsoft.Web/sites/slots/siteextensions" `
-ResourceGroupName $resourceGroupName -Name ("$webAppName/$webAppDeploymentSlotName") `
-ApiVersion $azureRestApiVersion
if (($slotExtensions -eq $null) -or (-not $slotExtensions.Properties.id.Contains($core3Extension)))
{
Write-Host ("Slot extension " + $core3Extension + " is not added to web app slot " + ("$webAppName/$webAppDeploymentSlotName") + ". Adding...")
$slotExtensions = New-AzureRmResource -ResourceType "Microsoft.Web/sites/slots/siteextensions" `
-ResourceGroupName $resourceGroupName -Name ("$webAppName/$webAppDeploymentSlotName/$core3Extension") `
-ApiVersion $azureRestApiVersion -Force
if ($LastExitCode -ne 0) {
throw $Error[1]
}
Write-Host ("Done.")
}
else
{
Write-Host ("Site extension " + $core3Extension + " is already added to web app slot " + ("$webAppName/$webAppDeploymentSlotName") + ".")
}
}
}
}
catch
{
Write-Host $Error[0] -ForegroundColor Red
exit 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment