Build Python apps
Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019
You can use Azure Pipelines to build, test, and deploy Python apps and scripts as part of your CI/CD system. This article focuses on creating a basic pipeline. This quickstart walks through how to create a simple Flask app with three pages that use a common base template and deploy it with Azure DevOps.
You don't have to set up anything for Azure Pipelines to build Python projects. Python is preinstalled on Microsoft-hosted build agents for Linux, macOS, or Windows. To see which Python versions are preinstalled, see Use a Microsoft-hosted agent.
To learn about configuring Python in pipelines, see Customize Python.
If you want a more complex example, see Use CI/CD to deploy a Python web app to Azure App Service on Linux.
Prerequisites
You must have the following items in Azure DevOps:
- A GitHub account where you can create a repository. Create one for free.
- An Azure DevOps organization and project. Create one for free.
- An ability to run pipelines on Microsoft-hosted agents. You can either purchase a parallel job or you can request a free tier.
1 - Fork the sample code
Import this repo into your Git repo in Azure DevOps Server 2019:
For the following sample Python Flask tutorial:
https://github.com/Microsoft/python-sample-vscode-flask-tutorial
2 - Create your pipeline
Sign in to Azure Pipelines. Your browser will go to
https://dev.azure.com/my-organization-name
and display your Azure DevOps dashboard.Go to your project and select Pipelines > Create a new pipeline.
Select GitHub as the location of your source code.
If you're redirected to GitHub to sign in, enter your GitHub credentials.
When the list of repositories appears, select your Node.js sample repository.
Azure Pipelines analyzes the code in your repository and recommends the
Python package
template for your pipeline. Select that template.Azure Pipelines generates a YAML file for your pipeline. Select Save and run > Commit directly to the main branch, and then choose Save and run again.
A new run starts. Wait for the run to finish.
When you're done, you have a YAML file azure-pipelines.yml in your repository that's ready for you to customize.
Customize your pipeline
- Edit the
azure-pipelines.yml
file in your repository and update the Python version references.
trigger:
- main
pool:
vmImage: ubuntu-latest
strategy:
matrix:
Python37:
python.version: '3.7'
Python38:
python.version: '3.8'
Python39:
python.version: '3.9'
Python310:
python.version: '3.10'
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '$(python.version)'
displayName: 'Use Python $(python.version)'
- script: |
python -m pip install --upgrade pip
pip install -r requirements.txt
displayName: 'Install dependencies'
- script: |
pip install pytest pytest-azurepipelines
pytest
displayName: 'pytest'
- Add an
azure-pipelines.yml
file in your repository. Customize this snippet for your build.
trigger:
- main
pool: Default
steps:
- script: python -m pip install --upgrade pip
displayName: 'Install dependencies'
- script: pip install -r requirements.txt
displayName: 'Install requirements'
Create a pipeline (if you don't know how, see Create your first pipeline), and for the template select YAML.
Set the Agent pool and YAML file path for your pipeline.
Save the pipeline and queue a build. When the Build #nnnnnnnn.n has been queued message appears, select the number link to see your pipeline in action.
When you're ready to make changes to your pipeline, Edit it.
3 - Run your pipeline
Save and run your pipeline. After your pipeline runs, verify that the jobs ran successfully.
Next steps
Congratulations, you've successfully completed this quickstart! To run Python scripts or run specific versions of Python, see Configure Python.
Feedback
Submit and view feedback for