title | description | ms.topic | ms.assetid | ms.date | monikerRange | ms.custom |
---|---|---|---|---|---|---|
Quickstart - Use Azure Pipelines to build and publish a Python app |
Automatically build and test Python apps with Azure Pipelines |
quickstart |
141149f8-d1a9-49fa-be98-ee9a825a951a |
12/20/2022 |
>=azure-devops-2019 |
devx-track-python, freshness-fy22q2 |
[!INCLUDE version-gt-eq-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.
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.
::: moniker range="azure-devops-2019"
Import this repo into your Git repo in Azure DevOps Server 2019:
::: moniker-end
::: moniker range=">=azure-devops-2020"
For the following sample Python Flask tutorial:
::: moniker-end
https://github.com/Microsoft/python-sample-vscode-flask-tutorial
::: moniker range=">=azure-devops-2020"
-
Sign in to Azure Pipelines. Your browser will go to
https://dev.azure.com/my-organization-name
and display your Azure DevOps dashboard. -
Go to your project and select Pipelines > Create a new 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 Node.js sample repository.
-
Azure Pipelines analyzes the code in your repository and recommends the
Python package
template for your pipeline. Select that template. -
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.
-
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.
- Edit the
azure-pipelines.yml
file in your repository and update the Python version references.
trigger:
- main
pool:
vmImage: ubuntu-latest
strategy:
matrix:
Python37:
python.version: '3.7'
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'
::: moniker-end
::: moniker range="azure-devops-2019"
- 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'
-
Create a pipeline (if you don't know how, see Create your first pipeline), and for the template select YAML.
-
Set the Agent pool and YAML file path for your pipeline.
-
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.
-
When you're ready to make changes to your pipeline, Edit it.
::: moniker-end
Save and run your pipeline. After your pipeline runs, verify that the jobs ran successfully.
:::image type="content" source="media/python-successful-jobs.png" alt-text="Screenshot of complete Python jobs.":::
Congratulations, you've successfully completed this quickstart! To run Python scripts or run specific versions of Python, see Configure Python.
[!div class="nextstepaction"] Configure Python