Wednesday 26 April 2023

CI/CD with GitHub Actions to deploy background jobs as Azure WebJobs

 A background job is a task that we need to run in the background like a scheduled job and the Azure WebJobs provides this capability as part of the Azure App Service.

Azure WebJobs is the feature of Azure AppService where you can run your background tasks along with your web application deployed in App Service with NO additional cost.

To know more about WebJobs please read through this article here: https://learn.microsoft.com/en-us/azure/app-service/webjobs-create

In this article, we will talk about how to automate the background task deployment to the Azure AppService WebJob along with the web application. Please note that you can also deploy only the background job as a web job in the Azure app service too.

If you would have followed me on my previous article about automating the web application deployment to the Azure AppService here “CI/CD with GitHub Actions to deploy Applications to Azure App Service” then you have almost got this to 90%. Yes, it is 90% because the deployment and the process are all the same and for rest 10% all you have to do is, publish your background tasks to a specific folder along with your web application.

Note: If you are hosting the web application along with web job then you must have to host it together in a single pipeline as I’ll be showing here or else the wwwroot folder will get overrriden with lastest deployment and this happens because everything goes into the wwwroot folder. Web application content will got directly to the wwwroot folder and background task will insided wwwroot/App_Data/Jobs/Triggered/{your Job Name}

For this example, I’ve got a simple .net console application as a background task which I’m going to deploy as a web Job.

To achieve this, We need to modify Step 7 from my previous article to prepare the app settings. In my case, I’m adding one more execution step in the pipeline as I’m deploying the web application and web job both. So the Step 7 code will finally be as:

In the above code, I added the extra step in the build job to modify the connection string of the background task “DemoTask” which is a console application for me inside a folder called “webjob” from my code repo.

Next, we will modify Step 8 from my previous article, to add a new step to build and publish the console application as a background task i.e. web job. Here is the modified step 8:

In the above code, if you observed, the background task is being published in a specific folder as “${{env.DOTNET_ROOT}}/myapp/App_Data/Jobs/Triggered/${{ env.webJobName }}”

So this is the only catch that, your background task must be in App_Data/Jobs/Triggered/{job_name} inside wwwroot which is the base path.

Thats all. We are done and this is how the folder structure will look like in Azure App Service.

To configure your background task schedule and all, either you can do it directly from the portal Or you can do it through code using web job sdk. Check out the details here: https://learn.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to

Hope you enjoyed the content, follow me for more like this, and please don’t forget to LIKE it. Happy programming.