分享方式:


自定義 Python 管線

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

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

您可能需要 要求平行作業 的免費授與,或購買 平行作業 以執行管線。

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

使用特定的 Python 版本

若要在管線中使用特定版本的 Python,請將使用 Python 版本工作新增azure-pipelines.yml。 下列範例 YAML 管線定義會將管線設定為使用 Python 3.11。

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

使用多個 Python 版本

若要執行具有多個 Python 版本的管線,例如,若要針對這些版本測試套件,請使用 matrix Python 版本的 來定義 job 。 然後設定工作 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

安裝相依性

您可以使用文稿搭配 來安裝特定的 PyPI 套件 pip。 下列範例會安裝或升級 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 測試

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