Python 在 github 操作中 运行 时未发生自动版本控制

Python automatic versioning not happening while running in github actions

我试图在成功构建后将包从 CI 直接推送到 pypi。 我已经尝试了几个工具,比如“setuptools-scm”,一切正常,我根据我的标签自动更改版本,比如我本地的 package-0.0.2.post11-py3-none-any.whl

当我推送相同的代码作为 github 操作的一部分(命令 Run python3 setup.py sdist bdist_wheel)时,我没有看到版本得到更新,我总是得到 package-0.0.0-py3-none-any.whl

下面是setup.py

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

yml 文件:

 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"

我不清楚我在这里做错了什么,有人可以帮助解决这个问题吗?

谢谢

操作 - uses: actions/checkout@v2 未在签出时提取标签。必须另外添加以下行以从 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

正如 所说,默认情况下签出存储库的浅表副本(一次提交)。除非最后一次提交有标签,否则基于标签的自动版本控制工具将无法使用。

由于 actions/checkout 合并了 Pull Request #258 Fetch all history for all tags and branches when fetch-depth=0,您可以使用 fetch-depth: 0 选项和 actions/checkout@v2 来获取完整的历史记录(包括标签)并启用设置工具-scm 以正确推断版本名称。

  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

不幸的是,这对于大型回购来说可能会很慢,actions/checkout #249 有一个未解决的问题来解决这个问题。