Skip to content

Commit

Permalink
Publish to PyPI through GitHub (#689)
Browse files Browse the repository at this point in the history
In the webpage, the version format is v[major].[minor]
In setup.py, the prefix v will be removed, so the __version__ will be [major].[minor]
v[major].[minor] will be used as tag name in gh actions
  • Loading branch information
lanpa committed Feb 12, 2023
1 parent a4a19db commit ceabb20
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 46 deletions.
46 changes: 0 additions & 46 deletions .github/workflows/build-pip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,49 +78,3 @@ jobs:
path: |
examples/runs
upload-to-pypi:
if: startsWith(github.ref, 'refs/tags/v')
name: Upload to pypi if tagged with version
needs: build
runs-on: ubuntu-latest
steps:
- name: Dump GitHub context
id: github_context_step
run: echo '${{ toJSON(github) }}'

# upload:
# if: startsWith('github.ref', 'refs/tags/v')
# name: Publish a Python distribution to PyPI test site
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v2
# - name: Set up Python 3.9
# uses: actions/setup-python@v2
# with:
# python-version: 3.9
#
# - name: Install pypa/build
# run: >-
# python -m
# pip install
# build wheel
# --user
#
# - name: Build a binary wheel and a source tarball
# run: |
# echo `pwd`
# python setup.py -q sdist bdist_wheel --universal
#
# - name: Publish to Test PyPI
# uses: pypa/gh-action-pypi-publish@release/v1
# with:
# password: ${{ secrets.PYPI_API_TESTSITE }}
# repository_url: https://test.pypi.org/legacy/


# - name: Publish to Test PyPI
# uses: pypa/gh-action-pypi-publish@release/v1
# with:
# password: ${{ secrets.PYPI_API_TESTSITE }}
# repository_url: https://test.pypi.org/legacy/
#
46 changes: 46 additions & 0 deletions .github/workflows/publish-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,49 @@ jobs:
env:
VERSION: ${{ github.event.inputs.publish_version }}
upload-to-pypi:
name: Publish to PyPI test site
runs-on: ubuntu-latest
steps:
- name: Dump GitHub context
id: github_context_step
run: |
echo '${{ toJSON(github) }}'
echo "target version: ${{ github.event.inputs.publish_version }}"
- uses: actions/checkout@v3
- name: Set up Python 3.9
uses: actions/setup-python@v3
with:
python-version: 3.9

- name: Install pypa/build
run: >-
python -m
pip install
build wheel
--user
- name: Build a binary wheel and a source tarball
run: |
echo `pwd`
python setup.py -q sdist bdist_wheel --universal ${{ github.event.inputs.publish_version }}
- name: Install tensorboardX from whl
run: pip install dist/tensorboardX*.whl

- name: Print version
run: python -c "import tensorboardX; print(tensorboardX.__version__)"

- name: Publish to Test PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TESTSITE }}
repository_url: https://test.pypi.org/legacy/

- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
#password: ${{ secrets.PYPI_API }}
repository_url: https://upload.pypi.org/legacy/

8 changes: 8 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ def run(self):
preparing_PyPI_package = 'sdist' in sys.argv or 'bdist_wheel' in sys.argv
version_git = version = subprocess.check_output(['git', 'describe', '--always']).decode('ascii').strip()

# pass version without using argparse
# format example: v1.2.3
publish_version = sys.argv[-1]
if publish_version[0] == 'v':
version_git = publish_version[1:]
sys.argv = sys.argv[:-1]
print(version_git)

if not preparing_PyPI_package:
if os.path.exists('.git'):
sha = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode('ascii').strip()
Expand Down

0 comments on commit ceabb20

Please sign in to comment.