Skip to content

Commit

Permalink
chore: update release.yml (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
Snoopy1866 authored Nov 27, 2024
1 parent 0cd15d8 commit b11ee69
Showing 1 changed file with 16 additions and 91 deletions.
107 changes: 16 additions & 91 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,67 +1,18 @@
name: Build, Release, and Publish
name: Publish Python 🐍 distribution 📦 to PyPI and TestPyPI

on:
workflow_dispatch:

permissions:
contents: write
packages: write

jobs:
build:
name: Build distribution 📦
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better release experience

- name: Extract Current Version Number
id: extract_current_version
run: |
CURRENT_VERSION=$(grep '__version__' src/pystatpower/__init__.py | cut -d '"' -f 2)
echo "CURRENT_VERSION=$CURRENT_VERSION" >> $GITHUB_OUTPUT
- name: Get Previous Version Number
id: get_previous_version
run: |
PREVIOUS_VERSION=$(git describe --tags --abbrev=0 2>/dev/null || echo "0.0.0")
PREVIOUS_VERSION=$(echo "$PREVIOUS_VERSION" | xargs) # 去除可能的空格
echo "PREVIOUS_VERSION=$PREVIOUS_VERSION" >> $GITHUB_OUTPUT
- name: Compare Versions
id: compare_versions
run: |
CURRENT_VERSION="${{ steps.extract_current_version.outputs.CURRENT_VERSION }}"
PREVIOUS_VERSION="${{ steps.get_previous_version.outputs.PREVIOUS_VERSION }}"
python - <<EOF
import sys
from packaging import version
try:
current_version = version.parse("${CURRENT_VERSION}")
previous_version = version.parse("${PREVIOUS_VERSION}")
if current_version > previous_version:
print(f"Version increased from {previous_version} to {current_version}. Continuing the workflow.")
else:
print(f"Version did not increase. Stopping the workflow.")
sys.exit(1)
except Exception as e:
print(f"Error comparing versions: {e}")
sys.exit(1)
EOF
- name: Determine Release Type
id: determine_release_type
run: |
VERSION="${{ steps.extract_current_version.outputs.CURRENT_VERSION }}"
if [[ "$VERSION" =~ [abrc] ]]; then
echo "IS_PRERELEASE=true" >> $GITHUB_OUTPUT
else
echo "IS_PRERELEASE=false" >> $GITHUB_OUTPUT
fi
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v5
Expand All @@ -76,54 +27,28 @@ jobs:
- name: Build package
run: python -m build

- name: Upload distribution
- name: Upload the distribution packages
uses: actions/upload-artifact@v4
with:
name: distribution
path: dist/*

- name: Create Tag
run: |
git config --local user.name "github-actions[bot]"
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git tag ${{ steps.extract_current_version.outputs.CURRENT_VERSION }}
git push origin ${{ steps.extract_current_version.outputs.CURRENT_VERSION }}
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.extract_current_version.outputs.CURRENT_VERSION }}
release_name: ${{ steps.extract_current_version.outputs.CURRENT_VERSION }}
draft: false
prerelease: ${{ steps.determine_release_type.outputs.IS_PRERELEASE }}
path: dist/

- name: Upload Release Asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: dist/pystatpower-${{ steps.extract_current_version.outputs.CURRENT_VERSION }}.tar.gz
asset_name: ${{ steps.extract_current_version.outputs.CURRENT_VERSION }}.tar.gz
asset_content_type: application/gzip

pypi-publish:
publish-to-pypi:
name: Publish Python 🐍 distribution 📦 to PyPI
if: startsWith(github.ref, 'refs/tags/')
needs:
- build
runs-on: ubuntu-latest
needs: build
environment:
name: release
url: https://pypi.org/p/pystatpower
permissions:
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
id-token: write # IMPORTANT: mandatory for trusted publishing
steps:
- name: Download distribution
- name: Download all the dists
uses: actions/download-artifact@v4
with:
name: distribution
path: dist

- name: Publish package distributions to PyPI
uses: pypa/[email protected]
name: python-package-distributions
path: dist/
- name: Publish distribution 📦 to PyPI
uses: pypa/gh-action-pypi-publish@release/v1

0 comments on commit b11ee69

Please sign in to comment.