Azure Function CI – 3. Deploying the Azure Function from the Build within VSTS

This post is the third and last one in a series of posts and will help you by deploying a CI build for a Azure Function.

Prerequisites

This blog post is totally written using the Azure Function Visual Studio template. Information and the Azure Function tools download are listed below:

In order to also make it deployable we need to take some steps.

Project changes

With all parts in place we can take the last steps to deploy the function. As there are still some small issues with the Preview Azure Function Tools for Visual Studio we need to use a ASP.Net Web Application project to create the deployment package. Right click the solution to add the new project of the type ASP.Net Web Application.

image_thumb171

In the next screen click the empty ASP.Net Web Application template because we do not need all the extra features that are included in the other templates.

image_thumb175

As mentioned above the project will be used to build a deployment package for the function. In order to get all files in place within the project we will configure a Pre Build event by opening the properties of the newly created project.

SET SOURCEPATH=$(SolutionDir)MSFT.Function\MsftHttpTrigger
SET FILEPATH=$(ProjectDir)MsftHttpTrigger

IF NOT EXIST "%FILEPATH%" (
GOTO MakeDir
) ELSE (
GOTO CopyFiles
)

:MakeDir
mkdir "%FILEPATH%"
:CopyFiles
xcopy /e /y "%SOURCEPATH%" "%FILEPATH%"

Now build the project, this will make sure all files are retrieved and placed within the project folder structure. When the build succeeds include the files within the project except the “project.json” and “project.lock.json”. By including the files within the project we make sure the files can be packaged by “msbuild”.  In the functions folder also include a dummy “run.csx” file in order to use the precompiled function within the Azure Portal. Last step for now is to delete the “web.config” file.

The solution now looks as followed:

image_thumb[6]

Now check / change the build order of the solution to make sure everything is build in the appropriate order to have the latest files in place within the deployment project.  Right click the solution and click on “Project Build Order”.

image_thumb133

Make sure the projects are build in the following sequence:

  1. Azure Function Project
  2. Implementation Project
  3. Deployment Project

When we would package the project at this time we would get a correct package but would also have some files we do not want to include in our package. This is the package.config file and the default “bin” folder of a web project. In order to exclude them unload the project in Visual Studio by right clicking and selecting “Unload Project”. When the project is unloaded right click it and click on “Edit [Project file name]”

image_thumb141

Add the following xml section within the configuration XML of the build configurations.

<ExcludeFoldersFromDeployment>bin</ExcludeFoldersFromDeployment>
<ExcludeFilesFromDeployment>package.config</ExcludeFilesFromDeployment>

When added the Property groups will look like:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  <DebugSymbols>true</DebugSymbols>
  <DebugType>full</DebugType>
  <Optimize>false</Optimize>
  <OutputPath>bin\</OutputPath>
  <DefineConstants>DEBUG;TRACE</DefineConstants>
  <ErrorReport>prompt</ErrorReport>
  <WarningLevel>4</WarningLevel>
  <ExcludeFoldersFromDeployment>bin</ExcludeFoldersFromDeployment>
  <ExcludeFilesFromDeployment>package.config</ExcludeFilesFromDeployment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "
  <DebugType>pdbonly</DebugType>
  <Optimize>true</Optimize>
  <OutputPath>bin\</OutputPath>
  <DefineConstants>TRACE</DefineConstants>
  <ErrorReport>prompt</ErrorReport>
  <WarningLevel>4</WarningLevel>
  <ExcludeFoldersFromDeployment>bin</ExcludeFoldersFromDeployment>
  <ExcludeFilesFromDeployment>package.config</ExcludeFilesFromDeployment>
</PropertyGroup>

Save the changes and reload the project by right clicking the it and selecting “Reload Project”. Commit all the changes you made and queue a new  build.

Deploy Azure Function

When the build succeed the build template needs to be adjust in order to deploy the Azure Function. Edit the build template and add so called “msbuild” arguments to the build action of the solution to make sure the package is created when the solution is build. In order to achieve this open the Build template editor of your previously created build and click on the “Build solution” action to add the following build arguments.

/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactstagingdirectory)\\"

image_thumb148

 

Next up is adding a task to the build template for deploying the resources to Azure. This can be be done by adding the task:  Azure App Services Deploy

image_thumb155

Configure this build step with the correct subscription, and Azure resource (the function were you would like to deploy to). Make sure you set a different package location, so that the action can find the deployment package created within the previous action:

$(build.artifactstagingdirectory)/**/*.zip

 

image_thumb183

 

Save and queue a new build. If this build succeeds your pre-compiled Azure Function is deployed to your Azure resource.

Note: This build template requires the Azure Function Host to be present within Azure if there is enough interest I will also create a series of post to deploy the Function by using ARM templates within the same pipeline.

Enable CI

Finally Continuous Integration needs to be enabled. To do this edit the build definition and click on the “Triggers” tab.

Triggers

Click the switch to enable the Continuous Integration trigger and keep the default setting to trigger build on changes committed to the master branch.

Continuous Integration VSTS

Related Posts

Restricting access to your Azure Web Application As you may know almost everything that is deployed to Azure is publicly available. As with Azure SQL Database you do not have a firewall available for...
Configure access to a private network for a Azure App Services On-Premise connections for Azure App Services can be created by using Hybrid Connections. Hybrid connections do not need any development or re-configu...
Extensions and Tips for deploying with Azure Resource Templates Working with Azure Services in different subscriptions means that the Azure Services need to run in different subscriptions. This often occurs when we...
Part 2 – Azure API Application to query the Azure SQL Database This post is the second in a series of three posts and will help you with the creation of identity pass-through authentication from a client applicati...
Part 1 – Azure SQL Database with Azure Active Directory Authentication This post is the first post in a series of three posts and will help you with the creation of identity pass-through authentication from a client appli...
Pass-Through Authentication with Azure Active Directory, Azure SQL, Azure API an... In situations you need to login to an application and use that identity to access an API (pass-through identity) and also get data from Azure SQL Serv...

Leave a Reply

Your email address will not be published. Required fields are marked *