Azure Pipelines with Python — by example

The Azure Pipeline Build

  • Build
  • Deploy
  • Install

Selecting an Operating System

- job: 'your_job_name' 
pool:
vmImage: 'Ubuntu-16.04'
- job: 'your_job_name' 
pool:
vmImage: 'vs2017-win2016'
  • macOS 10.13 (macOS-10.13)
  • Windows Server 1803 (win1803) - for running Windows containers
  • Visual Studio 2015 on Windows Server 2012R2 (vs2015-win2012r2)

Templating steps for re-use across jobs

jobs: 
- job: Build_Linux
pool:
vmImage: 'ubuntu-16.04'
steps:
- template: templates/steps.yml
- job: Build_Windows
pool:
vmImage: 'vs2017-win2016'
steps:
- template: templates/steps.yml

Test Output

Testing a Django application

1. Choose the Python version.

2. Install Django

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

3. Test your Django application

[pytest]
python_files = tests.py test_*.py *_tests.py
DJANGO_SETTINGS_MODULE = your_module_name.settings
- script: |     
pip install pytest-django
cd django-basic/azuredemo
pytest --junitxml=../../reports/django-basic.xml
displayName: 'Run tests'

4. Publish the test output

A multi-environment Django test

- script: |   
python -m pip install --upgrade pip
pip install django==$(django.version)
displayName: 'Install dependencies'

A basic Flask test

A Flask test with coverage

- script: |   
pip install pytest coverage
cd flask-basic
coverage run -m pytest --junitxml=../reports/flask-basic-coverage.xml
coverage report
displayName: 'Run tests and coverage'

Testing a basic Python library

- script: |   
python -m pip install --upgrade pip
pip install -r library-basic/requirements.txt
cd library-basic
pip install -e .
displayName: 'Install dependencies'
- script: |   
cd library-basic
pytest --junitxml=../reports/library-basic.xml
displayName: 'Run tests'
- script: |    
cd library-basic
python setup.py sdist bdist_wheel
displayName: 'Run tests'
- task: CopyFiles@2
inputs:
contents: library-basic/dist/**
targetFolder: $(Build.ArtifactStagingDirectory)
- task: PublishBuildArtifacts@1
inputs:
pathtoPublish: $(Build.ArtifactStagingDirectory)
artifactName: Library_Basic_Wheel

Testing a modern Python library with pyproject.toml

  • Run flit install instead of pip install -e .
  • Run flit build instead of python setup.py sdist bdist_wheel
  • Replace the version number with the build number
  • Each script step will start from the root path, even if the previous step changed directories.
  • Try to avoid using too much bash-scripting in your steps that is specific to Linux, this makes it easier to copy steps for Windows. In PowerShell, many of the commands you would expect in Linux exist, like cd ls mkdir rm .

--

--

--

Group Director of Talent at Dimension Data, father, Christian, Python Software Foundation Fellow, Apache Foundation Member.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Web development, management, and the tradeoff

Visual Regression Testing

Apache Nifi GetHTTP/InvokeHTTP SSL Issues

Refund Policy

Running Non-containerized Microservices

Fun with Recursion

Video Streaming Web Server

8 steps to keep your API sane

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Anthony Shaw

Anthony Shaw

Group Director of Talent at Dimension Data, father, Christian, Python Software Foundation Fellow, Apache Foundation Member.

More from Medium

Azure Active Directory Basics

Blob triggered function deployed with Azure DevOps

Build Azure DevOps Pipelines for Dynamics 365 CRM

Configure Azure Multi-Factor Authentication in Azure