|
| 1 | +name: Build and Deploy to PyPI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - 'v*.*.*' |
| 7 | + |
| 8 | +jobs: |
| 9 | + validate-tag: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + |
| 12 | + steps: |
| 13 | + - name: Checkout repository |
| 14 | + uses: actions/checkout@v3 |
| 15 | + |
| 16 | + - name: Extract current tag |
| 17 | + id: extract-tag |
| 18 | + run: | |
| 19 | + TAG_REF="${GITHUB_REF#refs/tags/}" |
| 20 | + echo "tag=$TAG_REF" >> $GITHUB_OUTPUT |
| 21 | +
|
| 22 | + - name: Validate tag format |
| 23 | + run: | |
| 24 | + TAG=${{ steps.extract-tag.outputs.tag }} |
| 25 | + if ! [[ "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 26 | + echo "Invalid tag format. Expected vX.Y.Z" |
| 27 | + exit 1 |
| 28 | + fi |
| 29 | +
|
| 30 | + - name: Fetch all tags |
| 31 | + run: git fetch --tags --quiet |
| 32 | + |
| 33 | + - name: Ensure tag is newer than latest |
| 34 | + run: | |
| 35 | + CURRENT="${{ steps.extract-tag.outputs.tag }}" |
| 36 | + LATEST=$(git tag --sort=-v:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -n 1) |
| 37 | +
|
| 38 | + echo "Current tag: $CURRENT" |
| 39 | + echo "Latest tag: $LATEST" |
| 40 | +
|
| 41 | + if [ -n "$LATEST" ]; then |
| 42 | + python3 -c " |
| 43 | +from packaging.version import parse |
| 44 | +if parse('$CURRENT'[1:]) <= parse('$LATEST'[1:]): |
| 45 | + raise SystemExit('Tag ' + '$CURRENT' + ' is not newer than latest tag ' + '$LATEST') |
| 46 | +" |
| 47 | +
|
| 48 | + build: |
| 49 | + needs: validate-tag |
| 50 | + runs-on: ubuntu-latest |
| 51 | +
|
| 52 | + steps: |
| 53 | + - uses: actions/checkout@v3 |
| 54 | + - uses: actions/setup-python@v4 |
| 55 | + with: |
| 56 | + python-version: "3.12" |
| 57 | + |
| 58 | + - name: Install build tools |
| 59 | + run: | |
| 60 | + python -m pip install --upgrade pip |
| 61 | + pip install build |
| 62 | +
|
| 63 | + - name: Build package |
| 64 | + run: python -m build |
| 65 | + |
| 66 | + - name: Upload build artifacts |
| 67 | + uses: actions/upload-artifact@v4 |
| 68 | + with: |
| 69 | + name: dist |
| 70 | + path: dist/ |
| 71 | + |
| 72 | + test-pypi: |
| 73 | + needs: build |
| 74 | + runs-on: ubuntu-latest |
| 75 | + |
| 76 | + steps: |
| 77 | + - uses: actions/checkout@v3 |
| 78 | + - uses: actions/setup-python@v4 |
| 79 | + with: |
| 80 | + python-version: "3.12" |
| 81 | + |
| 82 | + - name: Download built package |
| 83 | + uses: actions/download-artifact@v4 |
| 84 | + with: |
| 85 | + name: dist |
| 86 | + path: dist/ |
| 87 | + |
| 88 | + - name: Upload to Test PyPI |
| 89 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 90 | + with: |
| 91 | + password: ${{ secrets.TEST_PYPI_API_TOKEN }} |
| 92 | + repository-url: https://test.pypi.org/legacy/ |
| 93 | + |
| 94 | + pypi: |
| 95 | + needs: test-pypi |
| 96 | + runs-on: ubuntu-latest |
| 97 | + |
| 98 | + steps: |
| 99 | + - uses: actions/checkout@v3 |
| 100 | + - uses: actions/setup-python@v4 |
| 101 | + with: |
| 102 | + python-version: "3.12" |
| 103 | + |
| 104 | + - name: Download built package |
| 105 | + uses: actions/download-artifact@v4 |
| 106 | + with: |
| 107 | + name: dist |
| 108 | + path: dist/ |
| 109 | + |
| 110 | + - name: Upload to PyPI |
| 111 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 112 | + with: |
| 113 | + password: ${{ secrets.PYPI_API_TOKEN }} |
| 114 | + |
0 commit comments