Continuously update function app code using Azure Pipelines (2024)

  • Article

Use Azure Pipelines to automatically deploy to Azure Functions. Azure Pipelines lets you build, test, and deploy with continuous integration (CI) and continuous delivery (CD) using Azure DevOps.

YAML pipelines are defined using a YAML file in your repository. A step is the smallest building block of a pipeline and can be a script or task (prepackaged script). Learn about the key concepts and components that make up a pipeline.

You'll use the AzureFunctionApp task to deploy to Azure Functions. There are now two versions of the AzureFunctionApp task (AzureFunctionApp@1, AzureFunctionApp@2). AzureFunctionApp@2 includes enhanced validation support that makes pipelines less likely to fail because of errors.

Choose your task version at the top of the article. YAML pipelines aren't available for Azure DevOps 2019 and earlier.

Prerequisites

  • An Azure DevOps organization. If you don't have one, you can create one for free. If your team already has one, then make sure you're an administrator of the Azure DevOps project that you want to use.

  • An ability to run pipelines on Microsoft-hosted agents. You can either purchase a parallel job or you can request a free tier.

  • If you plan to use GitHub instead of Azure Repos, you also need a GitHub repository. If you don't have a GitHub account, you can create one for free.

  • An existing function app in Azure that has its source code in a supported repository. If you don't yet have an Azure Functions code project, you can create one by completing the following language-specific article:

    • C#
    • JavaScript
    • Python
    • PowerShell

    Quickstart: Create a C# function in Azure using Visual Studio Code

    Remember to upload the local code project to your GitHub or Azure Repos repository after you publish it to your function app.

Build your app

  1. Sign in to your Azure DevOps organization and navigate to your project.
  2. In your project, navigate to the Pipelines page. Then select New pipeline.
  3. Select one of these options for Where is your code?:
    • GitHub: You might be redirected to GitHub to sign in. If so, enter your GitHub credentials. When this connection is your first GitHub connection, the wizard also walks you through the process of connecting DevOps to your GitHub accounts.
    • Azure Repos Git: You're immediately able to choose a repository in your current DevOps project.
  4. When the list of repositories appears, select your sample app repository.
  5. Azure Pipelines analyzes your repository and in Configure your pipeline provides a list of potential templates. Choose the appropriate function app template for your language. If you don't see the correct template select Show more.
  6. Select Save and run, then select Commit directly to the main branch, and then choose Save and run again.
  7. A new run is started. Wait for the run to finish.

Example YAML build pipelines

The following language-specific pipelines can be used for building apps.

  • C#
  • JavaScript
  • Python
  • PowerShell

You can use the following sample to create a YAML file to build a .NET app.

If you see errors when building your app, verify that the version of .NET that you use matches your Azure Functions version. For more information, see Azure Functions runtime versions overview.

pool: vmImage: 'windows-latest'steps:- script: | dotnet restore dotnet build --configuration Release- task: DotNetCoreCLI@2 inputs: command: publish arguments: '--configuration Release --output publish_output' projects: '*.csproj' publishWebProjects: false modifyOutputPath: false zipAfterPublish: false- task: ArchiveFiles@2 displayName: "Archive files" inputs: rootFolderOrFile: "$(System.DefaultWorkingDirectory)/publish_output" includeRootFolder: false archiveFile: "$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip"- task: PublishBuildArtifacts@1 inputs: PathtoPublish: '$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip' artifactName: 'drop'

Deploy your app

You'll deploy with the Azure Function App Deploy task. This task requires an Azure service connection as an input. An Azure service connection stores the credentials to connect from Azure Pipelines to Azure.

To deploy to Azure Functions, add the following snippet at the end of your azure-pipelines.yml file. The default appType is Windows. You can specify Linux by setting the appType to functionAppLinux.

trigger:- mainvariables: # 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 # default is functionApp appName: $(appName) package: $(System.ArtifactsDirectory)/**/*.zip #Uncomment the next lines to deploy to a deployment slot #Note that deployment slots is not supported for Linux Dynamic SKU #deployToSlotOrASE: true #resourceGroupName: '<Resource Group Name>' #slotName: '<Slot name>'

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

Deploy a container

You can automatically deploy your code as a containerized function app after every successful build. To learn more about containers, see Working with containers and Azure Functions.

The simplest way to deploy to a container is to use the Azure Function App on Container Deploy task.

To deploy, add the following snippet at the end of your YAML file:

trigger:- mainvariables: # Container registry service connection established during pipeline creation dockerRegistryServiceConnection: <Docker registry service connection> imageRepository: <Name of your image repository> containerRegistry: <Name of the Azure container registry> dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile' tag: '$(Build.BuildId)' # Agent VM image name vmImageName: 'ubuntu-latest'- task: AzureFunctionAppContainer@1 # Add this at the end of your file inputs: azureSubscription: '<Azure service connection>' appName: '<Name of the function app>' imageName: $(containerRegistry)/$(imageRepository):$(tag)

The snippet pushes the Docker image to your Azure Container Registry. The Azure Function App on Container Deploy task pulls the appropriate Docker image corresponding to the BuildId from the repository specified, and then deploys the image.

For a complete end-to-end pipeline example, including building the container and publishing to the container registry, see this Azure Pipelines container deployment example.

Deploy to a slot

You can configure your 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

Create a pipeline with Azure CLI

To create a build pipeline in Azure, use the az functionapp devops-pipeline create command. The build pipeline is created to build and release any code changes that are made in your repo. The command generates a new YAML file that defines the build and release pipeline and then commits it to your repo. The prerequisites for this command depend on the location of your code.

  • If your code is in GitHub:

    • You must have write permissions for your subscription.

    • You must be the project administrator in Azure DevOps.

    • You must have permissions to create a GitHub personal access token (PAT) that has sufficient permissions. For more information, see GitHub PAT permission requirements.

    • You must have permissions to commit to the main branch in your GitHub repository so you can commit the autogenerated YAML file.

  • If your code is in Azure Repos:

    • You must have write permissions for your subscription.

    • You must be the project administrator in Azure DevOps.

Build your app

  1. Sign in to your Azure DevOps organization and navigate to your project.
  2. In your project, navigate to the Pipelines page. Then choose the action to create a new pipeline.
  3. Walk through the steps of the wizard by first selecting GitHub as the location of your source code.
  4. You might be redirected to GitHub to sign in. If so, enter your GitHub credentials.
  5. When the list of repositories appears, select your sample app repository.
  6. Azure Pipelines will analyze your repository and recommend a template. Select Save and run, then select Commit directly to the main branch, and then choose Save and run again.
  7. A new run is started. Wait for the run to finish.

Example YAML build pipelines

The following language-specific pipelines can be used for building apps.

  • C#
  • JavaScript
  • Python
  • PowerShell

You can use the following sample to create a YAML file to build a .NET app:

pool: vmImage: 'windows-latest'steps:- script: | dotnet restore dotnet build --configuration Release- task: DotNetCoreCLI@2 inputs: command: publish arguments: '--configuration Release --output publish_output' projects: '*.csproj' publishWebProjects: false modifyOutputPath: false zipAfterPublish: false- task: ArchiveFiles@2 displayName: "Archive files" inputs: rootFolderOrFile: "$(System.DefaultWorkingDirectory)/publish_output" includeRootFolder: false archiveFile: "$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip"- task: PublishBuildArtifacts@1 inputs: PathtoPublish: '$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip' artifactName: 'drop'

Deploy your app

You'll deploy with the Azure Function App Deploy v2 task. This task requires an Azure service connection as an input. An Azure service connection stores the credentials to connect from Azure Pipelines to Azure. You should create a connection that uses workload identity federation.

The v2 version of the task includes support for newer applications stacks for .NET, Python, and Node. The task includes networking predeployment checks. When there are predeployment issues, deployment stops.

To deploy to Azure Functions, add the following snippet at the end of your azure-pipelines.yml file. The default appType is Windows. You can specify Linux by setting the appType to functionAppLinux.

trigger:- mainvariables: # 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@2 # Add this at the end of your file inputs: azureSubscription: <Azure service connection> appType: functionAppLinux # default is functionApp appName: $(appName) package: $(System.ArtifactsDirectory)/**/*.zip deploymentMethod: 'auto' # 'auto' | 'zipDeploy' | 'runFromPackage'. Required. Deployment method. Default: auto. #Uncomment the next lines to deploy to a deployment slot #Note that deployment slots is not supported for Linux Dynamic SKU #deployToSlotOrASE: true #resourceGroupName: '<Resource Group Name>' #slotName: '<Slot name>'

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

Deploy a container

You can automatically deploy your code to Azure Functions as a custom container after every successful build. To learn more about containers, see Working with containers and Azure Functions .

Deploy with the Azure Function App for Container task

The simplest way to deploy to a container is to use the Azure Function App on Container Deploy task.

To deploy, add the following snippet at the end of your YAML file:

