-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6bbf870
commit 348b2d7
Showing
1 changed file
with
17 additions
and
92 deletions.
There are no files selected for viewing
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
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 | ||
|
@@ -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/') # only publish to PyPI on tag pushes | ||
needs: | ||
- build | ||
runs-on: ubuntu-latest | ||
needs: build | ||
environment: | ||
name: release | ||
url: https://pypi.org/p/pystatpower | ||
url: https://pypi.org/p/pystatpower # Replace <package-name> with your PyPI project name | ||
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 |