Deploying .NET Core WebJobs to Azure using Azure Build Pipeline
Using Azure Build and Release pipeline to deploy WebJob to a WebApp instance
Azure WebJobs are a great way to run background processes for your Web application on hosted on Azure WebApp. Althought Microsoft does not yet provide Visual Studio project templates for .NET Core (it only has built in project template for .NET framework 4.x) it is still possible to develop WebJob using .NET Core and run in on a WebApp. If you want to read more about writing WebJobs using .NET Core checkout my article Writing Azure WebJobs with dependency injection in .NET Core.
In this article we'll focus on automatic deployment of WebJob to Azure WebApp using Azure DevOps and it's build pipeline.
Azure WebApp folder structure
Before we start setting up the pipeline, we need to understand how WebJob files are organized on WebApp file system. To analyze that we need to first deploy a simple WebJob to the WebApp where we are going to run it. I used the same WebJob code described in Writing Azure WebJobs with dependency injection in .NET Core article, published it on my local development machine, packed it to zip file and deployed to WebApp using Azure portal UI.
Once WebJob is created inside the WebApp we'll have it's binaries and configuration copied to WebApp file system. To investigate WebApp filesystem you can connect to it via FTP, but much easier way is to do it directly from the Azure portal UI. From Azure portal you can launch WebApp Editor, which will start in a new browser tab and which looks quite similar to Visual Studio Code editor
From the WebApp editor you can see that WebJob folder with binaries is stored at WebApp root (wwwroot) under /app-data/Jobs/Triggered. The last folder in hierarchy varies depending on the trigger tipe for the WebJob. Depending on the triggered type this folder can be
Now one funny thing is that once you created a WebJob, there is not UI to update the WebJob settings. The only thing you can do it is by editing Settings.job file from WebApp Editor UI or overwrite it with a deployment either from Visual Studio or from Azure DevOps pipeline.
Since now we know the structure and the way WebJobs are organized on WebApp we can start with configuring Azure DevOps build pipeline.
Azure DevOps build pipeline configuration
Let's automate our WebApp deployment, so that we can always have latest code changes deployed to Azure WebApp as soon as the code is pushed to the remote repository. Since WebJobs are mainly lightweight and used for recurrent or continuous task, I will keep the build and deploy pipeline as simple as possible. Actually I will not even use the release pipeline for this and everything will be done within the Build pipeline with only 2 simple tasks (yup only 2 tasks).
For a sample pipeline I am using public Azure WebApp .NET Core project https://github.com/dejanstojanovic/Azure-WebJob-Core which will be deployed to a free tier WebApp instance on Azure. Once GitHub source is connected to the pipeline, you can start adding tasks.
Sample .NET Core boilerplate build pipeline comes with bunch of tasks which are needed in case you are doing some complex application deployment process. For example, in order to run Unit tests, you need to build project first and before that you need to restore it because you may want to use a custom NuGet feed. I will skip all these steps in this sample as they will just produce an overhead in the pipeline.
First step is to publish the project for which I used dotnet publish task. Dotnet publish command basically does restore and build by default, so skipping restore and build tasks will not break anything. Here is how that publish task configuration looks like in my sample build pipeline.
If you check the arguments input for the publish task, I entered the full path where the job will be deployed, same as the one you can see in the WebApp editor. I also unchecked "Zip Published Project" since I can simply just publish the folder. You can zip your publish output, but unless you really have big performance issues of your build, no need to complicate things. Because of choosing not to compress, I can simply just push the published output to the WebApp instance which is done in the second task.
Second task in the pipeline is Azure App Service Deploy task. This task will literarly take the previous publish task output and just copy it to the WebApp filesystem. Since output already has the proper folder structure, no additional steps are required.
No additional steps are required. Once you run the build pipeline, you will have your WebApp build and deployed to the WebApp you configured in the Azure App Service Deploy task. This two task approach does not apply if your WebJob projects has some significant functionality and Unit tests, but for simple WebJobs which are there just to perform some dummy repetitive task, this should be just enough.
- Azure WebJob project template targeting .NET Core
- .NET Core command-line interface (CLI) tools
- dotnet publish
Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.