Build and publish a Python app
Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019
In this quickstart, you create a pipeline that builds and tests a Python app. You see how to use Azure Pipelines to build, test, and deploy Python apps and scripts as part of your continuous integration and continuous delivery (CI/CD) system.
Prerequisites
- A GitHub account where you can create a repository. Create a GitHub account for free.
- An Azure DevOps organization. Create one for free.
- An Azure DevOps project. Create one using the Azure DevOps Project Creation Wizard.
- The ability to run pipelines on Microsoft-hosted agents. You need to request the free grant of parallel jobs or purchase a parallel job.
Python is preinstalled on Microsoft-hosted agents for Linux, macOS, and Windows. You don't have to set up anything more to build Python projects. To see which Python versions are preinstalled, see Use a Microsoft-hosted agent.
- A GitHub account where you can create a repository. Create a GitHub account for free.
- An Azure DevOps organization. Create one for free.
- An Azure DevOps project. Create one using the Azure DevOps Project Creation Wizard.
- A self-hosted agent. To create one, see Self-hosted agents.
- Python installed on your self-hosted agent. To install Python on your agent, see UsePythonVersion.
Fork the sample code
Fork the sample Python repository to your GitHub account.
- Go to the python-sample-vscode-flask-tutorial repository.
- Select Fork in the upper-right corner of the page.
- Select your GitHub account. By default, the fork is named the same as the parent repository, but you can name it something different.
Important
During the following procedures, you might be prompted to create a GitHub service connection or redirected to GitHub to sign in, install Azure Pipelines, or authorize Azure Pipelines. Follow the onscreen instructions to complete the process. For more information, see Access to GitHub repositories.
Create your pipeline
- In your Azure DevOps project, select Pipelines > Create Pipeline, and then select GitHub as the location of your source code.
- On the Select a repository screen, select your forked sample repository.
- On the Configure your pipeline screen, select Starter pipeline.
Customize your pipeline
On the Review your pipeline YAML screen, replace the contents of the generated azure-pipelines.yml file with the following code. The code:
- Installs required Python versions and dependencies.
- Packages build artifacts to a ZIP archive.
- Publishes the archive to your pipeline.
- 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 azure-pipelines.yml to match your project configuration.
- If you have a different agent pool, change the pool
name
parameter. - If necessary, change the Python version to 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'
Run your pipeline
Select Save and run, and then select Save and run again.
The Summary tab shows the status of your pipeline run.
To view your build artifact, select the published link in the Summary tab.
The Artifacts page shows the published build artifacts.
The Artifacts page shows the published build artifacts.
To view the test results, select the Tests tab.
Select Run.
The build number is displayed at the top of the page. Select the build number to see the details of the build.
The Summary tab shows the status of your pipeline run.
To download your build artifact, select the drop link from the Build artifacts published section.
To view the test results, select the Tests tab.
Clean up
When you finish this quickstart, you can delete the Azure DevOps project you created.
- In your project, select the Project settings gear icon in the lower left corner of the page.
- At the bottom of the Project overview page, select Delete.
- Enter the project name and select Delete.
Congratulations, you successfully created and ran a pipeline that built and tested a Python app. Now you can use Azure Pipelines to build, test, and deploy Python apps and scripts as part of your continuous integration and continuous delivery (CI/CD) system.