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.

In this quickstart, you learn how to create a pipeline to build and test a Python app.

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.

Prerequisites

1. Fork the sample code

Fork the sample Python repository to your GitHub account.

  1. Go to the python-sample-vscode-flask-tutorial repository.
  2. Select Fork in the upper-right corner of the page.
  3. Select the GitHub account where you want to fork the repository, and enter a name for your forked repository.

2. Create your pipeline

  1. Go to Azure Pipelines and select Start free. If prompted, sign in to your DevOps account.

  2. Go to your project and select Pipelines > Create 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 forked sample repository.

  6. On the Configure your pipeline tab, select Starter pipeline.

  1. In a browser, go to your DevOps Server collection.

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

  3. Select GitHub Enterprise Server 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 forked sample repository.

  6. On the Configure your pipeline tab, select Starter pipeline.

3. Customize your pipeline

Replace the generated azure-pipelines.yml file content with the following code. This code installs the required Python version and the dependencies, packages the Python package to a zip file published to your pipeline, and runs tests.

trigger:
- main

pool:
  vmImage: ubuntu-latest

strategy:
  matrix:
    Python310:
      python.version: '3.10'
    Python311:
      python.version: '3.11'
    Python312:
      python.version: '3.12'

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'

  - task: ArchiveFiles@2
    displayName: 'Archive files'
    inputs:
      rootFolderOrFile: $(System.DefaultWorkingDirectory)
      includeRootFolder: false
      archiveType: zip
      archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId)-$(python.version).zip
      replaceExistingArchive: true

  - task: PublishBuildArtifacts@1
    inputs:
      PathtoPublish: '$(Build.ArtifactStagingDirectory)'
      ArtifactName: 'drop'
      publishLocation: 'Container'

  - script: |
      pip install pytest pytest-azurepipelines
      pytest
    displayName: 'pytest'

Customize the azure-pipelines.yml to match your project configuration.

  1. Replace the generated YAML with the following code. This code installs the required Python version and the dependencies, packages the Python package to a zip file published to your pipeline, and runs tests.
  2. If you have a different agent pool, change the pool name parameter.
  3. Change the Python version to match a version installed on your self-hosted agent.
  trigger:
  - main

  pool: 
    name: '<your-pool-name or default>'

  steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: '3.12'
    displayName: 'Use Python 3.12'  

  - script: |
      python -m pip install --upgrade pip
      pip install -r requirements.txt
    displayName: 'Install dependencies'


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

  - task: PublishBuildArtifacts@1
    inputs:
      PathtoPublish: '$(Build.ArtifactStagingDirectory)'
      ArtifactName: 'drop'
      publishLocation: 'Container'

  - script: |
      pip install pytest pytest-azurepipelines
      pytest
    displayName: 'pytest'

4. Run your pipeline

Save and run your pipeline.

  1. Select Save and run.
  2. In the Save and run dialog, select Save and run.
  3. From the Summary tab, you can see the status of your pipeline run.
  1. Select Run.
  2. The build number is displayed at the top of the page. Select the build number to see the details of the build.

Screenshot of pipeline build link.

Screenshot of completed Python jobs.

To view your build artifact. select published link in the Summary tab.

Screenshot of published build artifacts link.

The Artifacts page shows the published build artifacts.

Screenshot of published build artifacts.

To view the test results, select the Tests tab.

Screenshot of pipeline test results.

The Summary tab shows the status of your pipeline run.

Screenshot of completed Python job.

To view your build artifact, select the published link in the Summary tab.

Screenshot of published build artifacts link.

To view the test results, select the Tests tab.

Screenshot of pipeline test results.

The Summary tab shows the status of your pipeline run.

Screenshot of completed Python job.

To download your build artifact, select the drop link from the Build artifacts published section.

To view the test results, select the Tests tab.

Screenshot of pipeline test results.

Clean up

When you're done with this quickstart, you can delete the project you created in Azure DevOps.

  1. Select the Project settings gear icon in the lower left corner of the page.
  2. At the bottom of the Project overview page, select Delete.
  3. Enter the project name and select Delete.

Congratulations, you successfully completed this quickstart!

Next steps