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

  1. Sign in to Azure Pipelines. Your browser will go to https://dev.azure.com/my-organization-name and display your Azure DevOps dashboard.

  2. Go to your project and select Pipelines > Create a new pipeline.

  3. Select GitHub as the location of your source code.

  4. If you're redirected to GitHub to sign in, enter your GitHub credentials.

  5. When the list of repositories appears, select your Python sample repository.

  6. Azure Pipelines analyzes the code in your repository and recommends the Python package template for your pipeline. Select that template.

  7. 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.

  8. 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

  1. Edit the azure-pipelines.yml file in your repository and update the Python version references.
trigger:
- main

pool:
  vmImage: ubuntu-latest
strategy:
  matrix:
    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'
  1. 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'
  1. Create a pipeline (if you don't know how, see Create your first pipeline), and for the template select YAML.

  2. Set the Agent pool and YAML file path for your pipeline.

  3. 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.

  4. 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.

Screenshot of complete Python jobs.

Next steps

Congratulations, you've successfully completed this quickstart! To run Python scripts or run specific versions of Python, see Configure Python.