Skip to content

Instantly share code, notes, and snippets.

@OlafD
Created March 16, 2022 09:58
Show Gist options
  • Save OlafD/2253619a19696e43655d6e1741d1dcb0 to your computer and use it in GitHub Desktop.
Save OlafD/2253619a19696e43655d6e1741d1dcb0 to your computer and use it in GitHub Desktop.
Deploy the release build of a web job from an ASP.Net project to an Azure App Service. The ExeFile parameter must not have a blank. To run the script Azure CLI is needed on the machine to get the publish credentials from the app service.
param (
[Parameter(Mandatory=$true)]
[string]$BuildOutput,
[Parameter(Mandatory=$true)]
[string]$ExeFile,
[Parameter(Mandatory=$true)]
[string]$WebApp,
[Parameter(Mandatory=$true)]
[string]$ResourceGroup,
[Parameter(Mandatory=$true)]
[string]$Subscription,
[Parameter(Mandatory=$true)]
[string]$JobName,
[Parameter(Mandatory=$true)]
[ValidateSet("triggered", "continuous")]
[string] $JobType,
[string] $TempDir = "C:\Temp",
[switch]$SkipLogin
)
if ($SkipLogin.ToBool() -eq $false)
{
az login
}
# parts below are taken from https://markheath.net/post/managing-webjobs-with-kudu-api#disqus_thread
# get publishing credentials
$user = az webapp deployment list-publishing-profiles -n $WebApp -g $ResourceGroup --query "[?publishMethod=='MSDeploy'].userName" -o tsv
$pass = az webapp deployment list-publishing-profiles -n $WebApp -g $ResourceGroup --query "[?publishMethod=='MSDeploy'].userPWD" -o tsv
# set up deployment credentials
$creds = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($creds))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{
Authorization = $basicAuthValue
}
# parts below taken from: https://mariankostal.com/2020/07/17/deploy-azure-webjob-using-powershell/
Write-Host "BuildOutput...: $BuildOutput"
Write-Host "ExeFile.......: $ExeFile"
Write-Host "WebApp........: $WebApp"
Write-Host "ResourceGroup.: $ResourceGroup"
Write-Host "Subscription..: $Subscription"
Write-Host "JobName.......: $JobName"
Write-Host "JobType.......: $JobType"
Write-Host "TempDir.......: $TempDir"
Write-Host "SkipLogin.....: $SkipLogin"
New-Item "$TempDir\$WebApp" -ItemType Directory
$webJobDir = "$TempDir\$WebApp\App_Data\jobs\$JobType\$JobName"
New-Item $WebJobDir -ItemType Directory
$files = Get-ChildItem -Path $BuildOutput -Exclude "*.xml", "*.pdb", "*.resources.dll", de*, es*, fr*, it*, ja*, ko*, ru*, zh-hans*, zh-hant* -Recurse
Copy-Item -Path $files -Destination $webJobDir
Compress-Archive -Path "$TempDir\$WebApp\*" -DestinationPath "$TempDir\$JobName.zip" -CompressionLevel Optimal -Force
Remove-Item "$TempDir\$WebApp" -Recurse -Force
$ZipHeaders = @{
Authorization = $basicAuthValue
"Content-Disposition" = "attachment; filename=$ExeFile"
}
$ZipHeaders
Write-Host "Uploading package..."
Invoke-WebRequest -Uri https://$WebApp.scm.azurewebsites.net/api/triggeredwebjobs/$JobName -Headers $ZipHeaders -InFile "$TempDir\$JobName.zip" -ContentType "application/zip" -Method Put -SkipHeaderValidation
Write-Host -ForegroundColor Green "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment