Wednesday, 21 September 2016

Using environment variables to create a generic WebJob script in Azure

I recently had to write a script for a WebJob in an Azure App Service Web App which was reused across multiple copies of the same Web App.

The script was simple, it simply needed to make a request to a scripted action (in my case PHP) within the website hosted by the Web App. The request was via a URL, which was dependent upon the name of the web app, e.g.if the web app name is "mycoolwebapp" then the URL of the scripted action would be
https://mycoolwebapp.azurewebsites.net/bin/mytask.php
Instead of writing different scripts each time, with the web app name hardcoded, it's possible to use environment variables from within the WebJob script to refer to the Web App environment variables. In this case, "mycoolwebapp.azurewebsites.net" can be referred to via the WEBSITE_HOSTNAME environment variable in a CMD script as follows
curl https://%WEBSITE_HOSTNAME%/bin/mytask.php
This single script can then be uploaded to multiple Web Jobs for your websites, without needing a different version of the script for every website.

No comments:

Post a Comment