How to deploy Azure WebJob as part of automatic VSTS deployment - azure

Is there a way of having my Azure WebJob automatically deploy without needing to right click and select "Publish as Azure WebJob" every time? i.e. when I check-in my solution it is automatically deploy to the Azure Portal webjob section

While I tried to accomplish this, I found out that there is no tooling support for dotnet core projects as of now. The proposed webjobs.props/ msbuild solutions are all dotnet framework specific.
However I also found out that a webjob can be anything that's executable on the local machine (could be node.js or just a batch command).
The key is to understand how WebJobs are recognized by the host:
A WebJob on a Windows host is (by what I gathered from experimenting with it) just a run.cmd file that contains instructions on how to start the webJob. For dotnet core that would be dotnet MyDll.dll %* (%* to pass arguments for output redirection etc. down from the host).
Now depending on wether the job is continuous or triggered the run.cmd file needs to be located either at app_data/jobs/continuous/[NameOfJob] or app_data/jobs/triggered/[NameOfJob]. For the triggered job you could also add a schedule using a settings.job file like described here.
If there is a run.cmd at the proper location it will be recognized as a WebJob
Now to deploy a webjob using VSTS regardless of the runtime framework follow these steps:
Build/Publish your WebJob if necessary to get the executables
Add a run.cmd file next to your webjob executables that contains the proper startup instructions. You could also add settings.job here if needed.
Create the folder hierarchy app_data/jobs/[triggered/continuous]/[nameOfJob] and copy your executables into the lowest folder. Make sure run.cmd is directly under the [nameOfJob]/ directory
Zip the app_data folder so that the zip-package contains the entire hierarchy
Publish your zip file using the normal Azure App Service Deployment task (just like deploying the web app)
And that's it.

Yes you can.
Brady Gaster has written about this on a blog post (haven't tried it myself).
From what I gather, the TL;DR; summary is the following:
add a file named webjobs.props to the properties folder of the Web Application Project or Console Application you’re publishing
After that, edit the file so the ManagementCertificate, SubscriptionId and PublishSettingsPath are filled with correct data.
Now you should be able to publish the files using the command
msbuild WebJobDemo.Web.csproj /p:DeployOnBuild=true /p:PublishProfile=WebJobDemo /p:VisualStudioVersion=12.0 /p:Password=asdfasdf
(Note, post is written for VS2013)
Afterwards you should see something like this.
You could of course automate this in VSTS (or any other build/deployment tool for that matter) whenever something is checked in to your repository.
A (rather complete) answer on how to do this in VSTS via the command-line can be found in this answer: https://stackoverflow.com/a/45703975/352640

You can do it through Continuous Integration Build (trigger the build once check in).
Regarding deploy WebJob, you can generate a package through Visual Studio Build task with /p:DeployOnBuild=true /p:WebPublishMethod=Package /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.stagingDirectory)" argument.
Then deploy it through Azure App Deployment task.
More information: Deploying and Schedule Azure WebJobs from VSTS to Azure Web App

In Visual Studio in order to enable automatic WebJobs deployment together with a Web project right-click the Web project in Solution Explorer, and then click: Add > Existing Project as Azure WebJob and create your WebJob.
More details can be found in an article by MS - webjobs-dotnet-deploy-vs

Related

how to configure webjob to access the web app's reference project files in azure

