Lately I have written a .net core web job and wanted to publish it via CD (continuous deployment) from Visual Studio Online. Soon I figured, Azure Web Job SDK doesn’t support (yet) .net core. The work I expected will take 10 mins took about an hour.
If you are also figuring out this, this blog post is what you are looking for.
I will describe the steps and provide a PowerShell script that does the deployment via Kudu API. Kudu is the Source Control management for Azure app services, which has a Zip API that allows us to deploy zipped folder into an Azure app service.
Here are the steps you need to follow. You can start by creating a simple .net core console application. Add a Power Shell file into the project that will do the deployment in your Visual Studio online release pipeline. The Power Shell script will do the following:
- Publish the project (using dotnet publish)
- Make a zip out of the artifacts
- Deploy the zip into the Azure web app
Publishing the project
We will use dotnet publish command to publish our project.
$resourceGroupName = "my-regource-group" $webAppName = "my-web-job" $projectName = "WebJob" $outputRoot = "webjobpublish" $ouputFolderPath = "webjobpublish\App_Data\Jobs\Continuous\my-web-job" $zipName = "publishwebjob.zip" $projectFolder = Join-Path ` -Path "$((get-item $PSScriptRoot ).FullName)" ` -ChildPath $projectName $outputFolder = Join-Path ` -Path "$((get-item $PSScriptRoot ).FullName)" ` -ChildPath $ouputFolderPath $outputFolderTopDir = Join-Path ` -Path "$((get-item $PSScriptRoot ).FullName)" ` -ChildPath $outputRoot $zipPath = Join-Path ` -Path "$((get-item $PSScriptRoot ).FullName)" ` -ChildPath $zipName if (Test-Path $outputFolder) { Remove-Item $outputFolder -Recurse -Force; } if (Test-path $zipName) {Remove-item $zipPath -Force} $fullProjectPath = "$projectFolder\$projectName.csproj" dotnet publish "$fullProjectPath" --configuration release --output $outputFolder
Create a compressed artifact folder
We will use System.IO.Compression.Filesystem assembly to create the zip file.
Add-Type -assembly "System.IO.Compression.Filesystem" [IO.Compression.Zipfile]::CreateFromDirectory( $outputFolderTopDir, $zipPath)
Upload the zip into Azure web app
Next step is to upload the zip file into the Azure web app. This is where we first need to fetch the credentials for the Azure web app and then use the Kudu API to upload the content. Here’s the script:
function Get-PublishingProfileCredentials ($resourceGroupName, $webAppName) { $resourceType = "Microsoft.Web/sites/config" $resourceName = "$webAppName/publishingcredentials" $publishingCredentials = Invoke-AzureRmResourceAction ` -ResourceGroupName $resourceGroupName ` -ResourceType $resourceType ` -ResourceName $resourceName ` -Action list ` -ApiVersion 2015-08-01 ` -Force return $publishingCredentials } function Get-KuduApiAuthorisationHeaderValue ($resourceGroupName, $webAppName) { $publishingCredentials = Get-PublishingProfileCredentials $resourceGroupName $webAppName return ("Basic {0}" -f ` [Convert]::ToBase64String( ` [Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, ` $publishingCredentials.Properties.PublishingPassword)))) } $kuduHeader = Get-KuduApiAuthorisationHeaderValue ` -resourceGroupName $resourceGroupName ` -webAppName $webAppName $Headers = @{ Authorization = $kuduHeader } # use kudu deploy from zip file Invoke-WebRequest ` -Uri https://$webAppName.scm.azurewebsites.net/api/zipdeploy ` -Headers $Headers ` -InFile $zipPath ` -ContentType "multipart/form-data" ` -Method Post # Clean up the artifacts now if (Test-Path $outputFolder) { Remove-Item $outputFolder -Recurse -Force; } if (Test-path $zipName) {Remove-item $zipPath -Force}
PowerShell task in Visual Studio Online
Now we can leverage the Azure PowerShell task in Visual Studio Release pipeline and invoke the script to deploy the web job.
That’s it!
Thanks for reading, and have a nice day!