-
Notifications
You must be signed in to change notification settings - Fork 0
137 lines (116 loc) · 4.63 KB
/
release.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# This workflow will upload a Python Package using Twine when a release is created
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
name: Build and Upload Python Package
on:
workflow_dispatch:
permissions:
contents: read
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 user.name "GitHub Actions"
git config user.email "[email protected]"
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/*.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]