Visual Studio 2017 Tools for Azure Functions and Continuous Integration with VSTS
UPDATE - July 2017
Code Deployments are now available in Azure Functions with the new Visual Studio 2017 Azure Functions Project type as of last month. Kudu hadn't supported the latest version of MS Build which from my understanding was the root cause of the original limitation. With that in mind for a quick Continuous Delivery setup, the Kudu Build is a great option. Saying that VSTS will give you a lot more functionality if you need it so if you've got VSTS environment you really can't go wrong!
The Azure Functions team has been rolling out updates and improvements to Azure Functions at a record pace. Recently announced at MS Build last week was added support for Visual Studio 2017 (in preview). The new tools brings an improved developer experience that among several things adds support for c# attribute based configuration as opposed to having to manage separate json configuration files for each function. You can read more on this announcement and how to download the tools over at https://blogs.msdn.microsoft.com/webdev/2017/05/10/azure-function-tools-for-visual-studio-2017/ .
Along with the tool update also comes a new project type and build process which may have an effect on any continuous integration process you may have in place if you want to move from the Visual Studio 2015 to Visual Studio 2017 developer environment.
The Continuous Integration story has always been a bit of a moving target with work arounds you would expect from an early release toolset. To be fair the entire toolset is in Preview so that's just part of the contract of developing with preview tools but none the less the Azure Team has been great assisting the community with workarounds.
I discovered the need for a work around after trying to integrate a new Function App built with Visual Studio 2017 Tools for Azure Functions into Azure via Continuous Deployment. Continuous Deployments in Azure Functions are pretty straight forward to setup (normally) and is available from the Platform Features tab in Azure Functions. Step through the wizard process and you're good to go.
Unfortunately everything looks great until you inspect the deployment details and find that no functions are deploying:
I suspected this would be the case with the new project type so it didn't come as a huge surprise. Visual Studio 2015 required some workarounds and so would Visual Studio 2017.
Posting an inquiry over on Github within an existing issue for Function Tooling for Visual Studio 2017 (https://github.com/Azure/Azure-Functions/issues/201) it quickly came to light that the new project type wasn't yet supported on Kudu, which Azure uses for Continuous Integration, so we would need to use VSTS. You can find out more details on a new issue located at https://github.com/Azure/Azure-Functions/issues/291
So the workaround is we're going to be using VSTS (Formerly called Visual Studio Online) to manage our build process. VSTS has been making great improvements to their CI\CD story in the last year so I didn't need any arm-twisting to switch gears.
Setting up Continuous Integration on VSTS For Azure Functions
You will, of course, need to have access to VSTS for this solution so if you don't head over to VSTS and create your free account. It is NOT necessary to host your code here - you can still use Github or other supported repositories as we will be using the Build Services.
There are a couple important highlights that I want to call attention to that will be covered in more detail in the walkthrough:
- The build requires Nuget 4
The build contains some custom MS Build Arguments
We need to use a Visual Studio 2017 Build Host
New Definition Step #1 - Visual Studio Template Selection
From the Build & Release tab in VSTS choose Build. Select "+New" button to get started building a new build definition. We're going to start with a Visual Studio Template.
New Definition Step #2 - Cleanup Default Tasks
You'll have some default tasks that we don't need for our most basic build sample. You can, of course, choose to add additional tasks as required by your needs but we're going to keep it simple for now. I've removed unneeded tasks until we have just the basics below:
New Definition Step #3 - Configure Sources
I host my code in VSTS Git Repositories in my current collection so configuration is pretty straight forward. You can optionally select branches. Note the ability to choose repositories from other providers.
New Definition Step #4 - Configure Nuget
I've kept all the default values EXCEPT the important one of selecting Nuget 4.0
New Definition Step #5 - Configure Build Solution
Another important step is to update the MSBuild Arguments with the following options (provided by the Azure Functions team on the Github Issue)
/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactsstagingdirectory)\output.zip" /p:DeployIisAppPath="Default Web Site"
These extra build steps will ensure that our package is created and in a location of our choosing. Take note of the location of $(build.artifactsstagingdirectory)\output.zip as we'll use that later.
New Definition Step #5 - Add Azure App Service Deploy
In this step we'll be adding a new and final task. We need to ensure our package gets deployed to the Azure App Service where our functions live. There are lots of tasks available so after selecting Add Task I find it helpful to quickly filter to Azure:
VSTS is going to quickly complain by notifying you have some extra details to provide so lets go ahead and select the task so we can get things wired up.
You will need to provide your subscription details that may require authentication and choose an existing App Service Name to deploy to. In addition I've updated the Package folder to match the same location the build solution process placed the output package.
New Definition Step #5 - Save & Queue
It's time to enjoy the fruits of your labor (well hopefully!). You can choose Save & Queue or just return later and select Queue.
IMPORTANT: Don't forget to select a Hosted VS2017 Build Agent or your build will fail!
If all goes well you should see your build process start up and complete and have all Green!
With this you should be able to get a simple Continuous Integration going for your Azure Functions. Big thanks to the Azure Functions Team for being so responsive with requests for assistance!