自定义适用于 Azure Pipelines 的 Python
可以使用 Azure Pipelines 生成 Python 应用,而无需设置自己的任何基础结构。 通常用于生成、测试和运行 Python 应用(如 pip)的工具将预安装在 Azure Pipelines 中的 Microsoft 托管代理上。
若要使用 Python 创建第一个管道,请参阅 Python 快速入门。
使用特定的 Python 版本
若要在管道中使用特定版本的 Python,请将“使用 Python 版本”任务添加到 azure-pipelines.yml。 此代码片段将管道设置为使用 Python 3.11:
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.11'
使用多个 Python 版本
若要运行具有多个 Python 版本的管道来实现特定目的(例如针对这些版本来测试包),请使用 Python 版本的 matrix
来定义 job
。 然后,设置 UsePythonVersion
任务以引用 matrix
变量。
jobs:
- job: 'Test'
pool:
vmImage: 'ubuntu-latest' # other options: 'macOS-latest', 'windows-latest'
strategy:
matrix:
Python38:
python.version: '3.8'
Python39:
python.version: '3.9'
Python310:
python.version: '3.10'
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '$(python.version)'
可以使用矩阵中的每个 Python 版本添加要运行的任务。
运行 Python 脚本
若要在存储库中运行 Python 脚本,请使用 script
元素并指定文件名。 例如:
- script: python src/example.py
还可以使用 Python 脚本任务运行内联 Python 脚本:
- task: PythonScript@0
inputs:
scriptSource: 'inline'
script: |
print('Hello world 1')
print('Hello world 2')
若要参数化脚本执行,请使用具有 arguments
值的 PythonScript
任务将参数传递到执行进程中。 可以使用 sys.argv
或更复杂的 argparse
库来分析参数。
- task: PythonScript@0
inputs:
scriptSource: inline
script: |
import sys
print ('Executing script file is:', str(sys.argv[0]))
print ('The arguments are:', str(sys.argv))
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--world", help="Provide the name of the world to greet.")
args = parser.parse_args()
print ('Hello ', args.world)
arguments: --world Venus
安装依赖项
可以使用脚本安装具有 pip
的特定 PyPI 包。 例如,此 YAML 会安装或升级 pip
以及 setuptools
和 wheel
包。
- script: python -m pip install --upgrade pip setuptools wheel
displayName: 'Install tools'
安装要求
更新 pip
和好友后,下一步通常是从 requirements.txt 安装依赖项:
- script: pip install -r requirements.txt
displayName: 'Install requirements'
运行测试
使用脚本在管道中安装和运行各种测试。
使用 flake8 运行 lint 测试
若要安装或升级 flake8
并使用它运行 lint 测试,请使用以下 YAML:
- script: |
python -m pip install flake8
flake8 .
displayName: 'Run lint tests'
使用 pytest 进行测试并使用 pytest-cov 收集覆盖率指标
使用此 YAML 安装 pytest
和 pytest-cov
、运行测试、以 JUnit 格式输出测试结果,以及以 Cobertura XML 格式输出代码覆盖率结果:
- script: |
pip install pytest pytest-azurepipelines
pip install pytest-cov
pytest --doctest-modules --junitxml=junit/test-results.xml --cov=. --cov-report=xml
displayName: 'pytest'
使用 Tox 运行测试
Azure Pipelines 可以运行并行 Tox 测试作业来拆分工作。 在开发计算机上,必须连续运行测试环境。 此示例使用 tox -e py
运行对于当前作业来说处于活动状态的 Python 版本。
- job:
pool:
vmImage: 'ubuntu-latest'
strategy:
matrix:
Python38:
python.version: '3.8'
Python39:
python.version: '3.9'
Python310:
python.version: '3.10'
steps:
- task: UsePythonVersion@0
displayName: 'Use Python $(python.version)'
inputs:
versionSpec: '$(python.version)'
- script: pip install tox
displayName: 'Install Tox'
- script: tox -e py
displayName: 'Run Tox'
发布测试结果
添加“发布测试结果”任务,以将 JUnit 或 xUnit 测试结果发布到服务器:
- task: PublishTestResults@2
condition: succeededOrFailed()
inputs:
testResultsFiles: '**/test-*.xml'
testRunTitle: 'Publish test results for Python $(python.version)'
发布代码覆盖率结果
添加“发布代码覆盖率结果”任务,以将代码覆盖率结果发布到服务器。 可以在生成摘要中看到覆盖率指标并下载 HTML 报告,供进一步分析。
- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml'
打包和交付代码
若要使用 twine
进行身份验证,请使用“Twine 身份验证”任务将身份验证凭据存储在 PYPIRC_PATH
环境变量中。
- task: TwineAuthenticate@0
inputs:
artifactFeed: '<Azure Artifacts feed name>'
pythonUploadServiceConnection: '<twine service connection from external organization>'
然后,添加一个使用 twine
来发布包的自定义脚本。
- script: |
twine upload -r "<feed or service connection name>" --config-file $(PYPIRC_PATH) <package path/files>
还可以使用 Azure Pipelines 为 Python 应用生成映像并将其推送到容器注册表。
相关扩展
- 适用于 PyCharm (IntelliJ) 的 Azure DevOps 插件 (Microsoft)
- Visual Studio Code 中的 Python (Microsoft)
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