Created
October 28, 2019 19:01
-
-
Save jenyayel/e90ab7bd3163d0052d86b0723929e8b1 to your computer and use it in GitHub Desktop.
Powershell to deploy Azure WebJob (can/should be used in Azure DevOps)
This file contains hidden or 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
# configurations | |
$workingDirectory = "$(System.DefaultWorkingDirectory)" | |
$sourceArtifact = "ZIP_FILE_THAT_HAS_YOUR_AAPLICATION_(PRODUCED_BY_BUILD)" | |
$entryPointDll = "FILE_THAT_HAS_STATIC_VOID_MAIN.dll" | |
$webJobName = "THE_NAME_OF_THE_WEBJOB" | |
$jobType = "continuous" | |
$appName = "THE_NAME_OF_AZURE_WEB_APP_THAT_WILL_HOST_JOB" | |
$resourceGroupName = "GROUP_NAME" | |
Write-Host "Validating artifact file" | |
$zippedArtifact = Get-ChildItem -Path $workingDirectory -Filter $sourceArtifact -Recurse | |
if ($null -eq $zippedArtifact) { throw [System.IO.FileNotFoundException] "No artifacts were found" } | |
if ($zippedArtifact -is [array]) { throw [System.ArgumentException] "More than one artifact was found" } | |
Write-Host "Adding startup script to artifact" | |
$artifact = "$($workingDirectory)/$($zippedArtifact.Name)" | |
Copy-Item $zippedArtifact.FullName -Destination $artifact | |
"dotnet $entryPointDll" | Out-File "$workingDirectory/run.cmd" -Encoding ASCII | |
Compress-Archive -Path "$workingDirectory/run.cmd" -Update -DestinationPath $artifact | |
Write-Host "Retreiving publishing credentials" | |
$app = Get-AzWebApp -ResourceGroupName $resourceGroupName -Name $appName | |
$publishingCredentials = Invoke-AzResourceAction ` | |
-ResourceGroupName $app.ResourceGroup ` | |
-ResourceType "Microsoft.Web/sites/config" ` | |
-ResourceName "$($app.Name)/publishingcredentials" ` | |
-Action list ` | |
-Force | |
$user = $publishingCredentials.Properties.PublishingUserName | |
$pass = $publishingCredentials.Properties.PublishingPassword | |
$creds = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("${user}:${pass}"))) | |
# PUT file | |
$endpoint = "https://$($appName).scm.azurewebsites.net/api/$($jobType)webjobs/$($webJobName)" | |
Write-Host "Uploading file to $endpoint" | |
try { | |
Invoke-RestMethod ` | |
-Uri $endpoint ` | |
-ContentType "application/zip" ` | |
-Method "PUT" ` | |
-Headers @{ | |
"Authorization" = "Basic $creds" | |
"Content-Disposition" = "attachement; filename=run.cmd" | |
} ` | |
-UserAgent "PowerShell $($PSVersionTable.PSVersion.ToString())" ` | |
-InFile $artifact | |
Write-Host "Successfully uploaded file" | |
} | |
catch { | |
Write-Error "Failed to PUT a file with status $($_.Exception.Response.StatusCode.value__): $($_.ErrorDetails.Message)" | |
throw [System.InvalidOperationException] "Failed to deploy file" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good job it save me some time to make new script but two things: