Skip to content

Instantly share code, notes, and snippets.

@PlagueHO
Last active August 6, 2020 19:37
Show Gist options
  • Save PlagueHO/c028ce068df16c3afa68eaa810bcb9f6 to your computer and use it in GitHub Desktop.
Save PlagueHO/c028ce068df16c3afa68eaa810bcb9f6 to your computer and use it in GitHub Desktop.
Publish an Azure Web App using a Service Principal in PowerShell to an Azure RM App Service
[CmdletBinding()]
param (
[Parameter(Mandatory = $True)]
[pscredential]
$Credential,
[Parameter(Mandatory = $True)]
[System.String]
$TenantId,
[Parameter(Mandatory = $True)]
[System.String]
$SubscriptionId,
[Parameter(Mandatory = $True)]
[System.String]
$WebAppPath,
[Parameter(Mandatory = $True)]
[System.String]
$ResourceGroupName,
[Parameter(Mandatory = $True)]
[System.String]
$WebAppServiceName,
[Parameter()]
[System.String]
$SlotName,
[Parameter()]
[System.String]
$MSDeployPath = "$env:ProgramFiles\IIS\Microsoft Web Deploy V3\msdeploy.exe"
)
if (-not (Test-Path -Path $MSDeployPath))
{
Throw "MSDeploy.exe not found at '$MSDeployPath'. Please install MSDeploy or specify the path to MSDeploy.exe on this system."
}
# Connect to Azure using SP
$connectParameters = @{
Credential = $Credential
TenantId = $TenantId
SubscriptionId = $SubscriptionId
}
Write-Verbose -Message 'Connecting to Azure.'
$null = Add-AzureRmAccount @connectParameters -ServicePrincipal
# If a slot name is passed ensure all cmdlets use it
if ([String]::IsNullOrEmpty($SlotName)) {
$slotParameters = $null
} else {
$slotParameters = @{ Slot = $SlotName }
}
# Get the Publishing profile from Azure
$publishProfilePath = Join-Path -Path $ENV:Temp -ChildPath 'publishprofile.xml'
Write-Verbose -Message 'Getting publishing profile for web app'
$null = Get-AzureRmWebAppSlotPublishingProfile `
-OutputFile $publishProfilePath `
-Format WebDeploy `
-ResourceGroupName $ResourceGroupName `
-Name $WebAppServiceName `
@slotParameters
# Stop the web app slot to make sure deployment is possible.
Write-Verbose -Message 'Stopping web app.'
$null = Stop-AzureRmWebAppSlot `
-ResourceGroupName $ResourceGroupName `
-Name $WebAppServiceName `
@slotParameters
# Deploy the web site
$source = "-source:contentPath=$WebAppPath"
$dest = "-dest:contentPath=d:\home\site\wwwroot\,publishSettings=$publishProfilePath"
Write-Verbose -Message 'Publising web app content.'
& $MSDeployPath @('-verb:sync', $source, $dest)
# Start the web app back up
Write-Verbose -Message 'Starting web app.'
$null = Start-AzureRmWebAppSlot `
-ResourceGroupName $ResourceGroupName `
-Name $WebAppServiceName `
@slotParameters
Write-Verbose -Message 'Waiting for web app to start up...'
$webappSlot = Get-AzureRmWebAppSlot `
-ResourceGroupName $ResourceGroupName `
-Name $WebAppServiceName `
@slotParameters
# Wait for the site to report that it has started (optional)
while ($webappSlot.state -ne 'Running')
{
Start-Sleep -Seconds 1
Write-Verbose -Message 'Waiting for web app to start up...'
$webappSlot = Get-AzureRmWebAppSlot `
-ResourceGroupName $ResourceGroupName `
-Name $WebAppServiceName `
@slotParameters
}
Write-Verbose -Message 'Web app deployment complete.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment