Skip to content

Commit c3535df

Browse files
committed
create workflow
1 parent 8d09ffc commit c3535df

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

.github/workflows/release.yml

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
name: release
2+
3+
on:
4+
push:
5+
tags: # regex for version tags according to python's pep 0440 std version convention https://peps.python.org/pep-0440/#version-specifiers
6+
- "[0-9]+.[0-9]+.[0-9]+"
7+
- "[0-9]+.[0-9]+.[0-9]+a[0-9]+"
8+
- "[0-9]+.[0-9]+.[0-9]+b[0-9]+"
9+
- "[0-9]+.[0-9]+.[0-9]+rc[0-9]+"
10+
11+
env:
12+
PACKAGE_NAME: "spicecode"
13+
OWNER: "spicecodecli"
14+
15+
jobs:
16+
details:
17+
runs-on: ubuntu-latest
18+
outputs:
19+
new_version: ${{ steps.release.outputs.new_version }}
20+
suffix: ${{ steps.release.outputs.suffix }}
21+
tag_name: ${{ steps.release.outputs.tag_name }}
22+
steps:
23+
- uses: actions/checkout@v2
24+
25+
- name: Extract tag and Details
26+
id: release
27+
run: |
28+
if [ "${{ github.ref_type }}" = "tag" ]; then
29+
TAG_NAME=${GITHUB_REF#refs/tags/}
30+
NEW_VERSION=$(echo $TAG_NAME | awk -F'-' '{print $1}')
31+
SUFFIX=$(echo $TAG_NAME | grep -oP '[a-z]+[0-9]+' || echo "")
32+
echo "new_version=$NEW_VERSION" >> "$GITHUB_OUTPUT"
33+
echo "suffix=$SUFFIX" >> "$GITHUB_OUTPUT"
34+
echo "tag_name=$TAG_NAME" >> "$GITHUB_OUTPUT"
35+
echo "Version is $NEW_VERSION"
36+
echo "Suffix is $SUFFIX"
37+
echo "Tag name is $TAG_NAME"
38+
else
39+
echo "No tag found"
40+
exit 1
41+
fi
42+
43+
check_pypi:
44+
needs: details
45+
runs-on: ubuntu-latest
46+
steps:
47+
- name: Fetch information from PyPI
48+
run: |
49+
response=$(curl -s https://pypi.org/pypi/${{ env.PACKAGE_NAME }}/json || echo "{}")
50+
latest_previous_version=$(echo $response | jq --raw-output "select(.releases != null) | .releases | keys_unsorted | last")
51+
if [ -z "$latest_previous_version" ]; then
52+
echo "Package not found on PyPI."
53+
latest_previous_version="0.0.0"
54+
fi
55+
echo "Latest version on PyPI: $latest_previous_version"
56+
echo "latest_previous_version=$latest_previous_version" >> $GITHUB_ENV
57+
58+
- name: Compare versions and exit if not newer
59+
run: |
60+
NEW_VERSION=${{ needs.details.outputs.new_version }}
61+
LATEST_VERSION=$latest_previous_version
62+
if [ "$(printf '%s\n' "$LATEST_VERSION" "$NEW_VERSION" | sort -rV | head -n 1)" != "$NEW_VERSION" ] || [ "$NEW_VERSION" == "$LATEST_VERSION" ]; then
63+
echo "The new version $NEW_VERSION is not greater than the latest version $LATEST_VERSION on PyPI."
64+
exit 1
65+
else
66+
echo "The new version $NEW_VERSION is greater than the latest version $LATEST_VERSION on PyPI."
67+
fi
68+
69+
setup_and_build:
70+
needs: [details, check_pypi]
71+
runs-on: ubuntu-latest
72+
steps:
73+
- uses: actions/checkout@v2
74+
75+
- name: Set up Python
76+
uses: actions/setup-python@v4
77+
with:
78+
python-version: "3.12"
79+
80+
- name: Install build dependencies
81+
run: |
82+
python -m pip install --upgrade pip
83+
pip install wheel setuptools build
84+
85+
- name: Update version in setup.py
86+
run: |
87+
# Replace version in setup.py with the new version from tag
88+
sed -i "s/version=\"[0-9]*\.[0-9]*\.[0-9]*\"/version=\"${{ needs.details.outputs.new_version }}\"/g" setup.py
89+
cat setup.py
90+
91+
- name: Install dependencies
92+
run: |
93+
pip install -r requirements.txt
94+
95+
- name: Build source and wheel distribution
96+
run: |
97+
python setup.py sdist bdist_wheel
98+
99+
- name: Upload artifacts
100+
uses: actions/upload-artifact@v3
101+
with:
102+
name: dist
103+
path: dist/
104+
105+
pypi_publish:
106+
name: Upload release to PyPI
107+
needs: [setup_and_build, details]
108+
runs-on: ubuntu-latest
109+
environment:
110+
name: release
111+
permissions:
112+
id-token: write
113+
steps:
114+
- name: Download artifacts
115+
uses: actions/download-artifact@v3
116+
with:
117+
name: dist
118+
path: dist/
119+
120+
- name: Publish distribution to PyPI
121+
uses: pypa/gh-action-pypi-publish@release/v1
122+
123+
github_release:
124+
name: Create GitHub Release
125+
needs: [setup_and_build, details]
126+
runs-on: ubuntu-latest
127+
permissions:
128+
contents: write
129+
steps:
130+
- name: Checkout Code
131+
uses: actions/checkout@v3
132+
with:
133+
fetch-depth: 0
134+
135+
- name: Download artifacts
136+
uses: actions/download-artifact@v3
137+
with:
138+
name: dist
139+
path: dist/
140+
141+
- name: Create GitHub Release
142+
id: create_release
143+
env:
144+
GH_TOKEN: ${{ github.token }}
145+
run: |
146+
gh release create ${{ needs.details.outputs.tag_name }} dist/* --title ${{ needs.details.outputs.tag_name }} --generate-notes

0 commit comments

Comments
 (0)