|
| 1 | +name: Release Python Package |
| 2 | +on: |
| 3 | + push: |
| 4 | + tags: |
| 5 | + - "v*" |
| 6 | +jobs: |
| 7 | + release: |
| 8 | + runs-on: ubuntu-latest |
| 9 | + steps: |
| 10 | + # Checkout project repository |
| 11 | + - name: Checkout |
| 12 | + uses: actions/checkout@v3 |
| 13 | + |
| 14 | + # Setup Python environment |
| 15 | + - name: Setup Python |
| 16 | + uses: actions/setup-python@v4 |
| 17 | + with: |
| 18 | + python-version: "3.9" |
| 19 | + |
| 20 | + # Install dependencies |
| 21 | + - name: Install dependencies |
| 22 | + run: | |
| 23 | + python -m pip install --upgrade pip |
| 24 | + pip install wheel twine |
| 25 | +
|
| 26 | + # Extract version from tag |
| 27 | + - name: Get version from tag |
| 28 | + run: echo "NEW_VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_ENV |
| 29 | + |
| 30 | + # Determine if it's a pre-release |
| 31 | + - name: Check if pre-release |
| 32 | + run: | |
| 33 | + if [[ ${{ env.NEW_VERSION }} == *"-"* ]]; then |
| 34 | + echo "RELEASE_TAG=beta" >> $GITHUB_ENV |
| 35 | + else |
| 36 | + echo "RELEASE_TAG=latest" >> $GITHUB_ENV |
| 37 | + fi |
| 38 | +
|
| 39 | + # Update changelog unreleased section with new version |
| 40 | + - name: Update changelog |
| 41 | + uses: superfaceai/release-changelog-action@v1 |
| 42 | + with: |
| 43 | + path-to-changelog: CHANGELOG.md |
| 44 | + version: ${{ env.NEW_VERSION }} |
| 45 | + operation: release |
| 46 | + |
| 47 | + - name: Update version |
| 48 | + run: | |
| 49 | + sed -i "s/version=.*,/version='${{ env.NEW_VERSION }}',/" setup.py |
| 50 | +
|
| 51 | + # Configure Git |
| 52 | + - name: Git configuration |
| 53 | + run: | |
| 54 | + git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 55 | + git config --global user.name "GitHub Actions" |
| 56 | +
|
| 57 | + # Commit changes |
| 58 | + - name: Commit version and changelog |
| 59 | + run: | |
| 60 | + git add CHANGELOG.md |
| 61 | + git add setup.py |
| 62 | + git commit -m "chore: release ${{ env.NEW_VERSION }}" |
| 63 | +
|
| 64 | + # Push changes and tags |
| 65 | + - name: Push changes |
| 66 | + env: |
| 67 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 68 | + run: | |
| 69 | + git push origin HEAD:main |
| 70 | +
|
| 71 | + # Build package |
| 72 | + - name: Build package |
| 73 | + run: python setup.py sdist bdist_wheel |
| 74 | + |
| 75 | + - name: Publish to PyPI |
| 76 | + env: |
| 77 | + TWINE_USERNAME: __token__ |
| 78 | + TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} |
| 79 | + run: | |
| 80 | + python -m twine upload dist/* |
| 81 | +
|
| 82 | + # Read version changelog |
| 83 | + - id: get-changelog |
| 84 | + name: Get version changelog |
| 85 | + uses: superfaceai/release-changelog-action@v1 |
| 86 | + with: |
| 87 | + path-to-changelog: CHANGELOG.md |
| 88 | + version: ${{ env.NEW_VERSION }} |
| 89 | + operation: read |
| 90 | + |
| 91 | + # Update GitHub release with changelog |
| 92 | + - name: Update GitHub release documentation |
| 93 | + uses: softprops/action-gh-release@v1 |
| 94 | + with: |
| 95 | + tag_name: ${{ env.NEW_VERSION }} |
| 96 | + body: ${{ steps.get-changelog.outputs.changelog }} |
| 97 | + prerelease: ${{ startsWith(github.event.inputs.release-type, 'pre') }} |
| 98 | + env: |
| 99 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments