Freigeben über


Deploying and Schedule Azure WebJobs from VSTS to Azure Web App (With bonus CRON Scheduling)

WebAPI apps with WebJobs schedules are currently the intuitive way to work with Web Apps.
In this article, I am going to show you how to deploy a WebApp along with Web Jobs, and as an added bonus, use Azure’s internal CRON Scheduler to schedule the WebJobs. Sweet! Let’s get started.

We already have this blog that shows how to publish a Web App from VSTS. We’ll expand on that.

Add a New Azure WebJob Project

Create a new Azure WebJob Project, File->Add->New Project
1 2

You could set up the schedule for this,

3

This creates a webjob-publish-settings.json file

Sample:

{

"$schema": "https://schemastore.org/schemas/json/webjob-publish-settings.json",

"webJobName": "WebJob",

"startTime": null,

"endTime": null,

"jobRecurrenceFrequency": null,

"interval": null,

"runMode": "OnDemand"

}

This will use Azure Scheduler Services to schedule the job.

 

Going back to our build definition from this, let’s add another task for Azure Web App Deployment for the WebJob,
4
Modify the MSBuild Arguments as follows, removing, [/p:PackageAsSingleFile=true].

/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.stagingDirectory)"

So, in the artifact drop, we will get 2 WebDeploy Packages, one for the Web App and the other for Web Job.

We should be good to go, let’s queue a build.

5

 

Post build, you’ll notice that separate zip files are created,

6

Let’s check our Web App on Azure Portal,

7

Simple as that, the WebJob automatically is placed under the appropriate place, automatically.

 

BONUS! (Using CRON Scheduler)

If you’re familiar with the CRON Scheduler, or want more flexibility in scheduling, Azure has an internal CRON Scheduler, that can be used, https://azure.microsoft.com/en-in/documentation/articles/web-sites-create-web-jobs/#CreateScheduledCRON

Note: This technique is available to Web Apps running in Basic, Standard or Premium mode, and requires the Always On setting to be enabled on the app.

For this to work, simply create a file called Settings.job

9

For instance, to trigger the WebJob every 15 minutes, your settings.job would have:

{

"schedule": "0 */15 * * * *"

}

 

Now as part of the build this file must also be packaged to the Web Deploy package of the Web Job.
To do that, ensure this file is copied to the Build Output Directory.

8

There you go! You now have a custom scheduled WebJob using CRON Expression as well.

Cheers!

Content: Manigandan B
Review: Venkat NA

Comments

  • Anonymous
    June 22, 2016
    I have the same setup as you in this example where my build definition contains a webApp and a webJob. If the deployment order has the webAPp first and the webjob second, it seems some bin files get deployed and overriden from the webjob deployment, causing an error when browsing the webapp.
    • Anonymous
      June 30, 2016
      If you feel that the files are being overridden, please check the Set DoNotDeleteFlag. That should stop removing files.
      • Anonymous
        January 17, 2017
        I am facing same issue ..If deployment order has the webapp first and webjob second bin files from webjob package is getting deployed which is causing error when browsing web app.. I have already checked DoNotDeleteFlag
        • Anonymous
          January 17, 2017
          Hi Neha, I just created a new build with a WebApp that has 3 webjobs. It automatically deploys all the webjobs on seperate directories. I even used a single deployment task to do this (just the webapp) With the support ticket you already have with us, we will get in touch with you for a discussion soon.
  • Anonymous
    August 27, 2016
    Hey - thanks for this. I have a single build step as you have, that builds the solution with the webapp and the webjob. I've removed the 'PackageAsSingleFile' flag from the build arguments. The webapp deployment step successfully executes first, but when the webjob deployment step executes it complains: Cannot update deployment status for demowebapp - {"Message":"Deployment with id '50' exists"}It also seems to 'overwrite' the webapp - I no longer seem to be able to get content from it.However, the webjob does seem to be deployed.I can't seem to get them both to deploy together.Any ideas??Thanks,Tim
    • Anonymous
      September 05, 2016
      It doesn't quite work well if you do a single packaging. That's why, we package it separately, use different deployment steps for each web app and web job.
  • Anonymous
    August 29, 2016
    Hi,I am not able to schedule the jobs .My web jobs always configured as On-Demand jobs instead of scheduled jobs. My website runs in standard and I have enabled always-on as well.
    • Anonymous
      August 29, 2016
      I have found that the batch job runs every 15 minutes as per the scheduled time.The problem is , eventhough the batch job is scheduled in the mamagement portal it still shows as the On-Demand process.Thanks
      • Anonymous
        September 05, 2016
        Hi Prakash, It's because the system will not know that it uses CRON scheduling in the background. It will only recognize the azure scheduler and it's schedules.
  • Anonymous
    March 22, 2017
    Hi, I am trying to follow your post but the problem is that the WebJob does not get deployed into the artifacts folder, only the WebApps are being deployed. I am using Azure WebJob 2.0.0 and seems like the Build step only create zips for WebApps
    • Anonymous
      April 23, 2017
      Javier, I'm having the same issue. Did you ever find a solution?
    • Anonymous
      May 17, 2017
      i am having the same issue
    • Anonymous
      September 20, 2017
      I had the same issue...Installing the nuget Microsoft.Web.WebJobs.Publish solved the problem.However, I am wondering if there is way to get this working without the publish package?
  • Anonymous
    May 04, 2017
    Hello,I have a app service which contains a website and a web API (using virtual apps) and 2 webjobs. Everytime the deployment has finished, only one webjob is deployed (the last one replaces the first).Is there any other setup that should be made in order to get it working?
  • Anonymous
    May 17, 2017
    i am not getting 2 files creating in stagingdirectory after remove singlefile=true from msbuild arguments.please let me know what possible reson so i m not getting thngs done