自訂適用于 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 版本的 來定義 jobmatrix 。 然後設定工作 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')

若要將腳本執行參數化,請使用 PythonScript 具有 arguments 值的 工作,將引數傳遞至執行中的進程。 您可以使用 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

安裝相依性

您可以使用腳本搭配 安裝特定的 PyPI 套件 pip 。 例如,此 YAML 會安裝或升級 pipsetuptoolswheel 套件。

- 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 來安裝和 pytestpytest-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 應用程式 建置映射 ,並將 它推送至容器登錄