Azure Pipelines - 使用 Poetry 的正确方法

发布于 2025-01-13 13:26:10 字数 1315 浏览 2 评论 0原文

对于 Azure Pipelines,使用 poetry 安装 Python 包依赖项的推荐方法是什么?我看到人们只通过 pip 下载poetry,这是一个很大的禁忌。

- script: |
    python -m pip install -U pip
    pip install poetry
    poetry install
  displayName: Install dependencies

我可以使用curl来下载诗歌

  - script: |
      curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
      export PATH=$PATH:$HOME/.poetry/bin
      poetry install --no-root
    displayName: 'Install dependencies'

但在后续的每个步骤中,我都必须再次将诗歌添加到PATH中...

  - script: |
      curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
      export PATH=$PATH:$HOME/.poetry/bin
      poetry install --no-root
    displayName: 'Install dependencies'

  - script: |
      # export PATH=$PATH:$HOME/.poetry/bin
      poetry run flake8 src
    displayName: 'Linter'

  - script: |
      # export PATH=$PATH:$HOME/.poetry/bin
      poetry add pytest-azurepipelines
      poetry run pytest src
    displayName: 'Tests'

是否有任何正确方法可以在Azure Pipelines中使用诗歌?

what would be a recommended way to install your Python's package dependencies with poetry for Azure Pipelines? I see people only downloading poetry through pip which is a big no-no.

- script: |
    python -m pip install -U pip
    pip install poetry
    poetry install
  displayName: Install dependencies

I can use curl to download poetry.

  - script: |
      curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
      export PATH=$PATH:$HOME/.poetry/bin
      poetry install --no-root
    displayName: 'Install dependencies'

But then in each subsequent step I have to add poetry to PATH again ...

  - script: |
      curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
      export PATH=$PATH:$HOME/.poetry/bin
      poetry install --no-root
    displayName: 'Install dependencies'

  - script: |
      # export PATH=$PATH:$HOME/.poetry/bin
      poetry run flake8 src
    displayName: 'Linter'

  - script: |
      # export PATH=$PATH:$HOME/.poetry/bin
      poetry add pytest-azurepipelines
      poetry run pytest src
    displayName: 'Tests'

Is there any right way to use poetry in Azure Pipelines?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

追星践月 2025-01-20 13:26:10

这个问题咨询了一位同事。他建议采取单独的步骤将 Poetry 添加到 PATH 中。

  - task: UsePythonVersion@0
    inputs:
      versionSpec: '3.8'
    displayName: 'Use Python 3.8'

  - script: |
      curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
      export PATH=$PATH:$HOME/.poetry/bin
      poetry install --no-root
    displayName: 'Install dependencies'

  - script: echo "##vso[task.prependpath]$HOME/.poetry/bin"
    displayName: Add poetry to PATH

  - script: |
      poetry run flake8 src
    displayName: 'Linter'

  - script: |
      poetry add pytest-azurepipelines
      poetry run pytest src
    displayName: 'Tests'

Consulted this issue with a collegue. He recommended doing separate step to add Poetry to the PATH.

  - task: UsePythonVersion@0
    inputs:
      versionSpec: '3.8'
    displayName: 'Use Python 3.8'

  - script: |
      curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
      export PATH=$PATH:$HOME/.poetry/bin
      poetry install --no-root
    displayName: 'Install dependencies'

  - script: echo "##vso[task.prependpath]$HOME/.poetry/bin"
    displayName: Add poetry to PATH

  - script: |
      poetry run flake8 src
    displayName: 'Linter'

  - script: |
      poetry add pytest-azurepipelines
      poetry run pytest src
    displayName: 'Tests'
千秋岁 2025-01-20 13:26:10

我不确定这是否是最佳实践,但 Azure 构建代理确实安装了 pipx,这是 受支持的安装 Poetry 的方式。它仍然需要每次安装,但如果没有自托管,您就无法避免这种情况。

我确实尝试过使用诗歌,它似乎按我的预期工作。它为 Poetry 创建了一个单独的虚拟环境,然后您可以随心所欲地使用它。需要注意的是,我不是 Poetry 或 Python 部署专家。

I'm not certain this is a best practice, but Azure build agents do have pipx installed, and this is a supported way to install Poetry. It does still require an install every time, but you can't avoid that without self hosting.

I did try using Poetry and it seems to work as I'd expect. It creates a separate virtual environment for Poetry and then you can use it as you wish. Caveat here is I'm no Poetry or Python deployment expert.

我要还你自由 2025-01-20 13:26:10

以下 azure-pipelines.yml 对我来说就像一个魅力:

trigger:
- main

pool:
  vmImage: ubuntu-latest

variables:
  pythonVersion: '3.9'

steps:
- task: UsePythonVersion@0
  inputs:
    versionSpec: '$(pythonVersion)'

- script: pip install poetry
  displayName: 'Install poetry'

- script: poetry install
  displayName: 'Install dependencies'

- script: poetry run ruff check
  displayName: 'Run linter'

- script: poetry run pytest -s
  displayName: 'Run tests'

The following azure-pipelines.yml works like a charm for me:

trigger:
- main

pool:
  vmImage: ubuntu-latest

variables:
  pythonVersion: '3.9'

steps:
- task: UsePythonVersion@0
  inputs:
    versionSpec: '$(pythonVersion)'

- script: pip install poetry
  displayName: 'Install poetry'

- script: poetry install
  displayName: 'Install dependencies'

- script: poetry run ruff check
  displayName: 'Run linter'

- script: poetry run pytest -s
  displayName: 'Run tests'
如若梦似彩虹 2025-01-20 13:26:10

根据您的描述,我认为您使用的代理是微软代理?

我查了微软代理的官方文档,没有提供诗歌。因此,如果您使用 Microsoft 主机代理并且想要使用诗歌,那么在管道运行期间安装诗歌是不可避免的,

因此我建议您在自主机代理上运行管道。

您可以使用虚拟机或已有诗歌的本地计算机,然后在其上设置自主机代理。

之后,您可以在其上运行管道,这次您不需要再安装诗歌了。

详细步骤:

1、在虚拟机或本地机器上运行以下命令。

pip install诗

2、安装configure,并在上述虚拟机或机器上运行代理.

在我这边,我在虚拟机上设置了一个代理:

在此输入图片描述

请参考这个官方文档,这个文档会告诉你如何安装和运行自启动您这边的主机代理:

https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/v2-windows?view=azure-devops

3、基于代理运行管道上面运行的。

pool:
  name: VMAS
steps:
- script: |
   echo Write your commands here
   
   echo Hello world
   
   python --version
   
   poetry --version
   
  displayName: 'Command Line Script'

那么您就不需要每次都安装它。
输入图片此处描述

如果您还有更多疑虑,请告诉我。

From your description, I think the agent you are using is a Microsoft agent?

I checked the official document of the Microsoft agent, there is no poetry provided. Therefore, if you use Microsoft-host agent and you want to use poetry, install poetry during the pipeline run is inevitable

So I recommend you run your pipeline on a self-host agent.

You can use a VM or your local machine which already has the poetry and then set up a self-host agent on it.

After that, you can run your pipeline on it, this time you don't need to install the poetry anymore.

Detailed steps:

1, run the below command on a VM or local machine.

pip install poetry

2, Install configure, and run the agent in above VM or machine.

On my side, I set up an agent on VM:

enter image description here

Please refer to this official document, this document will tell you how to install and run the self-host agent on your side:

https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/v2-windows?view=azure-devops

3, Run your pipeline based on the agent that ran above.

pool:
  name: VMAS
steps:
- script: |
   echo Write your commands here
   
   echo Hello world
   
   python --version
   
   poetry --version
   
  displayName: 'Command Line Script'

Then you don't need to install it every time.
enter image description here

Let me know if you have more concerns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文