使用 Azure Pipelines 發佈 Python 套件

Azure DevOps Services |Azure DevOps Server 2022 - Azure DevOps Server 2019

開發人員可以使用 Azure Pipelines 將 Python 套件發佈至 Azure Artifacts 摘要、公用登錄,或將它們儲存為管線成品。 本文將引導您瞭解如何:

  • 安裝必要條件
  • 連線 至 Azure Artifacts 摘要
  • 將 Python 套件發佈至 Azure Artifacts 摘要

必要條件

使用 Azure Artifacts 進行驗證

若要用來 twine 發佈 Python 套件,您必須先向 Azure Artifacts 摘要進行驗證。 TwineAuthenticate 工作會將對應項認證PYPIRC_PATH提供給環境變數。 接著會使用twine 這個變數,協助您直接從管線發佈套件。

- task: TwineAuthenticate@1
  inputs:
    artifactFeed: <PROJECT_NAME/FEED_NAME>     ## For an organization-scoped feed, artifactFeed: <FEED_NAME>

重要

儲存在環境變數中的 PYPIRC_PATH 認證會取代您 .ini.conf 檔案中的認證。

如果您在管線的不同階段新增多個 TwineAuthenticate 工作,則每個額外的工作執行都會擴充現有的PYPIRC_PATH環境變數(而非覆寫)。

將 Python 套件發佈至 Azure Artifacts 摘要

- script: |
    pip install build
    pip install twine
  displayName: 'Install build and twine'

- script: |
    python -m build -w
  displayName: 'Python build'

- task: TwineAuthenticate@1
  inputs:
    artifactFeed: <PROJECT_NAME/FEED_NAME>
  displayName: 'Twine Authenticate'

- script: |
    python -m twine upload -r <FEED_NAME> --config-file $(PYPIRC_PATH) dist/*.whl
  displayName: 'Upload to feed'

注意

若要使用 Azure Pipelines 將套件發佈至摘要, 專案集合組建服務和 專案的 建置服務 身分識別都必須 在摘要設定中指派摘要發行者(參與者) 角色。 如需詳細資訊,請參閱 管理許可權