I have a .net core web app that I deploy to azure using Azure Devops build/release pipelines. This project references Business project and a Models project that are part of the three-tier solution. The Models project consists of Entity Framework 6 code first models (including migrations).
Recently I have had to deploy a triggered webjob in order to accomplish a long running task. This was just created as a normal .Net console app and then published from within Visual Studio 2017 by selecting "Publish as Azure Web Job". This webjob is published to and runs under the .net core web app service mentioned above. It references the same Models and Business project that the .net core web app references.
My issue is that whenever the model is changed by introducing db migrations, the web job also must be updated since the models.dll that is published as part of the webjob project resides separately in a directory app_data\jobs\triggered\webjob under the main web app.
Is there any way to configure my webjob so that the models.dll and business.dll are directly referenced from that of the main web app? Failing that, how can I modify the Azure devops process to copy these files to the directory of the webjob upon successful deploy? Is there a guide for this?
how can I modify the Azure devops process to copy these files to the directory of the webjob upon successful deploy? Is there a guide for this?
AFAIK, we could use powershell scripts and git command to filter whether the file modification comes from models.dll and business.dll files, like:
$editedFiles = git diff HEAD HEAD~ --name-only
$editedFiles | ForEach-Object {
Switch -Wildcard ($_ ) {
'SubFolderA/models.dll*' { Write-Output "##vso[task.setvariable variable=UpdateFile]True" }
# The rest of your path filters
}
}
Code comes from here.
Then add custom conditions in the next task in the build pipeline:
and(succeeded(), eq(variables['UpdateFile'], 'True'))
In the next task, we could use the kudu API to update these files to the directory of the webjob upon successful deploy:
GET /api/zip/{path}/
Zip up and download the specified folder. The zip doesn't include the top folder itself. Make sure you include
the trailing slash!
PUT /api/zip/{path}/
Upload a zip file which gets expanded into the specified folder. Existing files are not deleted
unless they need to be overwritten by files in the zip. The path can be nested (e.g. `folder1/folder2`), and needs to exist.
You could check the similar thread and the document for some details.
Hope this helps.

How to publish asp.net core web job to azure app service

I have started with asp.net core 2 web app and I can publish it to App Service from Visual Studio using web deploy.
I've created new clean .net core 2 console app. I'm able to upload it as webjob and run using Azure Portal, but how do I publish it from local command line or Visual Studio?
Basically, I don't care whether it will be published alongside the Web Application or as standalone.
EDIT: I've somehow managed to get the publish dialog by right clicking the project and selecting Publish (not Publish as Azure WebJob) as menioned in the docs. But I still don't know what did the trick. Installing Azure SDK? Adding webjob-publish-settings.json? Adding Setting.job?
Publish .net core as webjob with Azure portal:
As you know:
A WebJob looks for specific file type, for example (.cmd, .bat, .exe, etc…)
To run a .NET Core console application you use the DOTNET command
Therefore, you need to create a file with an extension which is WebJob looking for that executes.
1.You could create a .net core conosole application. After running it, you will have the follow file in your projectname/bin/Debug/netcoreapp2.0
2.Create a run.cmd file under it. And the run.cmd content is as below:
#echo off
dotnet ConsoleApp7.dll
3.To deploy the .NET Core console application to an Azure App Service Web App Web Job access the Azure portal and navigate to the Azure App Service where you will host the WebJob.
Click on the WebJobs link and the Add button.
4.Upload the netcoreapp2.0.zip
5.Once the WebJob is successfuly uploaded, it will render in the WebJob blade. Click on it and you will see the Run button.
6.When you write output to the console using the WriteLine() method, it will show in the Run Details window on KUDU/SCM.
For more detail, you could refer to this article and this one.
Update:(publish with command line)
1.First, download your publish settings file of your webapp from Azure Portal.
2.Prepare the .zip folder you have created.
As David said, you could use WAWSDeploy to publish webjob with command line.
You could download WAWSDeploy with this link.
3.Then go to WAWSDeploy/bin/Debug folder to open the local command line.
Try the following command to deploy the webjob:
WAWSDeploy.exe DotNetCoreWebJobSample.zip [WEBSITE_NAME].PublishSettings /t app_data\jobs\triggered\DotNetCoreWebJobSample /v
Target directory will be app_data\jobs\triggered\[WEBJOB_NAME]. If this web job is a continuously running one, replace triggered with continuous.
Note:you could put the WAWSDeploy.exe and publish settings file and the .zip into a folder. If not, you should give the full path of publish settings and .zip file. So that you could publish webjob successfully.
For more detail about WAWSDeploy, refer to this article.
Make sure your csproj includes correct SDK's:
<Project Sdk="Microsoft.NET.Sdk;Microsoft.NET.Sdk.Publish">
Then just right click on the project in Visual Studio and click publish, select Microsoft Azure App Service and you should see the WebJob publish options:
Also notice that you should use Microsoft.NET.Sdk and not Microsoft.NET.Sdk.Web
If you are using Microsoft.NET.Sdk.Web, Visual Studio assumes that you are deploying to WebSite and not WebJob. The publish dialogs are slightly different for WebSite and WebJob. For example, for WebJob project you can specify WebJob Name.
You might be interested in:
github/aspnet/websdk/issue: WebJob publishing for Microsoft.NET.Sdk.Web
github/aspnet/Mvc/issue: How to publish console app as a WebJob rather than Web App
There is a great articel about Develop and deploy WebJobs using Visual Studio - Azure App Service that covers your question.
Basically after installing the prerequisites (depending on your VS version) you can
Right-click the Console Application project in the Solution Explorer, and then click Publish as Azure WebJob.

