kbk
kbk

Reputation: 605

Python automatic versioning not happening while running in github actions

I was trying to push the package from the CI directly to pypi after successful build. I have tried couple of tools, say "setuptools-scm", everything works well and i get automatic version change based on my tagging like package-0.0.2.post11-py3-none-any.whl in my local.

When i push the same code as part of github actions (command Run python3 setup.py sdist bdist_wheel) i dont see the versions getting updated and i always get package-0.0.0-py3-none-any.whl

Below is the snippet of setup.py

setuptools.setup(
    name="package",
    use_scm_version=True,
    setup_requires=['setuptools_scm']

ymlfile:

 publish:
    name: Build and publish Python 🐍 distributions 📦 to PyPI and TestPyPI
    runs-on: ubuntu-18.04
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.8
      uses: actions/setup-python@v2
      with:
        python-version: 3.8
    - name: Install scm version
      run: >-
        python -m
        pip install
        setuptools-scm==4.1.2        
    - name: Install wheel
      run: >-
        python -m
        pip install
        wheel==0.34.2
    - name: Build a binary wheel and a source tarball
      run: >-
        python3 setup.py sdist bdist_wheel
    - name: Publish distribution 📦 to Test PyPI
      uses: pypa/gh-action-pypi-publish@master
      with:
        password: ${{ secrets.test_pypi_password }}
        repository_url: https://test.pypi.org/legacy/
    - name: Publish distribution 📦 to PyPI
      if: startsWith(github.ref, 'refs/tags')
      uses: pypa/gh-action-pypi-publish@master
      with:
        password: ${{ secrets.pypi_password }}

pyproject.toml

# pyproject.toml
[build-system]
requires = ["setuptools>=42", "wheel", "setuptools_scm[toml]>=3.4"]
[tool.setuptools_scm]
write_to = "pkg/version.py"

I am not clear what i am doing wrong here, Can any one help to fix this ?

Thanks

Upvotes: 6

Views: 3183

Answers (2)

colorado_roads
colorado_roads

Reputation: 91

As kbk said, a shallow copy (one commit) of the repo is checked out by default. Unless the last commit has a tag, tag-based automatic versioning tools have nothing to work with.

Since the actions/checkout merged Pull Request #258 Fetch all history for all tags and branches when fetch-depth=0, you can use fetch-depth: 0 option with actions/checkout@v2 to fetch a complete history (including tags) and enable setuptools-scm to correctly deduce version names.

  steps:
  - uses: actions/checkout@v2
    with:
      fetch-depth: 0
  - name: Set up Python 3.8
    uses: actions/setup-python@v2
      with:
        python-version: 3.8

Unfortunately, this can be slow for large repos and there's an open issue with actions/checkout #249 to address this.

Upvotes: 5

kbk
kbk

Reputation: 605

the action - uses: actions/checkout@v2 is not fetching the tags along while checking out. Had to additionally add below lines to fetch the tags from git

publish:
    name: Build and publish Python 🐍 distributions 📦 to PyPI and TestPyPI
    runs-on: ubuntu-18.04
    steps:
    - uses: actions/checkout@v2
    - name: Fetch all history for all tags and branches
      run: git fetch --prune --unshallow
    - name: Set up Python 3.8
      uses: actions/setup-python@v2
      with:
        python-version: 3.8
    - name: Install scm version
      run: >-
        python -m
        pip install
        versiontag      
    - name: Install wheel
      run: >-
        python -m
        pip install
        wheel==0.34.2

Upvotes: 5

Related Questions