|
| 1 | +name: Publish to GitHub Release on Version Change |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + |
| 8 | +jobs: |
| 9 | + publish: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + steps: |
| 12 | + - name: Checkout code |
| 13 | + uses: actions/checkout@v3 |
| 14 | + with: |
| 15 | + fetch-depth: 2 |
| 16 | + |
| 17 | + - name: Set up Python |
| 18 | + uses: actions/setup-python@v4 |
| 19 | + with: |
| 20 | + python-version: '3.x' |
| 21 | + |
| 22 | + - name: Get current and previous version |
| 23 | + id: get_version |
| 24 | + run: | |
| 25 | + set -e |
| 26 | + CURR_VERSION=$(grep "version=" setup.py | head -1 | sed -E "s/.*version=['\"]([^'\"]*)['\"].*/\1/") |
| 27 | + PREV_VERSION=$(git show HEAD^:setup.py | grep "version=" | head -1 | sed -E "s/.*version=['\"]([^'\"]*)['\"].*/\1/") |
| 28 | + echo "Current version: $CURR_VERSION" |
| 29 | + echo "Previous version: $PREV_VERSION" |
| 30 | + echo "curr_version=$CURR_VERSION" >> $GITHUB_OUTPUT |
| 31 | + echo "prev_version=$PREV_VERSION" >> $GITHUB_OUTPUT |
| 32 | +
|
| 33 | + - name: Check if version changed |
| 34 | + id: version_check |
| 35 | + run: | |
| 36 | + if [ "${{ steps.get_version.outputs.curr_version }}" != "${{ steps.get_version.outputs.prev_version }}" ]; then |
| 37 | + echo "changed=true" >> $GITHUB_OUTPUT |
| 38 | + else |
| 39 | + echo "changed=false" >> $GITHUB_OUTPUT |
| 40 | + fi |
| 41 | +
|
| 42 | + - name: Create GitHub Release |
| 43 | + if: ${{ steps.version_check.outputs.changed == 'true' }} |
| 44 | + uses: softprops/action-gh-release@v1 |
| 45 | + with: |
| 46 | + tag_name: ${{ steps.get_version.outputs.curr_version }} |
| 47 | + name: Release ${{ steps.get_version.outputs.curr_version }} |
| 48 | + generate_release_notes: true |
| 49 | + env: |
| 50 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 51 | + |
| 52 | + - name: Install dependencies |
| 53 | + if: ${{ steps.version_check.outputs.changed == 'true' }} |
| 54 | + run: | |
| 55 | + python -m pip install --upgrade pip |
| 56 | + pip install setuptools wheel twine |
| 57 | +
|
| 58 | + - name: Build and publish |
| 59 | + if: ${{ steps.version_check.outputs.changed == 'true' }} |
| 60 | + env: |
| 61 | + TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} |
| 62 | + TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} |
| 63 | + run: | |
| 64 | + python setup.py sdist bdist_wheel |
| 65 | + twine upload dist/* |
0 commit comments