How to set up a CI/CD for Azure Functions with Azure DevOps

Amine Charot
Charot
Published in
5 min readMay 27, 2019

Azure Functions is an interesting tool, you can write a function from the Azure portal using many languages. You can also set up CI/CD pipeline which gets a code from a source control (In my case Git) and push it inside Azure Functions.

Using a CI/CD pipeline, you will benefit of a lot of things. The important one is “insurance”. You will be sure that you have the right process to deploy the right and same packages (which are built and tested) into all your environments.

Since we should automate the entire process, from the commit of the dev team to the deployment into the production environment. We will need an automation tool. Azure DevOps is free for the open source projects. It has a lot of services such as Boards, Pipeline… In this article we will only work with Azure Pipeline (Yes we don’t have to use everything).

From Git, Azure DevOps will pull the code, then it will enter to the build step where we will compile the code. Since we are using an C#, we will use the “dotnet” cmdlet. Then we should pass the unit tests.

Finally, Azure DevOps will deploy our Function into our “production” Azure Functions.

Azure Functions : Version Control

An Azure Function which will be probably deployed into the production environment, needs a set of developers that will write the code.

This code should pass through a version control such as Git or SVN. Once a developer push a commit, it should be built, tested …

Using the Version Control, you will benefit of more control of your code, an insurance that what you will deploy is saved and an easy way to rollback.

In my case, I’m going to work with Git. Once I write the code, I will push it to Github.

The function that I’ll use is simple. It does a simple addition, subtraction or division. You give it a body :

{
"Operation" : "addition";
"A" : "10",
"B" : "6"
}

The Function should give a result of 16.

Azure Functions : Build & Unit test

The CI in the article title stands for Continuous Integration. It means continuously building an application. The CI and the Version Control go together like a charm. Using CI, you encourage your developers to use a main branch in a repository to integrate and build their code instead of doing it manually in isolation. This will cause a lot of errors when they’ll try to integrate their code.

When a developer makes a change inside the source code, the CI must be able to fetch the code and build it.

When I say Build, I mean compile the code, test it and package it.

I made a simple Unit Test, just to show you that we are able to test an Azure Function.

Using Azure DevOps, we will create a build pipeline. Click on New pipeline.

Then we should specify the location of our source code. In my case it will be charotAmine/functionCICD.

Next, we should select a YAML template which will define our build. Since my app is an ASP.NET one, I’m going to choose ASP.NET Core Template.

Modify the YAML file to build,test and publish the artifacts. It should look like

Let’s talk about the YAML Template steps :

  • dotnet build : It will build the solution;
  • dotnet test : It will run the unit tests;
  • PublishTestResults@2 : To explore the tests in the GUI, they must be available and published. That’s the mission of PublishTestResults;
  • dotnet publish : It will create the Function App artifacts;
  • PublishBuildArtifacts@1 : It will make the artifacts available for the release pipeline.

Once you’ve configured the build, you can run it. You should see something like (yeah life is green, I swear I didn’t cheat :D )

The thing I like the most about Azure DevOps, is its UI. To visualize the tests, click on Tests and HOOOORAY! There are no test failures !

Azure Functions : Deploy

Now that our build pipeline is created and it is green. We can deploy our function !

First of all, you need to create a new release pipeline. Click on New pipeline.

Select a release template. In our case, we should select the Azure App Service deployment.

Once is done, we should add an artifact.

Select the Artifact source

Now, we should configure the Azure App Service deployment.

Then, select the package

Don’t forget to save your pipeline. Then, run it !

Ha ! Once its finished, you may see something like, you know something green maybe ?

Finally, I’ll test the addition in the Azure Portal, just to prove that everything was really really green :D

Bella ciao,

--

--