'Azure Pipelines - proper way to use Poetry

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?



Solution 1:[1]

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://docs.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.

Solution 2:[2]

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'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bowman Zhu-MSFT
Solution 2 Piotr Rarus