共用方式為


自定義 Python 管線

本文說明如何在 Azure Pipelines 中自定義建置、測試、封裝及傳遞 Python 應用程式和程式代碼。 若要使用 Python 建立您的第一個管線,請參閱 Python 快速入門

在 Azure Pipelines 中使用 Microsoft 裝載的代理程式 ,您可以建置 Python 應用程式,而不需要設定自己的基礎結構。 您經常用來建置、測試及執行 Python 應用程式的工具,包括 pip預安裝。

您可能需要 申請免費的平行工作 或購買 平行工作 以執行管線。

若要使用 Azure Pipelines 建置 Python 應用程式,您需要 已安裝 Python 的自我裝載代理程式 。 若要在代理程式上安裝 Python,請參閱 UsePythonVersion

使用特定的 Python 版本

若要在管線中使用特定版本的 Python,請將Use Python version task加入到azure-pipelines.yml中。 下列範例 YAML 管線定義會將管線設定為使用 Python 3.11。

steps:
- task: UsePythonVersion@0
  inputs:
    versionSpec: '3.11'

使用多個 Python 版本

若要執行具有多個 Python 版本的管線,例如針對這些版本測試套件,請定義一個包含 jobmatrix Python 版本。 然後將 UsePythonVersion 工作設為參考 matrix 變數。 例如:

jobs:
- job: 'Test'
  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
    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

安裝依賴項

您可以使用腳本搭配 pip 來安裝特定的 PyPI 套件。 下列範例會安裝或升級 pip 和和 setuptoolswheel 套件。

- 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 測試

下列 YAML 程式代碼會安裝或升級 flake8 ,並用它來執行 lint 測試。

- 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@2
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml'

封裝和傳遞程序代碼

twine使用 Python 對應項上傳驗證工作進行驗證,以將驗證認證儲存在環境變數中PYPIRC_PATH

- task: TwineAuthenticate@1
  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 應用程式建置映像 ,並將其 推送至容器登錄