-
Notifications
You must be signed in to change notification settings - Fork 1
437 lines (390 loc) · 15.1 KB
/
cd.yml
File metadata and controls
437 lines (390 loc) · 15.1 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# .github/workflows/cd.yml
name: treemapper CD
permissions: {}
concurrency:
group: release
cancel-in-progress: false
'on':
workflow_dispatch:
inputs:
version:
description: 'Version to release (e.g., 1.0.0)'
required: true
publish_to_pypi:
description: 'Publish to PyPI'
required: true
default: 'false'
type: choice
options:
- 'true'
- 'false'
jobs:
prepare-version:
name: Prepare Version Commit
runs-on: ubuntu-latest
outputs:
version: ${{ steps.set_outputs.outputs.version }}
tag_name: ${{ steps.set_outputs.outputs.tag_name }}
commit_sha: ${{ steps.commit_version.outputs.commit_sha }}
steps:
- name: Checkout Code
uses: actions/checkout@v6
with:
fetch-depth: 0 # Need full history for git bundle
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.11'
- name: Check that we're on main branch
env:
CURRENT_BRANCH: ${{ github.ref_name }}
run: |
if [ "$CURRENT_BRANCH" != "main" ]; then
echo "Error: Releases can only be created from the main branch. Current branch: $CURRENT_BRANCH"
exit 1
fi
- name: Validate version format (PEP 440)
env:
VERSION: ${{ github.event.inputs.version }}
run: |
# PEP 440: X.Y.Z with optional pre-release (a1, b1, rc1) or dev/post suffix
if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+((a|b|rc)[0-9]+)?(\.dev[0-9]+)?(\.post[0-9]+)?$ ]]; then
echo "Error: Invalid version format '$VERSION'."
echo "Expected PEP 440 format: 1.0.0, 1.0.0a1, 1.0.0b1, 1.0.0rc1, 1.0.0.dev1, 1.0.0.post1"
exit 1
fi
echo "Version format valid (PEP 440): $VERSION"
- name: Check version is not already set
env:
VERSION: ${{ github.event.inputs.version }}
run: |
CURRENT=$(python - <<'PY'
import re
content = open('src/treemapper/version.py').read()
m = re.search(r'__version__\s*=\s*["\']([^"\']+)["\']', content)
print(m.group(1) if m else '')
PY
)
if [ "$CURRENT" = "$VERSION" ]; then
echo "Error: version.py already contains version $VERSION"
echo "Nothing to release - version is already set."
exit 1
fi
echo "Current version: $CURRENT -> New version: $VERSION"
- name: Set version in version.py
env:
VERSION: ${{ github.event.inputs.version }}
run: |
echo "Setting version to $VERSION"
python - <<'PY'
import os, re, pathlib
ver = os.environ["VERSION"]
p = pathlib.Path("src/treemapper/version.py")
s = p.read_text(encoding="utf-8")
s, n = re.subn(r'__version__\s*=\s*["\'].*?["\']', f'__version__ = "{ver}"', s, count=1)
assert n == 1, "version assignment not found or multiple matches"
p.write_text(s, encoding="utf-8")
PY
echo "version.py content after change:"
cat src/treemapper/version.py
- name: Commit version bump (locally only, no push yet)
id: commit_version
env:
VERSION: ${{ github.event.inputs.version }}
run: |
git config user.name "github-actions[bot]"
# 41898282 is GitHub's bot user ID for github-actions[bot]
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add src/treemapper/version.py
if ! git diff --staged --quiet; then
git commit -m "Release version ${VERSION}"
else
echo "No changes to commit."
fi
COMMIT_SHA=$(git rev-parse HEAD)
echo "Commit SHA: $COMMIT_SHA"
echo "commit_sha=$COMMIT_SHA" >> "$GITHUB_OUTPUT"
- name: Check tag doesn't already exist
env:
VERSION: ${{ github.event.inputs.version }}
run: |
TAG="v${VERSION}"
git fetch --tags origin 2>/dev/null || true
if git rev-parse "$TAG" >/dev/null 2>&1; then
echo "Error: Tag $TAG already exists locally"
exit 1
fi
if git ls-remote --tags origin | grep -q "refs/tags/$TAG$"; then
echo "Error: Tag $TAG already exists on remote"
exit 1
fi
echo "Tag $TAG does not exist, proceeding..."
- name: Create local tag (no push yet)
env:
VERSION: ${{ github.event.inputs.version }}
run: |
git tag -a "v${VERSION}" -m "Release version ${VERSION}"
echo "Tag created locally: v${VERSION}"
- name: Upload git bundle for other jobs
run: |
git bundle create repo.bundle --all
- name: Upload bundle as artifact
uses: actions/upload-artifact@v7.0.1
with:
name: git-repo-bundle
path: repo.bundle
retention-days: 1
- name: Set outputs
id: set_outputs
env:
VERSION: ${{ github.event.inputs.version }}
run: |
echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
echo "tag_name=v${VERSION}" >> "$GITHUB_OUTPUT"
build-assets:
name: Build Assets
needs: prepare-version
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
asset_name: linux
python-version: '3.11'
- os: macos-latest
asset_name: macos
python-version: '3.11'
- os: windows-latest
asset_name: windows
python-version: '3.11'
runs-on: ${{ matrix.os }}
steps:
- name: Download git bundle
uses: actions/download-artifact@v8.0.1
with:
name: git-repo-bundle
- name: Restore repository from bundle
shell: bash
env:
TAG_NAME: ${{ needs.prepare-version.outputs.tag_name }}
run: |
git clone repo.bundle repo
cd repo
git checkout "$TAG_NAME"
# Debug: verify we're in the right state
echo "Current directory: $(pwd)"
echo "Current tag: $(git describe --tags --exact-match 2>/dev/null || echo 'no tag')"
echo "Current commit: $(git rev-parse HEAD)"
echo "Version file content:"
cat src/treemapper/version.py || echo "ERROR: version.py not found!"
echo "Directory structure:"
ls -la src/treemapper/ || echo "ERROR: src/treemapper not found!"
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
cache-dependency-path: './repo/pyproject.toml'
- name: Install Dependencies (including PyInstaller)
shell: bash
working-directory: ./repo
env:
EXPECTED_VERSION: ${{ needs.prepare-version.outputs.version }}
run: |
python -m pip install --upgrade pip
# Install in editable mode to ensure version.py changes are picked up
pip install -e .[dev]
# Verify the installed version matches what we expect
echo "Verifying installed version:"
python -c "from treemapper.version import __version__; print(f'Version in module: {__version__}')"
echo "Expected version: ${EXPECTED_VERSION}"
- name: Build with PyInstaller
shell: bash
working-directory: ./repo
env:
ASSET_DIR: ${{ matrix.asset_name }}
run: |
# Ensure PyInstaller can find the modules (use correct path separator for OS)
SEP=$(python -c "import os; print(os.pathsep)")
export PYTHONPATH="${PWD}/src${SEP}${PYTHONPATH:-}"
echo "PYTHONPATH: $PYTHONPATH"
# Run PyInstaller with explicit paths
python -m PyInstaller --clean -y --distpath "./dist/${ASSET_DIR}" treemapper.spec
# Verify the built executable exists (cross-platform)
echo "Checking for built executable in: ./dist/${ASSET_DIR}/"
ls -la "./dist/${ASSET_DIR}/" || echo "ERROR: dist directory not found!"
- name: Determine architecture
id: arch
shell: bash
env:
RUNNER_PLATFORM: ${{ runner.os }}
RUNNER_ARCHITECTURE: ${{ runner.arch }}
run: |
ARCH=$(uname -m)
if [[ "$RUNNER_PLATFORM" == "Windows" ]]; then
if [[ "$RUNNER_ARCHITECTURE" == "X64" ]]; then ARCH="x86_64"; \
elif [[ "$RUNNER_ARCHITECTURE" == "ARM64" ]]; then ARCH="arm64"; \
else ARCH="unknown"; fi
elif [[ "$RUNNER_PLATFORM" == "macOS" ]] && [[ "$ARCH" == "arm64" ]]; then
echo "Detected ARM on macOS"
fi
echo "Determined ARCH: $ARCH"
echo "arch=$ARCH" >> "$GITHUB_OUTPUT"
- name: Rename asset with proper name
shell: bash
working-directory: ./repo
env:
ASSET_DIR: ${{ matrix.asset_name }}
ARCH: ${{ steps.arch.outputs.arch }}
VERSION: ${{ needs.prepare-version.outputs.version }}
RUNNER_PLATFORM: ${{ runner.os }}
run: |
ASSET_NAME="treemapper-${ASSET_DIR}-${ARCH}-${VERSION}"
if [[ "$RUNNER_PLATFORM" == "Windows" ]]; then
ASSET_NAME="${ASSET_NAME}.exe"
mv "./dist/${ASSET_DIR}/treemapper.exe" "./dist/${ASSET_NAME}"
else
mv "./dist/${ASSET_DIR}/treemapper" "./dist/${ASSET_NAME}"
fi
- name: Upload artifact
uses: actions/upload-artifact@v7.0.1
with:
name: ${{ matrix.asset_name }}-binary
path: ./repo/dist/treemapper-*
retention-days: 1
publish-to-pypi:
name: Publish to PyPI
needs: [prepare-version, build-assets]
if: github.event.inputs.publish_to_pypi == 'true'
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/p/treemapper
permissions:
id-token: write
steps:
- name: Download git bundle
uses: actions/download-artifact@v8.0.1
with:
name: git-repo-bundle
- name: Restore repository from bundle
shell: bash
env:
TAG_NAME: ${{ needs.prepare-version.outputs.tag_name }}
run: |
git clone repo.bundle repo
cd repo
git checkout "$TAG_NAME"
# Debug: verify we're in the right state
echo "Current directory: $(pwd)"
echo "Current tag: $(git describe --tags --exact-match 2>/dev/null || echo 'no tag')"
echo "Current commit: $(git rev-parse HEAD)"
echo "Version file content:"
cat src/treemapper/version.py || echo "ERROR: version.py not found!"
echo "Directory structure:"
ls -la src/treemapper/ || echo "ERROR: src/treemapper not found!"
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.11'
- name: Install build tools
working-directory: ./repo
env:
EXPECTED_VERSION: ${{ needs.prepare-version.outputs.version }}
run: |
python -m pip install --upgrade pip
pip install build
# Verify version.py has the correct version
echo "Version in version.py:"
cat src/treemapper/version.py
echo "Expected version: ${EXPECTED_VERSION}"
- name: Build sdist and wheel
working-directory: ./repo
env:
VERSION: ${{ needs.prepare-version.outputs.version }}
run: |
# Build the distribution packages
python -m build
# Verify the built packages have correct version in filename
echo "Built packages:"
ls -la dist/
if [ -z "$VERSION" ]; then
echo "ERROR: VERSION is empty"
exit 1
fi
echo "Looking for version ${VERSION} in package names..."
ls dist/*"${VERSION}"* || echo "WARNING: No packages found!"
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@cef221092ed1bacb1cc03d23a2d87d1d172e277b # release/v1
with:
packages-dir: ./repo/dist/
print-hash: true
finalize-release:
name: Push Release and Create GitHub Release
needs: [prepare-version, build-assets, publish-to-pypi]
if: |
needs.prepare-version.result == 'success' &&
needs.build-assets.result == 'success' &&
(needs.publish-to-pypi.result == 'success' ||
needs.publish-to-pypi.result == 'skipped')
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Download git bundle
uses: actions/download-artifact@v8.0.1
with:
name: git-repo-bundle
- name: Restore repository from bundle
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMIT_SHA: ${{ needs.prepare-version.outputs.commit_sha }}
REPO_FULL_NAME: ${{ github.repository }}
run: |
git clone repo.bundle repo
cd repo
# Checkout the version commit (not old main) to preserve version bump
git checkout "$COMMIT_SHA"
echo "Current commit: $(git rev-parse HEAD)"
echo "Expected commit: ${COMMIT_SHA}"
# Set up remote to point to the actual GitHub repository
git remote set-url origin "https://x-access-token:${GH_TOKEN}@github.com/${REPO_FULL_NAME}.git"
- name: Push commit and tag to main
working-directory: ./repo
env:
TAG_NAME: ${{ needs.prepare-version.outputs.tag_name }}
run: |
git config user.name "github-actions[bot]"
# 41898282 is GitHub's bot user ID for github-actions[bot]
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
# Fetch latest state of remote main
git fetch origin main
# Check that remote main hasn't moved since we started the release
# Our commit should be based on the current remote main HEAD
REMOTE_MAIN=$(git rev-parse origin/main)
OUR_PARENT=$(git rev-parse HEAD^)
if [ "$REMOTE_MAIN" != "$OUR_PARENT" ]; then
echo "Error: Remote main has changed since release started."
echo "Expected parent: $OUR_PARENT"
echo "Remote main: $REMOTE_MAIN"
echo "Please re-run the release workflow."
exit 1
fi
# Push the version bump commit to main
git push origin HEAD:main
# Push the tag
git push origin "$TAG_NAME"
echo "Successfully pushed version commit and tag"
- name: Download all build artifacts
uses: actions/download-artifact@v8.0.1
with:
path: ./artifacts
- name: Create GitHub Release with assets
uses: softprops/action-gh-release@b4309332981a82ec1c5618f44dd2e27cc8bfbfda # v2
with:
tag_name: ${{ needs.prepare-version.outputs.tag_name }}
name: Release ${{ needs.prepare-version.outputs.version }}
draft: false
prerelease: false
generate_release_notes: true
files: ./artifacts/**/*-binary/treemapper-*