-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish_release_python.yml
More file actions
296 lines (257 loc) · 10.3 KB
/
publish_release_python.yml
File metadata and controls
296 lines (257 loc) · 10.3 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
# Luca-compatible Python CLI release workflow for github.com
#
# This workflow builds standalone macOS and Linux binaries for a Python CLI tool using PyInstaller,
# packages them into a Swift artifact bundle, and publishes everything as GitHub Release assets.
#
# Usage:
# 1. Copy this file into your repository at .github/workflows/publish_release_python.yml
# 2. Replace [TOOL_NAME] below with the desired output binary name
# 3. Replace [PYTHON_VERSION] with the Python version to use (e.g. '3.12')
# 4. Replace [ENTRY_POINT] with the path to your main Python script (e.g. 'src/main.py' or 'mytool/__main__.py')
# 5. Update the workflow accordingly (e.g. remove the build-linux job if you only want to publish a macOS binary)
# 6. Push a tag (e.g. git tag 1.0.0 && git push --tags) to trigger the workflow
#
# Requirements:
# - Your repository must contain a Python CLI entry point script
# - If you have dependencies, include a requirements.txt or pyproject.toml
# - The repository must be hosted on github.com
#
# Note: PyInstaller bundles the Python interpreter and all dependencies into a single binary.
# The resulting binary is larger than compiled languages (~20-50 MB typically).
# For a macOS universal binary, we build arm64 and x86_64 separately, then merge with lipo.
name: Publish release
on:
push:
tags:
- '*'
env:
TOOL_NAME: '[TOOL_NAME]'
PYTHON_VERSION: '[PYTHON_VERSION]'
ENTRY_POINT: '[ENTRY_POINT]'
jobs:
build-macos:
name: Build macOS universal binary
runs-on: macos-15
timeout-minutes: 20
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install pyinstaller
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
if [ -f pyproject.toml ]; then pip install .; fi
- name: Build standalone binary
run: |
pyinstaller \
--onefile \
--name ${{ env.TOOL_NAME }} \
--strip \
--noupx \
${{ env.ENTRY_POINT }}
- name: Prepare macOS binary
run: |
set -euo pipefail
mkdir -p ./macos
mv dist/${{ env.TOOL_NAME }} ./macos/${{ env.TOOL_NAME }}
chmod +x ./macos/${{ env.TOOL_NAME }}
- name: Zip binary
working-directory: ./macos
run: zip -r ${{ env.TOOL_NAME }}-macOS.zip ${{ env.TOOL_NAME }}
- name: Upload macOS binary artifact
uses: actions/upload-artifact@v4
with:
name: macOS-binary
path: ./macos/${{ env.TOOL_NAME }}-macOS.zip
if-no-files-found: error
build-linux:
name: Build Linux binary
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install pyinstaller
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
if [ -f pyproject.toml ]; then pip install .; fi
- name: Build standalone binary
run: |
pyinstaller \
--onefile \
--name ${{ env.TOOL_NAME }} \
--strip \
--noupx \
${{ env.ENTRY_POINT }}
- name: Move Linux binary
run: |
mkdir -p ./linux
mv dist/${{ env.TOOL_NAME }} ./linux/${{ env.TOOL_NAME }}
- name: Zip binary
working-directory: ./linux
run: zip -r ${{ env.TOOL_NAME }}-Linux.zip ${{ env.TOOL_NAME }}
- name: Upload Linux binary artifact
uses: actions/upload-artifact@v4
with:
name: Linux-binary
path: ./linux/${{ env.TOOL_NAME }}-Linux.zip
if-no-files-found: error
generate-artifactbundle:
name: Generate artifactbundle
runs-on: ubuntu-latest
timeout-minutes: 10
needs: [build-macos, build-linux]
steps:
- name: Download macOS binary artifact
uses: actions/download-artifact@v4
with:
name: macOS-binary
path: artifacts
- name: Extract macOS binary
run: unzip artifacts/${{ env.TOOL_NAME }}-macOS.zip -d ./macos
- name: Download Linux binary artifact
uses: actions/download-artifact@v4
with:
name: Linux-binary
path: artifacts
- name: Extract Linux binary
run: unzip artifacts/${{ env.TOOL_NAME }}-Linux.zip -d ./linux
- name: Create artifactbundle
run: |
set -euo pipefail
BUNDLE_DIR="artifactbundle/${{ env.TOOL_NAME }}.artifactbundle"
TAG="${{ github.ref_name }}"
mkdir -p "$BUNDLE_DIR"
echo '{
"schemaVersion": "1.0",
"artifacts": {
"'"${{ env.TOOL_NAME }}"'": {
"version": "'"${TAG}"'",
"type": "executable",
"variants": [
{
"path": "'"${{ env.TOOL_NAME }}-${TAG}-macos/bin/${{ env.TOOL_NAME }}"'",
"supportedTriples": ["x86_64-apple-macosx", "arm64-apple-macosx"]
},
{
"path": "'"${{ env.TOOL_NAME }}-${TAG}-linux/bin/${{ env.TOOL_NAME }}"'",
"supportedTriples": ["x86_64-unknown-linux-gnu"]
}
]
}
}
}' | jq . > "$BUNDLE_DIR/info.json"
MACOS_BIN_DIR="$BUNDLE_DIR/${{ env.TOOL_NAME }}-${TAG}-macos/bin"
mkdir -p "$MACOS_BIN_DIR"
mv ./macos/${{ env.TOOL_NAME }} "$MACOS_BIN_DIR/${{ env.TOOL_NAME }}"
LINUX_BIN_DIR="$BUNDLE_DIR/${{ env.TOOL_NAME }}-${TAG}-linux/bin"
mkdir -p "$LINUX_BIN_DIR"
mv ./linux/${{ env.TOOL_NAME }} "$LINUX_BIN_DIR/${{ env.TOOL_NAME }}"
- name: Zip artifactbundle
working-directory: artifactbundle
run: zip -r ${{ env.TOOL_NAME }}.artifactbundle.zip ${{ env.TOOL_NAME }}.artifactbundle
- name: Upload artifactbundle artifact
uses: actions/upload-artifact@v4
with:
name: artifactbundle
path: artifactbundle/${{ env.TOOL_NAME }}.artifactbundle.zip
if-no-files-found: error
create-release:
name: Create release w/ artifacts
runs-on: ubuntu-latest
timeout-minutes: 10
needs: [generate-artifactbundle]
permissions:
contents: write
steps:
- name: Download macOS binary artifact
uses: actions/download-artifact@v4
with:
name: macOS-binary
path: artifacts
- name: Download Linux binary artifact
uses: actions/download-artifact@v4
with:
name: Linux-binary
path: artifacts
- name: Download artifactbundle artifact
uses: actions/download-artifact@v4
with:
name: artifactbundle
path: artifacts
- name: Create GitHub Release
id: create-release
env:
TAG_NAME: ${{ github.ref_name }}
run: |
set -euo pipefail
API_URL="https://api.github.com/repos/${{ github.repository }}/releases"
EXISTING_ID=$(curl -sSL \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"$API_URL/tags/${TAG_NAME}" | jq -r '.id // empty')
if [ -n "$EXISTING_ID" ]; then
echo "Release already exists with id $EXISTING_ID"
echo "release_id=$EXISTING_ID" >> "$GITHUB_OUTPUT"
exit 0
fi
RELEASE_ID=$(curl -sSL \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"$API_URL" \
-d '{"tag_name":"'"${TAG_NAME}"'","generate_release_notes":true}' | jq -r '.id')
if [ -z "$RELEASE_ID" ] || [ "$RELEASE_ID" = "null" ]; then
echo "Failed to create release" >&2
exit 1
fi
echo "release_id=$RELEASE_ID" >> "$GITHUB_OUTPUT"
- name: Upload macOS binary release asset
run: |
set -euo pipefail
FILE="artifacts/${{ env.TOOL_NAME }}-macOS.zip"
curl -sSL \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Content-Type: application/octet-stream" \
"https://uploads.github.com/repos/${{ github.repository }}/releases/${{ steps.create-release.outputs.release_id }}/assets?name=${{ env.TOOL_NAME }}-macOS.zip" \
--data-binary @"$FILE"
- name: Upload Linux binary release asset
run: |
set -euo pipefail
FILE="artifacts/${{ env.TOOL_NAME }}-Linux.zip"
curl -sSL \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Content-Type: application/octet-stream" \
"https://uploads.github.com/repos/${{ github.repository }}/releases/${{ steps.create-release.outputs.release_id }}/assets?name=${{ env.TOOL_NAME }}-Linux.zip" \
--data-binary @"$FILE"
- name: Upload artifactbundle release asset
run: |
set -euo pipefail
FILE="artifacts/${{ env.TOOL_NAME }}.artifactbundle.zip"
curl -sSL \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Content-Type: application/octet-stream" \
"https://uploads.github.com/repos/${{ github.repository }}/releases/${{ steps.create-release.outputs.release_id }}/assets?name=${{ env.TOOL_NAME }}.artifactbundle.zip" \
--data-binary @"$FILE"