-
-
Notifications
You must be signed in to change notification settings - Fork 57
149 lines (130 loc) · 4.19 KB
/
publish.yml
File metadata and controls
149 lines (130 loc) · 4.19 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
name: Auto Release
on:
push:
branches: [main]
paths:
- 'pyproject.toml'
workflow_dispatch:
permissions:
contents: write
id-token: write
jobs:
check-version:
runs-on: ubuntu-latest
outputs:
new_release: ${{ steps.check.outputs.new_release }}
version: ${{ steps.version.outputs.version }}
tag: ${{ steps.version.outputs.tag }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Extract version from pyproject.toml
id: version
run: |
VERSION=$(grep -m1 '^version' pyproject.toml | sed 's/version = "\(.*\)"/\1/')
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "tag=v$VERSION" >> $GITHUB_OUTPUT
- name: Check if tag exists
id: check
run: |
if git rev-parse "v${{ steps.version.outputs.version }}" >/dev/null 2>&1; then
echo "new_release=false" >> $GITHUB_OUTPUT
else
echo "new_release=true" >> $GITHUB_OUTPUT
fi
create-tag:
needs: check-version
if: needs.check-version.outputs.new_release == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Create and push tag
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag -a "${{ needs.check-version.outputs.tag }}" -m "Release ${{ needs.check-version.outputs.tag }}"
git push origin "${{ needs.check-version.outputs.tag }}"
create-release:
needs: [check-version, create-tag]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Extract changelog section
id: changelog
run: |
VERSION="${{ needs.check-version.outputs.version }}"
# 提取 CHANGELOG.md 中对应版本的内容
NOTES=$(awk "/^## \[$VERSION\]/{found=1; next} /^---$/{if(found) exit} /^## \[/{if(found) exit} found{print}" CHANGELOG.md)
# 写入文件避免转义问题
echo "$NOTES" > /tmp/release_notes.md
- name: Create GitHub Release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAG: ${{ needs.check-version.outputs.tag }}
run: |
gh release create "$TAG" \
--title "SimTradeLab $TAG" \
--notes-file /tmp/release_notes.md
build:
needs: [check-version, create-tag]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: latest
- name: Build release distributions
run: poetry build
- name: Upload distributions
uses: actions/upload-artifact@v4
with:
name: release-dists
path: dist/
publish:
needs: [check-version, build]
runs-on: ubuntu-latest
environment: pypi
permissions:
id-token: write
contents: read
steps:
- name: Retrieve release distributions
uses: actions/download-artifact@v4
with:
name: release-dists
path: dist/
- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
verify:
needs: [check-version, publish]
runs-on: ubuntu-latest
steps:
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Verify PyPI publication
env:
VERSION: ${{ needs.check-version.outputs.version }}
run: |
python -m pip install --upgrade pip
for i in 1 2 3 4 5 6; do
if pip install "simtradelab==$VERSION"; then
break
fi
if [ "$i" -eq 6 ]; then
echo "Failed to install simtradelab==$VERSION from PyPI after retries."
exit 1
fi
echo "Package not available yet on PyPI, retrying in 30s..."
sleep 30
done
python -c "import simtradelab; print('SimTradeLab ' + simtradelab.__version__ + ' installed successfully')"