Azure WebJob not included in web app during build when using Microsoft.Web.WebJobs.Publishing 1.0.10 on TFS build machine

I'm currently using the 'Add Azure WebJob' functionality as part of the Azure SDK in Visual Studio to add a WebJob into a Web App that gets deployed to Azure. I use a custom .proj file for my build, and when I build using msbuild on the command line, the package that gets generated correctly includes the webjob. However, when I use TFS to build the custom .proj file, WITH EXACTLY THE SAME COMMAND the webjob doesn't get included. For whatever reason, the publish targets for the webjob in the targets file provided by Microsoft.Web.WebJobs.Publish nuget package simply don't get executed. Has anybody run across anything like this before ?

TFS Team Build: Deploy Azure Cloud Service

I'm having some trouble deploying an Azure Web Service using the new Team Build that is available at Visual Studio Online. When the solution have been built successfully, the deployment task tries to find the CsPkg and the CsCfg, which fails.
How do I specify the "Path of CsPkg/CsCfg under the default artifact directory" that is required? Do I have to specify some MSBuild arguments?
You can use the $(build.stagingDirectory) variable in the CsPkg/CsCfg fields. This will be the root of your repo. Your binaries will be at some location under there. E.g. $(build.stagingDirectory)\*.cspkg and
$(build.stagingDirectory)\*.cscfg
There's a huge list of built-in variables on MSDN.
You may also have to build your cloud service project with the MSBuild argument /t:Publish
I just checked: If you use the "Cloud Service" deployment template, all of this stuff is automatically filled in for you, including the paths.

Publish Azure Application with MSBuild

Visual Studio 2013 has a publish wizard for Cloud Service projects that packages and deploys a cloud service based on settings persisted in a .azurePubxml file.
I'm setting up automation of this process on a CI server, and want to leverage this functionality within MSBuild, but I'm unsure if it's even possible.
I have found a lot of articles that talk about shelling out to a PowerShell script and using a custom MSBuild target file to do the deploy, but that seems like a duplication of information that I'd rather not delve into.
Given that I have the Azure subscription credentials installed on the build machine, VS 2013, and Azure SDK 2.2, can I invoke the same mechanism that VS 2013 uses in the publish wizard to package and deploy my cloud service?
Currently, using MSBuild with the publish target, I can package the service into a .cspkg file, but I cannot figure out how to trigger to package/deploy based on a profile (.azurePubxml file) that defines the VS build config, and all of the Azure settings.
So, as you pointed out, the default targets will only give you a package. From there, Visual Studio is publishing things using their own infrastructure. Publishing the build from a CI server is considered something external to actual build process and rather something that is part of the deployment process.
If your CI technology has support for PowerShell then it's as easy as automating the Windows Azure PowerShell cmdlets to do your bidding. There's a nicely detailed article on how you might do this with Team Builds in TFS here on MSDN.

Resources