Questions? Feedback? powered by Olark live chat software
Hopp over navigasjon

Auto-update your Azure Site Extension

Posted on 3 juni, 2015

Software Engineer, Microsoft Azure
With Azure Site Extensions, you are able to extend any Azure Web App pretty easily and enhance the site’s functionality. For example, there are extensions which integrate New Relic and Visual Studio Online to any Azure Web App.  You can even write your own and post them to the Site Extension gallery.  If you don’t know how to create a site extension and publish it to the gallery, you can find the instructions here. However, updating those site extensions can be a bit of a hassle. Previously, when publishing a new version of your site extension, you needed the users to browse the Site Extension Gallery to obtain the newest version and then manually deploy the update via the update button. Now, we’re introducing a way for you to let your site extensions auto-update themselves with just one line of code!

Enable auto-updates

One Liner

To make your site extension auto-update you just need to include the following script tag in your site extension’s home page (or any page your users will often visit):
<script src="http://cdn.siteextensions.net/lib/siteExtensionUpdater/siteExtensionUpdater.1.0.0.min.js"></script>
Simple as that. Now every time anyone navigates to your site extension, the script will be running in the background and will check to see if there’s a newer version of the extension available. If there is, the script will update the user’s copy of your site extension and ask them to refresh their page to get the new bits. All in just a few seconds!
Note: The auto-update feature works for site extensions with a UI.

Install via Bower

If you prefer using Bower, we've got you covered there as well. You can install the script via bower by running:
bower install  azure-site-extension-updater

Using a custom applicationHost.xdt file

If you don’t use an applicationHost.xdt file for your site extension, you can skip this section.  If you do, there’s something you need to be aware of. One thing the auto-updater needs from you is that the id you publish in your site extension’s *.nuspec file should match the route you list for your site extension in your applicationHost.xdt file.
Note: If you don’t include an applicationHost.xdt file with your site extension, Kudu will generate a default one for you with the correct route
Example *.nuspec file:
<?xml version="1.0"?>
<package >
  <metadata>
    <id>MySiteExtension</id>
    <version>7.0.0</version>
    <authors>ZainRizvi</authors>
    <owners>ZainRizvi</owners>
    <licenseUrl>http://www.github.com/zainrizvi/mysite/license</licenseUrl>
    <projectUrl>http://www.github.com/zainrizvi/mysite</projectUrl>
    <iconUrl>http://www.github.com/zainrizvi/mysite/image.ico</iconUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Package description</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2015</copyright>
    <tags>Tag1 Tag2</tags>
  </metadata>
  <files>
    <file src="\**\*.*" target="content" />
  </files>
</package>
Example applicationHost.xdt file:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.applicationHost>
    <sites>
      <site name="%XDT_SCMSITENAME%" xdt:Locator="Match(name)">
        <application path="/MySiteExtension" xdt:Locator="Match(path)" xdt:Transform="Remove" />
        <application path="/MySiteExtension" applicationPool="%XDT_APPPOOLNAME%" xdt:Transform="Insert">
          <virtualDirectory path="/" physicalPath="%XDT_EXTENSIONPATH%" />
        </application>
      </site>
    </sites>
  </system.applicationHost>
</configuration>

Limitations

The auto-update script comes with two caveats:
  1. Because the auto-update script is a javascript file that has to run in a browser, for your site extension to actually get updated your user has to navigate to the page that imports the script. This means only UI-based site extensions can be auto-updated.
  2. The update occurs by making api calls to the Kudu (scm) site, but in order to get access to the Kudu site the user has to be authenticated. This means that if the route to your site extension isn’t based off of the Kudu site already (if your site extension isn’t routed under https://<sitename>.scm.azurewebsites.net/...) then the site extension will lack the necessary authorization and won’t get auto-updated. (By default your site extension is routed under the Kudu site)
 

How does it work?

The script parses the url of the page to figure out the name of the site extension. So if your site extension is located at:
 https://<sitename>.scm.azurewebsites.net/MySiteExtension/
It’ll figure that the name must be “MySiteExtension”. With the name in hand, the script just makes a couple API calls to the Kudu site to see if there is any updated version of your site extension available. If updates are available, then the script prompts Kudu to install the latest version of the site extension by sending:
PUT https://<sitename>.scm.azurewebsites.net/api/siteExtensions/<siteExtensionName>
  This is the same request made when you click the “update” button on a site extension in the gallery. To learn more, you can find the GitHub project and the unminified source here. Happy coding!