|
| 1 | +name: Publish to PyPI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + paths: |
| 7 | + - 'pyproject.toml' |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +jobs: |
| 11 | + check-version: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + outputs: |
| 14 | + should_publish: ${{ steps.check.outputs.should_publish }} |
| 15 | + version: ${{ steps.check.outputs.version }} |
| 16 | + steps: |
| 17 | + - uses: actions/checkout@v4 |
| 18 | + with: |
| 19 | + fetch-depth: 2 |
| 20 | + |
| 21 | + - name: Check version change |
| 22 | + id: check |
| 23 | + run: | |
| 24 | + VERSION=$(grep '^version = ' pyproject.toml | sed 's/version = "\(.*\)"/\1/') |
| 25 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 26 | + |
| 27 | + if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then |
| 28 | + echo "should_publish=true" >> $GITHUB_OUTPUT |
| 29 | + exit 0 |
| 30 | + fi |
| 31 | + |
| 32 | + git show HEAD~1:pyproject.toml > /tmp/old.toml 2>/dev/null || { |
| 33 | + echo "should_publish=true" >> $GITHUB_OUTPUT |
| 34 | + exit 0 |
| 35 | + } |
| 36 | + |
| 37 | + OLD_VERSION=$(grep '^version = ' /tmp/old.toml | sed 's/version = "\(.*\)"/\1/' || echo "") |
| 38 | + |
| 39 | + if [ "$VERSION" != "$OLD_VERSION" ]; then |
| 40 | + echo "should_publish=true" >> $GITHUB_OUTPUT |
| 41 | + else |
| 42 | + echo "should_publish=false" >> $GITHUB_OUTPUT |
| 43 | + fi |
| 44 | +
|
| 45 | + publish: |
| 46 | + needs: check-version |
| 47 | + if: needs.check-version.outputs.should_publish == 'true' |
| 48 | + runs-on: ubuntu-latest |
| 49 | + permissions: |
| 50 | + contents: write |
| 51 | + id-token: write |
| 52 | + environment: |
| 53 | + name: pypi |
| 54 | + url: https://pypi.org/project/xdk/ |
| 55 | + steps: |
| 56 | + - uses: actions/checkout@v4 |
| 57 | + |
| 58 | + - uses: actions/setup-python@v5 |
| 59 | + with: |
| 60 | + python-version: '3.11' |
| 61 | + |
| 62 | + - name: Build |
| 63 | + run: | |
| 64 | + pip install build |
| 65 | + python -m build |
| 66 | + |
| 67 | + - name: Publish to PyPI |
| 68 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 69 | + |
| 70 | + - name: Create tag |
| 71 | + run: | |
| 72 | + git config user.name "github-actions[bot]" |
| 73 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 74 | + git tag -a "v${{ needs.check-version.outputs.version }}" -m "v${{ needs.check-version.outputs.version }}" || true |
| 75 | + git push origin "v${{ needs.check-version.outputs.version }}" || true |
| 76 | + |
| 77 | + - name: Create Release |
| 78 | + uses: softprops/action-gh-release@v2 |
| 79 | + with: |
| 80 | + tag_name: v${{ needs.check-version.outputs.version }} |
| 81 | + generate_release_notes: true |
0 commit comments