Last active
July 25, 2023 00:20
-
-
Save jasonmitchell/9049af9c9e5ba06cc3e1 to your computer and use it in GitHub Desktop.
PowerShell script for publishing websites to Azure using Kudu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$username = "$my-website" | |
$password = "password" | |
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password))) | |
$apiUrl = "https://my-website.scm.azurewebsites.net/api/command" | |
$commandBody = @{ | |
command = "del /S /F /Q .\\" | |
dir = "site\\wwwroot" | |
} | |
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST -ContentType "application/json" -Body (ConvertTo-Json $commandBody) | Out-Null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param( | |
[Parameter(Mandatory = $true)] | |
[string]$subscriptionId, | |
[Parameter(Mandatory = $true)] | |
[string]$websiteName, | |
[Parameter(Mandatory = $true)] | |
[string]$buildZip, | |
[string]$destinationPath = "/site/wwwroot", | |
[int]$timeout = 600 | |
) | |
$ErrorActionPreference = "Stop" | |
Function JoinParts { | |
param ([string[]] $Parts, [string] $Separator = '/') | |
$search = '(?<!:)' + [regex]::Escape($Separator) + '+' | |
($Parts | Where-Object {$_ -and $_.Trim().Length}) -join $Separator -replace $search, $Separator | |
} | |
Select-AzureSubscription -SubscriptionId $subscriptionId | |
$website = Get-AzureWebsite -Name $websiteName | |
$username = $website.PublishingUsername | |
$password = $website.PublishingPassword | |
$base64Auth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password))) | |
$authHeader = @{Authorization=("Basic {0}" -f $base64Auth)} | |
$baseUri = ($website.SiteProperties.Properties | Where-Object { $_.Name -eq "RepositoryUri" } | Select-Object -First 1).Value | |
$commandApiUri = JoinParts ($baseUri, "/api/command") | |
$commandBody = @{ | |
command = "del /S /F /Q .\\" | |
dir = "site\\wwwroot" | |
} | |
Write-Output "Removing previous build..." | |
Invoke-RestMethod -Uri $commandApiUri -Headers $authHeader -Method POST -ContentType "application/json" -Body (ConvertTo-Json $commandBody) -TimeoutSec $timeout | Out-Null | |
Write-Output ("Publishing to URI '{0}'..." -f $deployApiUri) | |
$deployApiUri = JoinParts ($baseUri, "api/zip", $destinationPath) '/' | |
Invoke-RestMethod -Uri $deployApiUri -Headers $authHeader -Method PUT -InFile $buildZip -ContentType "multipart/form-data" -TimeoutSec $timeout | Out-Null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$username = "$my-website" | |
$password = "password" | |
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password))) | |
$apiUrl = "https://my-website.scm.azurewebsites.net/api/zip/site/wwwroot" | |
$filePath = "C:\Path\To\package.zip" | |
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method PUT -InFile $filePath -ContentType "multipart/form-data" | Out-Null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment