Publish #7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build, Release, and Publish | |
on: | |
workflow_dispatch: | |
permissions: | |
contents: write | |
packages: write | |
jobs: | |
build: | |
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 | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: "3.12" | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install build | |
- name: Build package | |
run: python -m build | |
- name: Upload distribution | |
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 }} | |
- 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: | |
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 | |
steps: | |
- name: Download distribution | |
uses: actions/download-artifact@v4 | |
with: | |
name: distribution | |
path: dist | |
- name: Publish package distributions to PyPI | |
uses: pypa/[email protected] |