Deploying applications to the cloud has never been easier, thanks to platforms like Azure and tools like Azure DevOps. In this guide, I’ll walk you through the process of setting up a pipeline to deploy a Python application to Azure Functions. This process can be used to automate your deployments, ensuring that your app is always up-to-date.
What is Azure Functions?
Azure Functions is a serverless compute service that allows you to run event-driven code without worrying about the infrastructure. It’s a great way to execute code in response to triggers, such as HTTP requests or timers.
What is Azure DevOps?
Azure DevOps is a suite of development tools that support the complete software development lifecycle. It includes repositories for code, pipelines for CI/CD, boards for tracking work, and more. We’ll use Azure DevOps Pipelines to automate our deployment process.
Prerequisites
Before you start, make sure you have the following:
- An Azure account
- An Azure DevOps account
- A Python application ready to deploy
- Basic knowledge of Git and command-line operations
Setting Up the Pipeline
The pipeline consists of two main stages: Build and Deploy. Here’s the YAML configuration we’ll use:
trigger:
branches:
include:
- main
variables:
azureResourceConnection: 'azure-service-connection'
azureFunctionAppName: 'my-python-app'
buildAgentImage: 'ubuntu-latest'
pythonEnvDirectory: 'python_env'
stages:
- stage: Build
displayName: Build stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(buildAgentImage)
steps:
- checkout: self
fetchDepth: 1
- task: Bash@3
displayName: Build extensions
inputs:
targetType: inline
script: >
if [ -f extensions.csproj ]; then
dotnet build extensions.csproj --output ./bin
fi
- task: UsePythonVersion@0
displayName: Use Python 3.8
inputs:
versionSpec: 3.8
- task: Bash@3
displayName: Install Application Dependencies
inputs:
targetType: inline
script: >
python3.8 -m venv $(pythonEnvDirectory)
source $(pythonEnvDirectory)/bin/activate
pip install setuptools
pip install -r requirements.txt
- task: ArchiveFiles@2
displayName: 'Archive files'
inputs:
rootFolderOrFile: $(System.DefaultWorkingDirectory)
includeRootFolder: false
- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact: drop'
- stage: Deploy
displayName: Deploy stage
dependsOn: Build
condition: succeeded()
jobs:
- deployment: Deploy
displayName: Deploy
environment: 'development'
pool:
vmImage: $(buildAgentImage)
strategy:
runOnce:
deploy:
steps:
- task: AzureFunctionApp@1
displayName: 'Azure functions app deploy'
inputs:
azureSubscription: '$(azureResourceConnection)'
appType: functionAppLinux
appName: $(azureFunctionAppName)
package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'
Key Components
- Trigger: This sets the pipeline to run on changes to the
main
branch. - Variables: Store important details like the Azure resource connection and function app name.
azureResourceConnection
: The Azure Resource Manager service connection name.azureFunctionAppName
: The name of your Azure Function app.buildAgentImage
: The image used for the build agent (e.g., Ubuntu).pythonEnvDirectory
: The directory name for the Python virtual environment.
- Build Stage:
- Checkout Code: Fetches the code from the repository.
- Build Extensions: Optional step to build .NET extensions if they exist.
- Use Python Version: Specifies the Python version (3.8 in this case).
- Install Dependencies: Sets up a virtual environment and installs required packages.
- Archive Files: Prepares the app files for deployment.
- Publish Artifact: Publishes the build artifact for the next stage.
- Deploy Stage:
- Deploy to Azure: Deploys the app to the specified Azure Function.
Deploying the Application
Once you’ve set up the pipeline, every time you push changes to the main
branch, the pipeline will automatically build and deploy your Python application to Azure Functions. This automation saves time and reduces the chance of errors during manual deployments.
Conclusion
By following these steps, you can set up a robust CI/CD pipeline for deploying Python applications to Azure Functions. This process ensures your app is always running the latest code, and it’s an excellent practice for maintaining high-quality software.
Feel free to customize the pipeline to fit your specific needs. Happy coding!