Skip to content

Commit cd6d278

Browse files
committed
fix: escaped variables in shell script for PyPI workflow
1 parent f169968 commit cd6d278

1 file changed

Lines changed: 47 additions & 79 deletions

File tree

‎.github/workflows/publish-pypi.yml‎

Lines changed: 47 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,99 +6,67 @@ on:
66
workflow_dispatch:
77

88
jobs:
9-
publish-pypi:
10-
name: Build & publish (PyPI)
9+
publish:
1110
runs-on: ubuntu-latest
12-
defaults:
13-
run:
14-
working-directory: python
15-
environment:
16-
name: pypi
17-
url: https://pypi.org/project/acedatacloud-x402/
18-
permissions:
19-
contents: write
20-
id-token: write
2111
steps:
22-
- name: Checkout
23-
uses: actions/checkout@v5
12+
- uses: actions/checkout@v4
13+
- uses: actions/setup-python@v5
2414
with:
25-
fetch-depth: 0
26-
27-
- name: Setup Python
28-
uses: actions/setup-python@v5
29-
with:
30-
python-version: "3.11"
31-
32-
- name: Install build tools
33-
run: |
34-
python -m pip install --upgrade pip
35-
pip install build hatchling
15+
python-version: '3.11'
3616

3717
- name: Compute CalVer version
38-
id: version
39-
shell: python
18+
id: calver
19+
env:
20+
PKG_NAME: acedatacloud-x402
4021
run: |
41-
import json, os, urllib.request, datetime, subprocess
42-
name = "acedatacloud-x402"
43-
prefix = datetime.datetime.now(datetime.timezone.utc).strftime("%Y.%-m.%-d")
44-
print(f"Package: {name}")
45-
print(f"Date prefix (UTC): {prefix}")
22+
DATE_PREFIX=$(date -u +'%Y.%-m.%-d')
23+
echo "Package: $PKG_NAME"
24+
echo "Date prefix (UTC): $DATE_PREFIX"
25+
VERSION=$(python3 -c "
26+
import json, os, urllib.request
27+
name = os.environ['PKG_NAME']
28+
prefix = os.environ['DATE_PREFIX']
4629
try:
47-
with urllib.request.urlopen(f"https://pypi.org/pypi/{name}/json", timeout=10) as r:
48-
versions = list(json.load(r).get("releases", {}).keys())
49-
except Exception as e:
50-
print(f"Error fetching from PyPI: {e}")
30+
with urllib.request.urlopen(f'https://pypi.org/pypi/{name}/json', timeout=10) as r:
31+
versions = list(json.load(r).get('releases', {}).keys())
32+
except Exception:
5133
versions = []
5234
53-
same = [v for v in versions if v == prefix or v.startswith(prefix + ".")]
35+
same = [v for v in versions if v == prefix or v.startswith(prefix + '.')]
5436
if not same:
55-
version = prefix
56-
else:
57-
max_n = 0
58-
for v in same:
59-
if v == prefix:
60-
if max_n < 1: max_n = 1
61-
continue
62-
tail = v[len(prefix) + 1:]
63-
try:
64-
n = int(tail)
65-
if n >= max_n: max_n = n + 1
66-
except ValueError:
67-
pass
68-
version = f"{prefix}.{max_n}"
37+
print(prefix); exit(0)
6938
70-
print(f"Resolved version: {version}")
71-
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
72-
f.write(f"version={version}\n")
39+
max_n = 0
40+
for v in same:
41+
if v == prefix:
42+
if max_n < 1: max_n = 1
43+
continue
44+
tail = v[len(prefix) + 1:]
45+
try:
46+
n = int(tail)
47+
if n >= max_n: max_n = n + 1
48+
except ValueError:
49+
pass
50+
print(f'{prefix}.{max_n}')
51+
")
52+
echo "Resolved version: $VERSION"
53+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
7354
74-
- name: Stamp version into pyproject.toml
75-
env:
76-
VERSION: ${{ steps.version.outputs.version }}
55+
- name: Update version in pyproject.toml
7756
run: |
78-
python3 -c "
79-
import os, re, pathlib
80-
version = os.environ['VERSION']
81-
p = pathlib.Path('pyproject.toml')
82-
text = p.read_text()
83-
text = re.sub(r'^version = \"[^\"]+\"', f'version = \"{version}\"', text, count=1, flags=re.M)
84-
p.write_text(text)
85-
"
86-
grep -m1 '^version' pyproject.toml
87-
88-
- name: Build distributions
89-
run: python3 -m build
57+
VERSION=${{ steps.calver.outputs.version }}
58+
sed -i "s/^version = \".*\"/version = \"$VERSION\"/" pyproject.toml
59+
cat pyproject.toml | grep "^version ="
9060
91-
- name: Publish to PyPI
92-
uses: pypa/gh-action-pypi-publish@release/v1
93-
with:
94-
packages-dir: python/dist/
61+
- name: Install dependencies
62+
run: |
63+
python -m pip install --upgrade pip
64+
pip install build twine
9565
96-
- name: Create Release
66+
- name: Build and publish
9767
env:
98-
GITHUB_TOKEN: ${{ github.token }}
99-
VERSION: ${{ steps.version.outputs.version }}
68+
TWINE_USERNAME: __token__
69+
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
10070
run: |
101-
gh release create "python-v$VERSION" python/dist/* \
102-
--title "Python v$VERSION" \
103-
--generate-notes \
104-
--repo "${{ github.repository }}" || echo "Release already exists, skipping"
71+
python -m build
72+
twine upload dist/*

0 commit comments

Comments
 (0)