Creating a CI/CD pipeline for Azure Functions

Creating a CI/CD pipeline for Azure Functions

Azure functions are cute. You can fire up the online function editor from the Azure portal and start hacking away using many languages. In my case, I’d like to start writing some C# and being able to run it and debug it quickly. Eventually, I end up craving Visual Studio & ReSharper along with some unit tests.

Instead of using the Azure Portal, I like to use pre-compiled Azure functions created from Visual Studio

Why?

Can’t edit a function created from the portal in Visual Studio :pensive:

Functions created from the portal are typically “script” functions and they are not compilable. There is no DDL. As you may know, the extension is a .csx.

So what?

Today, you can’t deploy your Azure function created in the portal from VSTS. So, they are not as easy to add to your CI/CD pipeline. At this point, I typically start treating my functions like pets and not cattle.

I manually try to deploy a function between environments and paying special attention that the configuration matches between the instances.

All of this applies to Logic Apps too.

This is a is a big no-no for DevOps.

The Solution: pre-compiled functions using VSTS

1. Setting up the Project

Create a new Azure Function project

Add a function a trigger of your liking

Pick the function trigger

2. (Optional) Create the ARM Template

Create an ARM template for your azure function app and its service plan. If you don’t know how to do it, I wrote a blogpost about it.

For the sake of brevity you don’t have to create an ARM template. Be aware that this is still treating your function as a pet :cat: If you choose the dark side, then simply don’t add the Azure Resource Group VSTS task in the release definition.

3. Build and Release

Build Agent and Build Definition

Provision Build Agent with Azure Functions SDK or use simply use Hosted VS 2017 Build Agent.

Create a new build definition using the ASP.NET build definition template.

Then, if you’re trying this on a new solution, you can simply save and queue this definition.

Release Definition

The release will be the slightly more complex part. It will be very familiar to deploying an App Service.

Example Release Definition

1. Deploy the ARM template (Optional)

Start by adding an Azure Resource Group Deployment task to deploy the ARM template that will create the Azure Function Apps and their consumption service plans.

2. Use the App Service deployment task for each function app

To deploy the function code, for each function app add an App Service deploy task.

You will notice that in the build artifact, there’s a web deploy package for each of the function apps.

Make sure that you’re deploying the right package for the appropriate function app. The web deploy is speficied on the Package or folder field.

Queue it up and give it a go!

References