Skip to content

Instantly share code, notes, and snippets.

@jenyayel
Created October 28, 2019 19:01
Show Gist options
  • Save jenyayel/e90ab7bd3163d0052d86b0723929e8b1 to your computer and use it in GitHub Desktop.
Save jenyayel/e90ab7bd3163d0052d86b0723929e8b1 to your computer and use it in GitHub Desktop.
Powershell to deploy Azure WebJob (can/should be used in Azure DevOps)
# 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"
}
@PedroEss
Copy link

Good job it save me some time to make new script but two things:

  1. For me Rest-Method do now work because it is throwing Parameter name: offset Value out of range. I solved it changing to Invoke- WebRequest.
  2. It is worth to check first how our uri looks like by creating webjob from the portal and check url in Network section in PUT operation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment