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.
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.
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:
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
Sign in to your Azure DevOps organization and navigate to your project.
In your project, navigate to the Pipelines page. Then select New pipeline.
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.
When the list of repositories appears, select your sample app repository.
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.
Select Save and run, then select Commit directly to the main branch, and then choose Save and run again.
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.
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.
You can use the following sample to create a YAML file to build a JavaScript app:
YAML
pool: vmImage:ubuntu-latest# Use 'windows-latest' if you have Windows native +Node modulessteps:- bash:|
if [ -f extensions.csproj ]
then
dotnet build extensions.csproj --output ./bin
fi
npm install
npm run build --if-present
npm prune --production
- task:ArchiveFiles@2 displayName:"Archive files" inputs: rootFolderOrFile:"$(System.DefaultWorkingDirectory)" includeRootFolder:false archiveFile:"$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip"- task:PublishBuildArtifacts@1 inputs: PathtoPublish:'$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip' artifactName:'drop'
Use one of the following samples to create a YAML file to build an app for a specific Python version. Python is only supported for function apps running on Linux.
YAML
pool: vmImage:ubuntu-lateststeps:- task:UsePythonVersion@0 displayName:"Set Python version to 3.9" inputs: versionSpec:'3.9' architecture:'x64'- bash:|
if [ -f extensions.csproj ]
then
dotnet build extensions.csproj --output ./bin
fi
pip install --target="./.python_packages/lib/site-packages" -r ./requirements.txt
- task:ArchiveFiles@2 displayName:"Archive files" inputs: rootFolderOrFile:"$(System.DefaultWorkingDirectory)" includeRootFolder:false archiveFile:"$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip"- task:PublishBuildArtifacts@1 inputs: PathtoPublish:'$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip' artifactName:'drop'
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. Deploying to a Flex Consumption app is not supported with @v1 of the AzureFunctionApp task.
YAML
trigger:-mainvariables:# Azure service connection established during pipeline creation azureSubscription:<NameofyourAzuresubscription> appName:<Nameofthefunctionapp># Agent VM image name vmImageName:'ubuntu-latest'- task:DownloadBuildArtifacts@1# Add this at the end of your file inputs: buildType:'current' downloadType:'single' artifactName:'drop' itemPattern:'**/*.zip' downloadPath:'$(System.ArtifactsDirectory)'- task:AzureFunctionApp@1 inputs: azureSubscription:<Azureserviceconnection> 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.
To deploy, add the following snippet at the end of your YAML file:
YAML
trigger:-mainvariables:# Container registry service connection established during pipeline creation dockerRegistryServiceConnection:<Dockerregistryserviceconnection> imageRepository:<Nameofyourimagerepository> containerRegistry:<NameoftheAzurecontainerregistry> 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.
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:
To create a build pipeline in Azure, use the az functionapp devops-pipeline createcommand. 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
Sign in to your Azure DevOps organization and navigate to your project.
In your project, navigate to the Pipelines page. Then choose the action to create a new pipeline.
Walk through the steps of the wizard by first selecting GitHub as the location of your source code.
You might be redirected to GitHub to sign in. If so, enter your GitHub credentials.
When the list of repositories appears, select your sample app repository.
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.
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.
You can use the following sample to create a YAML file to build a JavaScript app:
YAML
pool: vmImage:ubuntu-latest# Use 'windows-latest' if you have Windows native +Node modulessteps:- bash:|
if [ -f extensions.csproj ]
then
dotnet build extensions.csproj --output ./bin
fi
npm install
npm run build --if-present
npm prune --production
- task:ArchiveFiles@2 displayName:"Archive files" inputs: rootFolderOrFile:"$(System.DefaultWorkingDirectory)" includeRootFolder:false archiveFile:"$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip"- task:PublishBuildArtifacts@1 inputs: PathtoPublish:'$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip' artifactName:'drop'
Use one of the following samples to create a YAML file to build an app for a specific Python version. Python is only supported for function apps running on Linux.
YAML
pool: vmImage:ubuntu-lateststeps:- task:UsePythonVersion@0 displayName:"Set Python version to 3.9" inputs: versionSpec:'3.9' architecture:'x64'- bash:|
if [ -f extensions.csproj ]
then
dotnet build extensions.csproj --output ./bin
fi
pip install --target="./.python_packages/lib/site-packages" -r ./requirements.txt
- task:ArchiveFiles@2 displayName:"Archive files" inputs: rootFolderOrFile:"$(System.DefaultWorkingDirectory)" includeRootFolder:false archiveFile:"$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip"- task:PublishBuildArtifacts@1 inputs: PathtoPublish:'$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip' artifactName:'drop'
Check the generated archive to ensure that the deployed file has the right format.
To learn about potential issues with these pipeline tasks, see Functions not found after deployment.
You can use the following sample to create a YAML file to package a PowerShell app.
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. Deploying to a Flex Consumption app requires you to set both appType: functionAppLinux and isFlexConsumption: true.
trigger:-mainvariables:# Azure service connection established during pipeline creation azureSubscription:<SUBSCRIPTION_NAME> appName:<APP_NAME># Agent VM image name vmImageName:'windows-latest'- task:AzureFunctionApp@2# Add this at the end of your file inputs: azureSubscription:<AZURE_SERVICE_CONNECTION> appType:functionApp# this specifies a Windows-based function app 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>'#slotName: '<SLOT_NAME>'
YAML
trigger:-mainvariables:# Azure service connection established during pipeline creation azureSubscription:<SUBSCRIPTION_NAME> appName:<APP_NAME># 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# This specifies a Linux-based function app#isFlexConsumption: true # Uncomment this line if you are deploying to a Flex Consumption app 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>'#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.
If you opted to deploy to a deployment slot, you can add the following step to perform a slot swap. Deployment slots are not yet available for the Flex Consumption SKU.
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
To deploy, add the following snippet at the end of your YAML file:
YAML
trigger:-mainvariables:# Container registry service connection established during pipeline creation dockerRegistryServiceConnection:<DOCKER_REGISTRY_SERVICE_CONNECTION> imageRepository:<IMAGE_REPOSITORY_NAME> containerRegistry:<AZURE_CONTAINER_REGISTRY_NAME> 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:'<APP_NAME>' 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.
Create a pipeline with Azure CLI
To create a build pipeline in Azure, use the az functionapp devops-pipeline createcommand. 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.
Tham gia chuỗi buổi gặp gỡ để xây dựng các giải pháp AI có thể mở rộng dựa trên các trường hợp sử dụng trong thế giới thực với các nhà phát triển và chuyên gia đồng nghiệp.
Xây dựng các giải pháp toàn năng trong Microsoft Azure để tạo Azure Functions, triển khai và quản lý các ứng dụng web, phát triển các giải pháp sử dụng bộ nhớ Azure và hơn thế nữa.