Deploy Azure Functions with Azure DevOps Pipelines

Automate the deployment of your Azure Functions to the cloud using Azure DevOps Pipelines. This tutorial guides you through setting up a CI/CD pipeline to build, test, and deploy your serverless functions.

1

Prerequisites

  • An Azure subscription.
  • An Azure DevOps organization and project.
  • A sample Azure Functions project (e.g., a Node.js, C#, or Python function).
  • Source code for your Azure Function hosted in a Git repository (e.g., Azure Repos, GitHub).
2

Create a Build Pipeline

This pipeline will build your function app, run tests, and publish the build artifacts.

1. Navigate to your Azure DevOps project and select "Pipelines" -> "Pipelines".

2. Click "New pipeline".

3. Select your code repository (e.g., Azure Repos Git, GitHub).

4. Choose "Starter pipeline" or select a template if available for your language (e.g., Azure Functions - Node.js).

Here's a sample YAML for a Node.js Azure Functions build pipeline:

trigger:
- main

pool:
vmImage: 'ubuntu-latest'

steps:
- task: NodeTool@0
inputs:
versionSpec: '18.x'
displayName: 'Install Node.js'

- script: |
npm install
npm run build --if-present
npm run test --if-present
displayName: 'npm install, build and test'

- task: ArchiveFiles@2
inputs:
rootFolderOrFile: '$(System.DefaultWorkingDirectory)'
includeRootFolder: false
archiveType: 'zip'
archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
displayName: 'Archive build output'

- publish: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
artifact: drop
displayName: 'Publish Artifact: drop'
Note: Adjust the script section based on your project's build and test commands. For other languages, use appropriate tasks like UseDotNet@2 for .NET or UsePythonVersion@0 for Python.

5. Save and run the pipeline.

3

Create a Release Pipeline

This pipeline will take the build artifact and deploy it to your Azure Function App.

1. Navigate to "Pipelines" -> "Releases" and click "New pipeline".

2. Select the "Azure Functions deployment" template.

3. Add an artifact and link it to the build pipeline created in the previous step. Select the appropriate artifact (e.g., drop).

4. Configure the "Stage":

  • Azure subscription: Select your Azure subscription.
  • Deployment Method: Choose "Zip Deploy" or "Run From Package" for Azure Functions.
  • Azure Function App name: Select or enter the name of your target Azure Function App.
  • Runtime stack: Choose the correct runtime (e.g., Node, .NET, Python).

Here's a sample task configuration for deploying an Azure Function:

- deployment: DeployAzureFunction
displayName: Deploy Azure Function App
environment: 'Production'
strategy:
runOnce:
deploy:
steps:
- task: AzureFunctionApp@1
inputs:
azureSubscription: ''
appType: 'functionAppLinux'
appName: ''
package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'
runtimeStack: 'NODE|18'
deploymentMethod: 'zipDeploy'
# Optional: Specify slot name if deploying to a specific slot
# deployToSlotOrASE: true
# slotName: 'staging'
Note: Ensure you have created an Azure service connection in your Azure DevOps project settings that points to your Azure subscription. Replace placeholders like '<your-azure-subscription-service-connection>' and '<your-function-app-name>'.

5. Enable continuous deployment triggers if you want releases to be created automatically when a new build is available.

6. Save the release pipeline.

4

Run and Verify

Manually create a release for the release pipeline to initiate the deployment.

Once the deployment is complete, navigate to your Azure Function App in the Azure portal to verify that the latest code has been deployed successfully. You can test your function endpoints.

Azure Function App in Azure Portal
Verify deployment in the Azure portal.

Key Concepts

Next Steps