trigger:- mainvariables: # Container registry service connection established during pipeline creation dockerRegistryServiceConnection: <Docker registry service connection> imageRepository: <Name of your image repository> containerRegistry: <Name of the Azure container registry> dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile' tag: '$(Build.BuildId)' # Agent VM image name vmImageName: 'ubuntu-latest'- task: AzureFunctionAppContainer@1 # Add this at the end of your file inputs: azureSubscription: '<Azure service connection>' appName: '<Name of the function app>' imageName: $(containerRegistry)/$(imageRepository):$(tag)

The snippet pushes the Docker image to your Azure Container Registry. The Azure Function App on Container Deploy task pulls the appropriate Docker image corresponding to the BuildId from the repository specified, and then deploys the image.

Deploy to a slot

You can configure your 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@2 inputs: azureSubscription: <Azure service connection> appType: functionAppLinux appName: <Name of the Function app> package: $(System.ArtifactsDirectory)/**/*.zip deploymentMethod: 'auto' 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

Create a pipeline with Azure CLI

To create a build pipeline in Azure, use the az functionapp devops-pipeline create command. The build pipeline is created to build and release any code changes that are made in your repo. The command generates a new YAML file that defines the build and release pipeline and then commits it to your repo. The prerequisites for this command depend on the location of your code.

  • If your code is in GitHub:

    • You must have write permissions for your subscription.

    • You must be the project administrator in Azure DevOps.

    • You must have permissions to create a GitHub personal access token (PAT) that has sufficient permissions. For more information, see GitHub PAT permission requirements.

    • You must have permissions to commit to the main branch in your GitHub repository so you can commit the autogenerated YAML file.

  • If your code is in Azure Repos:

    • You must have write permissions for your subscription.

    • You must be the project administrator in Azure DevOps.

Next steps

  • Review the Azure Functions overview.
  • Review the Azure DevOps overview.
Continuously update function app code using Azure Pipelines (2024)
Top Articles
How much TikTok pays for views, according to creators
Earnest Student Loans Review for 2024 | LendEDU
5 Bijwerkingen van zwemmen in een zwembad met te veel chloor - Bereik uw gezondheidsdoelen met praktische hulpmiddelen voor eten en fitness, deskundige bronnen en een betrokken gemeenschap.
Craigslist Dog Sitter
ds. J.C. van Trigt - Lukas 23:42-43 - Preekaantekeningen
Cvs Devoted Catalog
True Statement About A Crown Dependency Crossword
Florida (FL) Powerball - Winning Numbers & Results
Used Wood Cook Stoves For Sale Craigslist
Nonuclub
Zürich Stadion Letzigrund detailed interactive seating plan with seat & row numbers | Sitzplan Saalplan with Sitzplatz & Reihen Nummerierung
Nebraska Furniture Tables
Classic Lotto Payout Calculator
Stihl Km 131 R Parts Diagram
Viha Email Login
Grayling Purnell Net Worth
Epguides Strange New Worlds
Skip The Games Fairbanks Alaska
Craigslist Pearl Ms
Joan M. Wallace - Baker Swan Funeral Home
Yosemite Sam Hood Ornament
Play It Again Sports Norman Photos
Avatar: The Way Of Water Showtimes Near Maya Pittsburg Cinemas
Craigslist Hunting Land For Lease In Ga
800-695-2780
UCLA Study Abroad | International Education Office
Ticket To Paradise Showtimes Near Cinemark Mall Del Norte
Wonder Film Wiki
Is Henry Dicarlo Leaving Ktla
How do you get noble pursuit?
Askhistorians Book List
Ringcentral Background
Desales Field Hockey Schedule
Moonrise Time Tonight Near Me
Smayperu
new haven free stuff - craigslist
Craigslist Lakeside Az
Skip The Games Grand Rapids Mi
Who Is Responsible for Writing Obituaries After Death? | Pottstown Funeral Home & Crematory
Foxxequeen
Pulaski County Ky Mugshots Busted Newspaper
Pink Runtz Strain, The Ultimate Guide
How Big Is 776 000 Acres On A Map
Bekkenpijn: oorzaken en symptomen van pijn in het bekken
Noga Funeral Home Obituaries
El Patron Menu Bardstown Ky
Goosetown Communications Guilford Ct
Houston Primary Care Byron Ga
Kenmore Coldspot Model 106 Light Bulb Replacement
Noelleleyva Leaks
Vrca File Converter
Latest Posts
Article information

Author: Van Hayes

Last Updated:

Views: 6104

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.