-
Notifications
You must be signed in to change notification settings - Fork 1
279 lines (245 loc) · 8.78 KB
/
Copy pathcd.yml
File metadata and controls
279 lines (245 loc) · 8.78 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
# .github/workflows/cd.yml
name: TreeMapper CD
permissions:
contents: write
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@v5
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.11'
- name: Check that we're on main branch
run: |
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$CURRENT_BRANCH" != "main" ]; then
echo "Error: Releases can only be created from the main branch. Current branch: $CURRENT_BRANCH"
exit 1
fi
- name: Set version in version.py
run: |
VERSION="${{ github.event.inputs.version }}"
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
run: |
git config user.name 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 ${{ github.event.inputs.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: Create local tag (no push yet)
run: |
git tag -a "v${{ github.event.inputs.version }}" -m "Release version ${{ github.event.inputs.version }}"
echo "Tag created locally: v${{ github.event.inputs.version }}"
- name: Upload git bundle for other jobs
run: |
git bundle create repo.bundle --all
- name: Upload bundle as artifact
uses: actions/upload-artifact@v4
with:
name: git-repo-bundle
path: repo.bundle
retention-days: 1
- name: Set outputs
id: set_outputs
run: |
echo "version=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT
echo "tag_name=v${{ github.event.inputs.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@v5
with:
name: git-repo-bundle
- name: Restore repository from bundle
run: |
git clone repo.bundle repo
cd repo
git checkout ${{ needs.prepare-version.outputs.tag_name }}
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: ${{ matrix.python-version }}
- name: Cache pip Dependencies
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ hashFiles('**/pyproject.toml') }}
restore-keys: |
${{ runner.os }}-pip-${{ matrix.python-version }}-
${{ runner.os }}-pip-
- name: Install Dependencies (including PyInstaller)
working-directory: ./repo
run: |
python -m pip install --upgrade pip
pip install .[dev]
- name: Build with PyInstaller
working-directory: ./repo
run: |
python -m PyInstaller --clean -y --dist ./dist/${{ matrix.asset_name }} treemapper.spec
- name: Determine architecture
id: arch
shell: bash
run: |
ARCH=$(uname -m)
if [[ "${{ runner.os }}" == "Windows" ]]; then
if [[ "${{ runner.arch }}" == "X64" ]]; then ARCH="x86_64"; \
elif [[ "${{ runner.arch }}" == "ARM64" ]]; then ARCH="arm64"; \
else ARCH="unknown"; fi
elif [[ "${{ runner.os }}" == "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
run: |
ASSET_NAME="treemapper-${{ matrix.asset_name }}-${{ steps.arch.outputs.arch }}"
ASSET_NAME="${ASSET_NAME}-v${{ needs.prepare-version.outputs.version }}"
if [[ "${{ runner.os }}" == "Windows" ]]; then
ASSET_NAME="${ASSET_NAME}.exe"
mv "./dist/${{ matrix.asset_name }}/treemapper.exe" "./dist/${ASSET_NAME}"
else
mv "./dist/${{ matrix.asset_name }}/treemapper" "./dist/${ASSET_NAME}"
fi
- name: Upload artifact
uses: actions/upload-artifact@v4
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@v5
with:
name: git-repo-bundle
- name: Restore repository from bundle
run: |
git clone repo.bundle repo
cd repo
git checkout ${{ needs.prepare-version.outputs.tag_name }}
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.11'
- name: Install build tools
working-directory: ./repo
run: |
python -m pip install --upgrade pip
pip install build
- name: Build sdist and wheel
working-directory: ./repo
run: python -m build
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: ./repo/dist/
finalize-release:
name: Push Release and Create GitHub Release
needs: [prepare-version, build-assets, publish-to-pypi]
if: |
always() &&
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
steps:
- name: Download git bundle
uses: actions/download-artifact@v5
with:
name: git-repo-bundle
- name: Restore repository from bundle
run: |
git clone repo.bundle repo
cd repo
- name: Push commit and tag to main
working-directory: ./repo
run: |
git config user.name github-actions[bot]
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
# Push the version bump commit to main
git push origin HEAD:main
# Push the tag
git push origin ${{ needs.prepare-version.outputs.tag_name }}
echo "Successfully pushed version commit and tag"
- name: Download all build artifacts
uses: actions/download-artifact@v5
with:
path: ./artifacts
- name: Create GitHub Release with assets
uses: softprops/action-gh-release@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-*