Deploy .Net Core Console Application based Azure WebJob using PowerShell

In this tutorial, We are going to see how to deploy an Azure WebJob which is based out of an .Net Core Console Application using PowerShell. At this point of time there is no built-in support in Visual Studio to deploy .Net Core based application as an Azure WebJob. We can either deploy the WebJob directly at Azure Portal (which is straight forward) or by using PowerShell. We are going to use combination of PowerShell and Azure Cmdlets to deploy the WebJob.

NOTE: Following tutorial has to be completed before proceeding with present tutorial – Azure WebJob with .Net Core Console Application.

To execute this tutorial, we need Azure PowerShell Cmdlets 3.1.0.

image

NOTE: Updated this tutorial on 2/26/2017 with MSBuild based Dotnet SDK.

I am using following version of Dotnet SDK which is based on MSBuild/CSProj (remember that previous versions of .Net Core are based on Project.json/XProj.).

 image

As a continuation to afore mentioned tutorial, we are going to create following PowerShell script with Deploy-WebJob.ps1 file name.

 # Set the Azure Subscription in current PowerShell context.
function setAzureSubscription($basePath){
    $path = '{0}/{1}' -f $basePath, 'rami.publishsettings'
    Import-AzurePublishSettingsFile -PublishSettingsFile $path | Out-Null
    Select-AzureSubscription -SubscriptionName "Visual Studio Ultimate with MSDN" | Out-Null
    Write-Host "$(Get-Date –f $timeStampFormat) - Set Azure subscription Completed " -foregroundcolor "green"
}

# Build the console application using DotNet CLI.
function build($basePath){
    $path = '{0}/{1}' -f $basePath, 'bin'
    Get-ChildItem -Path $path -Include *.* -File -Recurse | foreach { $_.Delete()}  
    Write-Host "$(Get-Date –f $timeStampFormat) - Bin Directory Completed " -foregroundcolor "green"

    dotnet build --configuration release $basePath | Out-Null
    Write-Host "$(Get-Date –f $timeStampFormat) - Dotnet Build Completed " -foregroundcolor "green"
}

# Create the ZIP package using System.IO API.
function createPackage($basePath){    
    $path = '{0}/{1}' -f $basePath, 'bin\Release'
    $zipPath = '{0}/{1}' -f $basepath, 'bin\Release.zip'

    Add-Type -Assembly System.IO.Compression.FileSystem 
    $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
    [System.IO.Compression.ZipFile]::CreateFromDirectory($path, $zipPath,
                                                          $compressionLevel, 
                                                          $false) 
    Write-Host "$(Get-Date –f $timeStampFormat) - Zip Package Created " -foregroundcolor "green"
}

# Deploy the ZIP Package to AzureWebsite using New-AzureWebsiteJob cmdlet.
function deploy($webSite, $jobName, $jobType, $basepath){
    $zipPath = '{0}/{1}' -f $basepath, 'bin\Release.zip'   
    New-AzureWebsiteJob -Name $webSite -JobName $jobName  -JobType $jobType -JobFile $zipPath | Out-Null

    Write-Host "$(Get-Date –f $timeStampFormat) - Completed Deployment " -foregroundcolor "green"
}

#Invoke all the above created functions
function execute($webSite, $jobName, $jobType, $basePath){
    setAzureSubscription $basePath
    build $basePath
    createPackage $basePath
    deploy $webSite $jobName $jobType $basepath
}
 

Above script does following activities -

  1. Using Azure PublishSettings profile, we will first set a valid Azure Subscription to PowerShell console context.
  2. We will delete all the previously build output files of console application. Then build the console application using “Dotnet Build” in release mode.
  3. We create the ZIP Package of Release folder using System.IO.Compression.ZipFile API and place it in the bin folder.
  4. Deploy the created ZIP Package to the AzureWebsite using New-AzureWebsiteJob cmdlet. This step will override the previous Job with the new package.
  5. Finally call all above methods in the mentioned order.

To make a successful deployment, we need to make following modifications to console application solution.

Include publishsettings, run.bat files at the root of console application. PowerShell script has been developed to pull the publishsettings file from root of console application, it can be modified to support any other path as well. More details about Run.bat can be found at Azure WebJob with .Net Core Console Application.

image

Include the CopyToOutputDirectory in CSProj file to copy Run.bat file to output directory i.e., Release folder.

<ItemGroup>
  <None Update="run.bat">
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  </None>
</ItemGroup>

Now when we execute the PowerShell script, we will have following output. Make sure Deploy-WebJob.ps1 is present on Desktop.

image

If we explore WebJobs section of AzureWebsite, we have following WebJob created.

image

When we execute the WebJob using Postman tool, we get 202 Accepted Response.

image

The generated output (which is a multiplication table of passed input) is shown below.

image

That’s it for now. Happy Coding and Stay Tuned!!!

You may also like...