Your web browser is out of date. Update your browser for more security, speed and the best experience on this site.

Update your browser
CapTech Home Page

Blog | July 12, 2019

Using KUDU APIs with Azure Web Jobs

Carles Quiles

KUDU is essentially the Open Source platform and engine behind the deployment-related features in Azure Web Sites. When deploying or managing a deployment on Azure Web Sites with Visual Studio we usually use Web Deploy and mostly everything is taken care for us, but if we are using any other method or source, KUDU is the engine that makes that possible.

Also, KUDU provides a set of APIs to access application settings, files, processes, runtime, source control information, Web Hooks and WebJobs on an Azure Web Site.

In this blog, I will show a couple of practical uses for using KUDU APIs when we are deploying or managing Azure Web Sites, in this case, an Azure WebJob.

Deploy a .NET Core WebJob with KUDU API

It is easy to create a .NET Core Azure WebJob in Visual Studio. Essentially, we add a new Project in a ASP.NET Core Web Application and we choose Console App as a project type to add to the solution. After that, we need to install Microsoft.Azure.WebJobs and Microsoft.Azure.WebJobs.Extensions NuGet packages.

Once we are ready for deployment, we only need to right-click the project file and Deploy.

But, wait, where is the Deploy option? Unfortunately, there is no Deploy option for .NET Core WebJobs, as it is for .NET Framework WebJobs type projects, and this is where we can use KUDU and PowerShell for that.

The PowerShell script will do the following tasks:

  • Publish the code and create a .Zip file
  • Get the user credentials to access the Azure Resource
  • And, finally, use the KUDU API to access the website and deploy the .zip file
#Publish the code
dotnet publish -c Release -o $publishFolder

# make the zip
$mainZip = $(Get-Location).Path + "\Deploy\" + ""
if(Test-path $mainZip) {Remove-item $mainZip}
Add-Type -assembly ""
[io.compression.zipfile]::CreateFromDirectory($(Get-Location).Path + "\" + $publishFolder, $mainZip)
#Get the Credentials for deployment user
$user = az webapp deployment list-publishing-profiles -n $appName -g $resourceGroup --query "[?publishMethod=='MSDeploy'].userName" -o tsv
$pass = az webapp deployment list-publishing-profiles -n $appName -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"
#Build the Request to KUDU API
$ZipHeaders = @{
 Authorization = $basicAuthValue
 "Content-Disposition" = "attachment; filename=run.cmd"
# upload the job using the Kudu API
Invoke-WebRequest -Uri https://$appName.scm.azurewebs... -Headers $ZipHeaders -InFile $mainZip -ContentType "application/zip" -Method Put