-
Notifications
You must be signed in to change notification settings - Fork 72
181 lines (149 loc) · 5.24 KB
/
publish.yml
File metadata and controls
181 lines (149 loc) · 5.24 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
name: Build and publish package
on:
pull_request:
push:
branches:
- main
workflow_dispatch:
concurrency:
group: pypi-publish-${{ github.ref }}
cancel-in-progress: false
permissions:
contents: read
jobs:
build:
name: Test and build distribution
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install package and build tools
run: |
python -m pip install --upgrade pip
python -m pip install -e .
python -m pip install build twine
- name: Run tests
run: python -m django test tests --settings=tests.test_settings --verbosity=1
- name: Build package
run: python -m build --sdist --wheel
- name: Ensure wheel and source distribution exist
run: |
shopt -s nullglob
wheel_files=(dist/*.whl)
sdist_files=(dist/*.tar.gz)
if [ ${#wheel_files[@]} -eq 0 ]; then
echo "Wheel artifact is missing from dist/." >&2
exit 1
fi
if [ ${#sdist_files[@]} -eq 0 ]; then
echo "Source distribution artifact is missing from dist/." >&2
exit 1
fi
printf 'Wheel artifacts:\n'
printf '%s\n' "${wheel_files[@]}"
printf 'Source distribution artifacts:\n'
printf '%s\n' "${sdist_files[@]}"
- name: Validate package metadata
run: python -m twine check dist/*
- name: Upload package artifacts
uses: actions/upload-artifact@v4
with:
name: package-dist
path: dist/*
if-no-files-found: error
publish:
name: Publish to PyPI
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
environment: pypi
permissions:
contents: write
id-token: write
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install metadata tooling
run: python -m pip install --upgrade setuptools
- name: Read package metadata
id: package
run: |
package_name="$(python setup.py --name)"
package_version="$(python setup.py --version)"
if [ -z "$package_name" ] || [ -z "$package_version" ]; then
echo "Could not read package name/version from setup.py" >&2
exit 1
fi
echo "name=$package_name" >> "$GITHUB_OUTPUT"
echo "version=$package_version" >> "$GITHUB_OUTPUT"
echo "Package: $package_name $package_version"
- name: Ensure version is not already on PyPI
env:
PACKAGE_NAME: ${{ steps.package.outputs.name }}
PACKAGE_VERSION: ${{ steps.package.outputs.version }}
run: |
python - <<'PY'
import os
import sys
import urllib.error
import urllib.request
package_name = os.environ["PACKAGE_NAME"].replace("_", "-")
package_version = os.environ["PACKAGE_VERSION"]
if not package_name or not package_version:
raise SystemExit("PACKAGE_NAME and PACKAGE_VERSION must not be empty.")
url = f"https://pypi.org/pypi/{package_name}/{package_version}/json"
print(f"Checking {url}")
try:
urllib.request.urlopen(url, timeout=15)
except urllib.error.HTTPError as exc:
if exc.code == 404:
print(f"{package_name} {package_version} is not on PyPI yet.")
sys.exit(0)
raise
raise SystemExit(
f"{package_name} {package_version} already exists on PyPI. "
"Bump setup.py version before merging to main."
)
PY
- name: Download package artifacts
uses: actions/download-artifact@v4
with:
name: package-dist
path: dist
- name: Ensure downloaded wheel and source distribution exist
run: |
shopt -s nullglob
wheel_files=(dist/*.whl)
sdist_files=(dist/*.tar.gz)
if [ ${#wheel_files[@]} -eq 0 ]; then
echo "Wheel artifact is missing from downloaded dist/." >&2
exit 1
fi
if [ ${#sdist_files[@]} -eq 0 ]; then
echo "Source distribution artifact is missing from downloaded dist/." >&2
exit 1
fi
printf 'Publishing wheel artifacts:\n'
printf '%s\n' "${wheel_files[@]}"
printf 'Publishing source distribution artifacts:\n'
printf '%s\n' "${sdist_files[@]}"
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
- name: Create GitHub release
env:
GH_TOKEN: ${{ github.token }}
PACKAGE_NAME: ${{ steps.package.outputs.name }}
PACKAGE_VERSION: ${{ steps.package.outputs.version }}
run: |
gh release create "$PACKAGE_VERSION" dist/* \
--target "$GITHUB_SHA" \
--title "$PACKAGE_NAME $PACKAGE_VERSION" \
--generate-notes