Search code examples
azure-devopspython-poetry

Azure DevOps Pipelines and Poetry Dynamic Versioning


I have a pipeline which builds a Python package and publishes it to Azure Artecfacts.

It all works, but when the pipeline runs it builds a new version of my package with the version "0.0.0". (Recommended default version when using Poetry Dynamic Versioning)

When I run "poetry build" locally, I get what I would consider a proper version based on the last tag e.g. my-wonderful-package-0.1.1.post8.dev0+2321b98.tar.gz

Here are the most important parts of my pipeline:

 - task: UsePythonVersion@0
  inputs:
    versionSpec: '3.8'
    addToPath: true
    architecture: 'x64'

- task: CmdLine@2
  inputs:
    script: |
      curl -sSL https://install.python-poetry.org | python3 -
      export PATH=$PATH:$HOME/.poetry/bin
  displayName: 'Install poetry'

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

- task: CmdLine@2
  inputs:
    script: |
      poetry install --no-root
  displayName: 'Install Poetry dependencies'

- task: CmdLine@2
  displayName: Build Artifacts
  inputs:
    script: |
      echo Building distribution package
      poetry self add "poetry-dynamic-versioning[plugin]"
      poetry build

Also here is the dynamic settings from my pyproject.toml:

[build-system]
requires = ["poetry-core", "poetry-dynamic-versioning"]
build-backend = "poetry_dynamic_versioning.backend"

[[tool.poetry.source]]
(info about my feed)

[tool.poetry-dynamic-versioning]
enable = true
vcs = "git"
style= "pep440"

Should I not expect poetry build to behave the same way when building locally vs on the build agent? Am I missing something fundamental about how Poetry Dynamic Versioning works?


Solution

  • I can reproduce the similar issue in my pipeline with the same settings in pyproject.toml file.

    enter image description here

    The cause of the issue could be related to the fetch depth of the Pipeline repo.

    By default, the Shallow fetch of the pipeline repo is 1 by default.

    You can try to set the fetchDepth to 0 in YAML Pipeline.

    For example:

    steps:
    - checkout: self
      fetchDepth: 0
      
    - task: UsePythonVersion@0
      inputs:
        versionSpec: '3.8'
        addToPath: true
        architecture: 'x64'
    
    - task: CmdLine@2
      inputs:
        script: |
          curl -sSL https://install.python-poetry.org | python3 -
          export PATH=$PATH:$HOME/.poetry/bin
      displayName: 'Install poetry'
     ...
    

    Or you can navigate to YAML Pipeline -> ... -> Triggers -> YAML -> Get sources -> Shallow fetch. You can unselect the option.

    enter image description here

    enter image description here