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
- A GitHub account where you can create a repository. Create one for free.
- An Azure DevOps organization. Create one for free.
- An Azure DevOps project. Create one using the Azure DevOps Project Creation Wizard.
- An ability to run pipelines on Microsoft-hosted agents. You can either purchase a parallel job or you can request a free tier.
- A GitHub account where you can create a repository. Create one for free.
- An Azure DevOps Server.
- An Azure DevOps project. Create one using the Azure DevOps Server Project Creation Wizard.
- A self-hosted agent. To create one, see Self-hosted agents.
- Python versions installed on your self-hosted agent. To learn how to install Python on your agent, see UsePythonVersion.
1. 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 the GitHub account where you want to fork the repository, and enter a name for your forked repository.
2. Create your pipeline
Go to Azure Pipelines and select Start free. If prompted, sign in to your DevOps account.
Go to your project and select Pipelines > Create 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 forked sample repository.
On the Configure your pipeline tab, select Starter pipeline.
In a browser, go to your DevOps Server collection.
Go to your project and select Pipelines > Create a new pipeline.
Select GitHub Enterprise Server 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 forked sample repository.
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.
- 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.
- If you have a different agent pool, change the pool
name
parameter. - 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.
- Select Save and run.
- In the Save and run dialog, select Save and run.
- From the Summary tab, you can see the status of your pipeline run.
- Select Run.
- The build number is displayed at the top of the page. Select the build number to see the details of the build.
To view your build artifact. select published link in the Summary tab.
The Artifacts page shows the published build artifacts.
To view the test results, select the Tests tab.
The Summary tab shows the status of your pipeline run.
To view your build artifact, select the published link in the Summary tab.
To view the test results, select the Tests tab.
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're done with this quickstart, you can delete the project you created in Azure DevOps.
- 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 completed this quickstart!
Next steps
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for