Deploy an Azure Function

You can automatically deploy your Azure Function after every successful build.

Before you begin

Based on the desired runtime, import (into Azure DevOps) or fork (into GitHub) the following repository.

If you already have an app in GitHub that you want to deploy, you can try creating a pipeline for that code.

However, if you are a new user, then you might get a better start by using our sample code. In that case, fork this repo in GitHub:

    https://github.com/microsoft/devops-project-samples/tree/master/dotnet/aspnetcore/functionApp

Build your app

Follow the guidance in Create your first pipeline to setup the build pipeline. The CI steps will be similar to any Nodejs or .NET Core apps. When you're done, you'll have a YAML pipeline to build, test, and publish the source as an artifact.

We aren't yet advising new users to use YAML pipelines to deploy from Azure DevOps Server 2019. If you're an experienced pipeline user and already have a YAML pipeline to build your java function app, then you might find the examples below useful.

YAML pipelines aren't available on TFS.

Now you're ready to read through the rest of this topic to learn some of the more common configurations to customize the deployment of an Azure Function App.

Azure service connection

The Azure Function App Deploy task, similar to other built-in Azure tasks, requires an Azure service connection as an input. The Azure service connection stores the credentials to connect from Azure Pipelines or Azure DevOps Server to Azure.

You must supply an Azure service connection to the AzureFunctionApp task. Add the following YAML snippet to your existing azure-pipelines.yaml file. Make sure you add the service connection details in the variables section as shown below-

variables: 
  ## Add this under variables section in the pipeline
  azureSubscription: <Name of the Azure subscription>
  appName: <Name of the Function App>

The snippet assumes that the build steps in your YAML file build and publishes the source as an artifact. The Azure Function App Deploy task will pull the artifact corresponding to the BuildId from the Source type specified, and then deploys the artifact to the Azure Function App Service.

YAML pipelines aren't available on TFS.

Deploy with Azure Function App

The simplest way to deploy to an Azure Function is to use the Azure Function App Deploy task.

To deploy to Azure Function, add the following snippet at the end of your azure-pipelines.yml file:

trigger:
- main

variables:
  # Azure service connection established during pipeline creation
  azureSubscription: <Name of your Azure subscription>
  appName: <Name of the Function app>
  # Agent VM image name
  vmImageName: 'ubuntu-latest'

- task: AzureFunctionApp@1 # Add this at the end of your file
  inputs:
    azureSubscription: <Azure service connection>
    appType: functionAppLinux
    appName: $(appName)
    package: $(System.ArtifactsDirectory)/**/*.zip

The snippet assumes that the build steps in your YAML file produce the zip archive in the $(System.ArtifactsDirectory) folder on your agent.

YAML pipelines aren't available on TFS.

Deploy to a slot

You can configure the Azure Function App to have multiple slots. Slots allow you to safely deploy your app and test it before making it available to your customers.

The following YAML snippet shows how to deploy to a staging slot, and then swap to a production slot:

- task: AzureFunctionApp@1
  inputs:
    azureSubscription: <Azure service connection>
    appType: functionAppLinux
    appName: <Name of the Function app>
    package: $(System.ArtifactsDirectory)/**/*.zip
    deployToSlotOrASE: true
    resourceGroupName: <Name of the resource group>
    slotName: staging

- task: AzureAppServiceManage@0
  inputs:
    azureSubscription: <Azure service connection>
    WebAppName: <name of the Function app>
    ResourceGroupName: <name of resource group>
    SourceSlot: staging
    SwapWithProduction: true

YAML pipelines aren't available on TFS.