Created
September 15, 2021 11:31
-
-
Save akuryan/36ad6be7b943e7d6d9cb96f97c895818 to your computer and use it in GitHub Desktop.
This gist shows how could one use Kudu api/zip in combination with api/vfs to deliver archive outside of web app webroot and overcome hardcoded request timeout
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
$unicornFilesZip = "c:\temp\unicorn.zip"; | |
$indicatorFileName = "z.z." + "$(buildNumberVar)" + ".txt"; | |
$scmTargetPath = "C:\home\N"; | |
$unicornFolder = Split-Path $scmTargetPath -Leaf; | |
function Get-HttpBasicAuthHeader | |
{ | |
Param( | |
[Parameter(Mandatory=$true)] | |
[string]$UserName, | |
[Parameter(Mandatory=$true)] | |
[string]$Password | |
) | |
$pair = [string]::Format("{0}:{1}", $UserName, $Password) | |
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair) | |
$base64 = [System.Convert]::ToBase64String($bytes) | |
$basicAuthValue = [string]::Format("Basic {0}", $base64) | |
return @{ Authorization = $basicAuthValue } | |
} | |
Write-Output "Deploying Unicorn files"; | |
$Headers = @{}; | |
$Headers += Get-HttpBasicAuthHeader -UserName '$(scmUserName)' -Password '$(scmPasswrod)'; | |
$kuduApiUrl = "https://$(webAppName).scm.azurewebsites.net/api/zip/$($unicornFolder)"; | |
$kuduVfsUrl = "https://$(webAppName).scm.azurewebsites.net/api/vfs/$($unicornFolder)/$($indicatorFileName)"; | |
$checkFileRetryCount = 0; | |
$sleepTimeSeconds = 30; | |
$maxWaitTimeMinutes = 30; | |
# we will wait maximum for $maxWaitTimeMinutes | |
$maxRetryCount = ($maxWaitTimeMinutes * 60) / $sleepTimeSeconds; | |
# using Zip Api to deploy Unicorn files - it is much faster then deploying small files via MsDeploy | |
Write-Output "Deploying to $kuduApiUrl via zipdeploy Unicorn files in archive $unicornFilesZip"; | |
Invoke-WebRequest -Uri $kuduApiUrl -Headers $Headers -InFile $unicornFilesZip -ContentType "multipart/form-data" -Method Put -UseBasicParsing -ErrorAction SilentlyContinue -ErrorVariable errorVar; | |
# wait, until indicator is not present there | |
do { | |
$request = $null; | |
$result = $false; | |
$request = Invoke-WebRequest -Uri $kuduVfsUrl -Headers $Headers -UseBasicParsing -ErrorAction SilentlyContinue -ErrorVariable errorVar; | |
if ($null -ne $request -and $request.Status.Code -eq 200) { | |
$result = $true; | |
} | |
$message = "Check file is not present at $($kuduVfsUrl). Current try count is $($checkFileRetryCount)."; | |
if ($checkFileRetryCount -ge $maxRetryCount) { | |
Write-Output $message" Cancelling..."; | |
exit -1; | |
} | |
if ($result) { | |
Write-Output "Check file is present at $($kuduVfsUrl). Current try count is $($checkFileRetryCount)."; | |
} | |
else { | |
Write-Output $message; | |
} | |
Start-Sleep -Seconds $sleepTimeSeconds; | |
$checkFileRetryCount++; | |
} while ($result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment