如何使用Poetry将内容发布到Azure DevOps PyPI源?

6

我正在尝试使用Poetry设置Azure Devops,以发布到PyPI feed。

我知道关于Twine身份验证和将凭据存储到Azure Key Vault。但是否有更简单直接的方法?类似这样:

- script: |
    source .venv/bin/activate
    poetry build
  displayName: Build wheel
- script: |
    source .venv/bin/activate
    poetry publish -u USER -p PASS
  displayName: Publish wheel
3个回答

8

可以的。在Azure DevOps网页界面中:

  1. 创建新的PyPI feed(Artifacts > New feed > Create)。
  2. 创建PyPI凭据(Connect to feed > Python > Generate Python credentials)。
  3. 创建名为usernamepassword秘密管线变量,其值为PyPI凭据(Pipelines > Edit > Variables > New variable > Keep this value secret > OK)。
  4. 使用以下内容更新azure-pipelines.yml CI文件:
trigger:
- master

pool:
  vmImage: ubuntu-latest

steps:
- task: UsePythonVersion@0
  inputs:
    versionSpec: 3.7
  displayName: Install Python
- script: |
    python -m pip install -U pip
    pip install poetry
    poetry install
  displayName: Install software
- script: |
    poetry run python -m unittest discover tests/ -v
  displayName: Test software
- script: |
    poetry build
  displayName: Package software
- script: |
    poetry config repositories.azure https://pkgs.dev.azure.com/{your organization}/_packaging/{your feed}/pypi/upload
    poetry config http-basic.azure $(username) $(password)
    poetry publish -r azure
    exit 0
  displayName: Publish software

这是否意味着我需要在我的 .azure-pipelines.yml 文件中以明文形式发布我的密码或个人身份验证令牌?如果不是,我不确定你提到的诗歌 http-basic 配置行应该放什么。 - Dan Yeaw
@DanYeaw 是的,你可以在 azure-pipelines.yml 文件中以明文形式编写你的凭据(因为你要求一个简单直接的解决方案)。 - Géry Ogam
是的,简单明了很好,但我们还需要一些不会将我们的凭据暴露给所有人的东西。 - Dan Yeaw
1
@DanYeaw 我已经更新了答案,介绍了如何使用 Azure Pipelines 的_secret variables_来设置 PyPI 凭据,避免将其暴露在版本控制系统中,这是推荐的做法。希望这能帮到你。 - Géry Ogam

5

我们考虑使用 poetry 构建项目,并使用 twine 发布,这样我们就可以利用 Azure 的 TwineAuthenticate 了:

steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: '$(python.version)'
    displayName: 'Use Python $(python.version)'

  - script: |
      python -m pip install --upgrade pip
      pip install poetry
      pip install twine
      poetry install --no-dev
    displayName: 'Install dependencies'

  - script: |
      poetry build
    displayName: 'Build package'

  - task: TwineAuthenticate@1
    inputs:
      artifactFeed: 'repo-name/feed-name'

  - script: |
      twine upload -r repo-name --config-file $(PYPIRC_PATH) dist/*.whl
    displayName: Upload package to Azure Artifact

2

您可能需要使用$(System.AccessToken)变量:

- task: Bash@3
    inputs:
      targetType: 'inline'
      script: |
        poetry config repositories.myazurepypi https://myorg.pkgs.visualstudio.com/123415462134546/_packaging/lcp-tools/pypi/upload/
        poetry publish -r myazurepypi -u a -p $(System.AccessToken)

对于任何想知道的人,使用发布到工件时用户名并不重要,无论是使用PAT还是System.AccessToken,但它不能为空。 - RubberDuck

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接