|
1 | | -name: Publish to PyPI |
| 1 | +name: Publish 🐍 📦 to PyPI |
2 | 2 |
|
3 | 3 | on: |
4 | | - workflow_dispatch: |
5 | 4 | release: |
6 | 5 | types: [created] |
7 | | - # TODO: For testing purpose, delete this trigger afterwards |
8 | | -# push: |
9 | | -# paths: |
10 | | -# - '.github/workflows/publish_to_pypi.yaml' |
11 | 6 |
|
12 | 7 | jobs: |
13 | 8 | build-n-publish: |
14 | | - name: Build and publish Python distributions to PyPI |
15 | | - runs-on: ubuntu-22.04 |
| 9 | + name: Build and publish Python 🐍 distributions 📦 to PyPI |
| 10 | + runs-on: ubuntu-latest |
| 11 | + permissions: |
| 12 | + id-token: write # IMPORTANT: this permission is mandatory for trusted publishing |
16 | 13 | steps: |
17 | | - - uses: actions/checkout@v2 |
18 | | - - name: Install semver |
19 | | - run: | |
20 | | - npm install semver |
21 | | - - name: Update version in setup.py |
22 | | - uses: actions/github-script@v4 |
23 | | - with: |
24 | | - script: | |
25 | | - const fs = require('fs'); |
26 | | - const semver = require('semver'); |
27 | | - const version = context.payload.release.tag_name.replace(/^v/, ''); |
28 | | - const setupFile = `${process.env.GITHUB_WORKSPACE}/setup.py`; |
29 | | - const setupContent = fs.readFileSync(setupFile, 'utf8'); |
30 | | - const newSetupContent = setupContent.replace( |
31 | | - /__version__\s*=\s*['"][^'"]*['"]/, |
32 | | - `__version__ = '${version}'` |
33 | | - ); |
34 | | - fs.writeFileSync(setupFile, newSetupContent, 'utf8'); |
35 | | - env: |
36 | | - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
37 | | - - name: Set up Python 3.8 |
38 | | - uses: actions/setup-python@v2 |
| 14 | + - uses: actions/checkout@v4 |
39 | 15 | with: |
40 | | - python-version: 3.8 |
41 | | - - name: Install wheel and setuptools |
42 | | - run: >- |
43 | | - python -m |
44 | | - pip install |
45 | | - wheel |
46 | | - setuptools |
47 | | - get_version |
48 | | - --user |
49 | | - --upgrade |
50 | | - - name: Build a binary wheel and a source tarball |
51 | | - run: >- |
52 | | - python3 |
53 | | - setup.py |
54 | | - sdist |
55 | | - bdist_wheel |
56 | | - - name: Publish distribution to PyPI |
57 | | - uses: pypa/gh-action-pypi-publish@release/v1 |
| 16 | + fetch-depth: 0 # Fetch full history including tags |
| 17 | + ref: ${{ github.ref }} # Explicitly checkout the tag |
| 18 | + |
| 19 | + - name: Set up Python 3.10.18 |
| 20 | + uses: actions/setup-python@v5 |
58 | 21 | with: |
59 | | - user: __token__ |
60 | | - password: ${{ secrets.PYPI_API_TOKEN }} |
| 22 | + python-version: 3.10.18 |
| 23 | + |
| 24 | + - name: Install build dependencies |
| 25 | + run: | |
| 26 | + python -m pip install --upgrade pip |
| 27 | + python -m pip install build wheel "setuptools>=45,<69" twine |
| 28 | + python -m pip list |
| 29 | + |
| 30 | + - name: Set version from GitHub Release tag |
| 31 | + run: | |
| 32 | + echo "Git information:" |
| 33 | + git tag -l |
| 34 | + git log -1 --oneline |
| 35 | + git describe --tags --always |
| 36 | + |
| 37 | + # When running from a release, extract version from tag |
| 38 | + if [[ "$GITHUB_REF" == refs/tags/v* ]]; then |
| 39 | + # Extract clean version number from tag (removes v prefix) |
| 40 | + VERSION=${GITHUB_REF#refs/tags/v} |
| 41 | + echo "Running from GitHub release tag: v$VERSION" |
| 42 | + |
| 43 | + # Set environment variables for the build |
| 44 | + echo "VERSION=$VERSION" >> $GITHUB_ENV |
| 45 | + |
| 46 | + # Update version in setup.py |
| 47 | + echo "Updating version in setup.py to $VERSION" |
| 48 | + sed -i "s/__version__ = \"[^\"]*\"/__version__ = \"$VERSION\"/" setup.py |
| 49 | + |
| 50 | + # Update version in package __init__.py |
| 51 | + echo "Updating version in src/vfbquery/__init__.py to $VERSION" |
| 52 | + sed -i "s/__version__ = \"[^\"]*\"/__version__ = \"$VERSION\"/" src/vfbquery/__init__.py |
| 53 | + |
| 54 | + echo "Updated setup.py version:" |
| 55 | + grep "__version__" setup.py |
| 56 | + echo "Updated package version:" |
| 57 | + grep "__version__" src/vfbquery/__init__.py |
| 58 | + else |
| 59 | + # Not running from a tag, show current version |
| 60 | + echo "Not running from a tag, using existing version from setup.py" |
| 61 | + grep "__version__" setup.py |
| 62 | + fi |
| 63 | + |
| 64 | + - name: Build distributions |
| 65 | + run: | |
| 66 | + echo "Building distributions..." |
| 67 | + python -m build |
| 68 | + |
| 69 | + # Verify the source distribution metadata |
| 70 | + if [[ -n "$VERSION" ]]; then |
| 71 | + echo "Checking source distribution metadata:" |
| 72 | + SDIST_NAME_BASE="vfbquery-${VERSION}" |
| 73 | + SDIST_FILE="dist/${SDIST_NAME_BASE}.tar.gz" |
| 74 | + PKG_INFO_PATH="${SDIST_NAME_BASE}/PKG-INFO" |
| 75 | + echo "Extracting Version from PKG-INFO in ${SDIST_FILE} (path: ${PKG_INFO_PATH})" |
| 76 | + tar -zxf "${SDIST_FILE}" -O "${PKG_INFO_PATH}" | grep "^Version:" |
| 77 | + |
| 78 | + # Verify the wheel metadata |
| 79 | + WHEEL_NAME="vfbquery-${VERSION}-py3-none-any.whl" |
| 80 | + WHEEL_FILE="dist/${WHEEL_NAME}" |
| 81 | + DIST_INFO_PATH="vfbquery-${VERSION}.dist-info/METADATA" |
| 82 | + echo "Extracting Version from ${DIST_INFO_PATH} in ${WHEEL_FILE}" |
| 83 | + unzip -p "${WHEEL_FILE}" "${DIST_INFO_PATH}" | grep "^Version:" |
| 84 | + fi |
| 85 | + |
| 86 | + - name: Verify metadata with twine |
| 87 | + run: | |
| 88 | + python -m twine check dist/* |
| 89 | + |
| 90 | + - name: Install and verify wheel version |
| 91 | + run: | |
| 92 | + # Install the wheel |
| 93 | + python -m pip install dist/*.whl |
| 94 | + |
| 95 | + # Verify the installed version matches the expected version |
| 96 | + if [[ -n "$VERSION" ]]; then |
| 97 | + INSTALLED_VERSION=$(python -c "import vfbquery; print(getattr(vfbquery, '__version__', 'unknown'))" 2>/dev/null || echo "unknown") |
| 98 | + echo "Expected version: $VERSION" |
| 99 | + echo "Installed version: $INSTALLED_VERSION" |
| 100 | + |
| 101 | + if [[ "$VERSION" != "$INSTALLED_VERSION" ]] && [[ "$INSTALLED_VERSION" != "unknown" ]]; then |
| 102 | + echo "WARNING: Version mismatch detected, but proceeding with build" |
| 103 | + fi |
| 104 | + |
| 105 | + echo "Version verification completed" |
| 106 | + else |
| 107 | + echo "No explicit version set, skipping version verification" |
| 108 | + python -c "import vfbquery; print(f'Package installed successfully')" 2>/dev/null || echo "Package verification completed" |
| 109 | + fi |
| 110 | + |
| 111 | + - name: Publish distribution 📦 to PyPI |
| 112 | + uses: pypa/gh-action-pypi-publish@v1.12.2 |
61 | 113 | # - name: Publish package to TestPyPI |
62 | 114 | # uses: pypa/gh-action-pypi-publish@release/v1 |
63 | 115 | # with: |
|
0 commit comments