包: Python Twine Upload 进行身份验证

Azure DevOps Services

版本 1. * | 其他版本

为生成范围的环境变量提供 twine 凭据 PYPIRC_PATH 。 这使你可以从生成将 Python 包发布到源 twine

YAML 代码段

# Python twine upload authenticate V1
# Authenticate for uploading Python distributions using twine. Add '-r FeedName/EndpointName --config-file $(PYPIRC_PATH)' to your twine upload command. For feed present in this organization, use the feed name as the repository (-r). Otherwise, use the endpoint name defined in the service connection.
- task: TwineAuthenticate@1
  inputs:
    #artifactFeed: MyTestFeed # Optional
    #pythonUploadServiceConnection: OtherOrganizationFeed # Optional

参数

参数 说明
artifactFeed (可选) 用于向 twine 进行身份验证的 Azure Artifacts 源名称。 对于项目范围的源,请使用以下语法: 项目名称/feedName
pythonUploadServiceConnection (可选) 来自外部组织的 twine 服务连接 名称,以便使用 twine 进行身份验证。 终结点中存储的凭据必须具有包上传权限。

提示

请参阅 组织范围的源与项目范围的源 ,以了解这两种类型之间的区别,并了解如何检查源是否属于项目范围或组织范围。

示例

以下示例演示如何将 python 分发发布到 Azure Artifacts 源和官方 python 注册表。

将 python 分发发布到 Azure Artifacts 源

在此示例中,我们要将身份验证设置为发布到专用 Azure Artifacts 源。 身份验证任务将创建一个 .pypirc 文件,其中包含向源发布分发所需的身份验证凭据。

# Install python distributions like wheel, twine etc
- script: |
     pip install wheel
     pip install twine
  
# Build the python distribution from source
- script: |
     python setup.py bdist_wheel
   
- task: TwineAuthenticate@1
  displayName: Twine Authenticate
  inputs:
    # In this case, name of the feed is 'myTestFeed' in the project 'myTestProject'. Project is needed because the feed is project scoped.
    artifactFeed: myTestProject/myTestFeed
  
# Use command line script to 'twine upload', use -r to pass the repository name and --config-file to pass the environment variable set by the authenticate task.
- script: |
     python -m twine upload -r myTestFeed --config-file $(PYPIRC_PATH) dist/*.whl

如果源是项目范围的,则 artifactFeed 输入将包含项目和源名称。 如果源是组织范围内的,则必须仅提供源名称。 了解详细信息

将 python 分发发布到官方 python 注册表

在此示例中,我们将设置用于发布到官方 python 注册表的身份验证。 为pypi创建twine service 连接项。 身份验证任务使用该服务连接来创建一个 .pypirc 文件,其中包含发布分发所需的身份验证凭据。

# Install python distributions like wheel, twine etc
- script: |
     pip install wheel
     pip install twine
  
# Build the python distribution from source
- script: |
     python setup.py bdist_wheel
   
- task: TwineAuthenticate@1
  displayName: Twine Authenticate
  inputs:
    # In this case, name of the service connection is "pypitest".
    pythonUploadServiceConnection: pypitest
  
# Use command line script to 'twine upload', use -r to pass the repository name and --config-file to pass the environment variable set by the authenticate task.
- script: |
     python -m twine upload -r "pypitest" --config-file $(PYPIRC_PATH) dist/*.whl

任务版本: Twine 身份验证

任务版本 Azure Pipelines TFS
1. * 可用 不支持
0. * 可用 不支持

常见问题解答

应在管道中运行此任务吗?

此任务必须在使用 twine 将 python 分发上传到经过身份验证的包源(如 Azure Artifacts)之前运行。 无其他排序要求。 此任务多次调用将不会堆栈凭据。 每次运行任务时,都将清除以前存储的所有凭据。

代理位于 web 代理后。 TwineAuthenticate 设置 twine 以使用我的代理?

否。 尽管此任务本身将在 代理已配置为使用的 web 代理的后面工作,但它不会将 twine 配置为使用代理。

我的管道需要访问其他项目中的源

如果管道运行在与承载源的项目不同的项目中,则必须将另一个项目设置为授予对生成服务的读/写访问权限。 有关更多详细信息,请参阅Azure Pipelines 中的包权限

开源

此任务在 GitHub 上开放源代码。 欢迎提供反馈和建议。