Convert Git tag to Python version on the command line in Gitlab CI

777 Views Asked by At

In a Gitlab CI pipeline, I want to check that the package that was just uploaded to a registry is there and works well. What I do is basically:

[…]

stages:
  - […]
  - deploy
  - deploytest

pip-upload:
  stage: deploy
  rules:
    - if: '$CI_COMMIT_TAG && $CI_COMMIT_TAG =~ /v[1-9].+/'
  script:
    - python3 setup.py sdist
    - python3 -m twine upload --repository-url […] dist/*

test-pip:
  stage: deploytest
  rules:
    - if: '$CI_COMMIT_TAG && $CI_COMMIT_TAG =~ /v[1-9].+/'
  script:
    - python3 -m pip install --extra-index-url […] --upgrade --pre "mypackage==$PACKAGE_VERSION"
    - cd /tmp
    - python3 -m pytest --pyargs mypackage

This however requires the creation of the Python version from the git tag on the command line. I use setuptools_scm in my package. I there a way to call this package in a way that it returns the version corresponding to a particular git tag?

1

There are 1 best solutions below

0
sytech On

You can run python -m setuptools_scm to get the version. See python -m setuptools_scm --help for additional options.

You can also run python setup.py --version to get the version (assuming setuptools-scm is installed).