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.
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).
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:
- 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'
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.
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:
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'
5. Enable continuous deployment triggers if you want releases to be created automatically when a new build is available.
6. Save the release pipeline.
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.
Key Concepts
- CI/CD: Continuous Integration and Continuous Deployment automates the software delivery process.
- Azure Pipelines: A cloud service that you can use to automate building, testing, and deploying to any cloud or on-premises.
- YAML Pipelines: Define your pipelines as code, enabling version control and consistency.
- Artifacts: The output of a build pipeline (e.g., a zip file containing your function code).
- Service Connection: Securely connects Azure DevOps to your Azure subscription.
- Deployment Slots: Allow you to manage deployments with zero downtime by staging your changes before swapping them into production.
Next Steps
- Explore different deployment methods like "Run From Package".
- Implement automated testing stages within your pipelines.
- Configure deployment slots for staging and production environments.
- Add security scanning and quality gates to your pipeline.