-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
497 lines (425 loc) · 16.8 KB
/
python-publish.yml
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
name: Test and Release
on:
push:
branches:
- main
- master
permissions:
id-token: write
contents: write
pull-requests: write
env:
REGISTRY: ghcr.io
REPO_LOWER: ${{ github.repository_owner }}/${{ github.event.repository.name }}
GHCR_REPO: ghcr.io/${{ github.repository }}
DOCKERHUB_REPO: byaidu/pdf2zh
WIN_EXE_PYTHON_VERSION: "3.12.9"
jobs:
check-repository:
name: Check if running in main repository
runs-on: ubuntu-latest
outputs:
is_main_repo: ${{ github.repository == 'Byaidu/PDFMathTranslate' }}
steps:
- run: echo "Running repository check"
test:
needs: check-repository
uses: ./.github/workflows/python-test.yml
if: needs.check-repository.outputs.is_main_repo == 'true'
build:
name: Build distribution 📦
needs: [test, check-repository]
if: needs.check-repository.outputs.is_main_repo == 'true'
runs-on: ubuntu-latest
outputs:
is_release: ${{ steps.check-version.outputs.tag }}
version: ${{ steps.check-version.outputs.tag && steps.get-release-version.outputs.version || steps.get-dev-version.outputs.version }}
steps:
- uses: actions/checkout@v4
with:
persist-credentials: true
fetch-depth: 2
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup uv with Python 3.12
uses: astral-sh/setup-uv@1edb52594c857e2b5b13128931090f0640537287 # v5.3.0
with:
python-version: "3.12"
enable-cache: true
cache-dependency-glob: "pyproject.toml"
- name: Check if there is a parent commit
id: check-parent-commit
run: |
echo "sha=$(git rev-parse --verify --quiet HEAD^)" >> $GITHUB_OUTPUT
- name: Detect and tag new version
id: check-version
if: steps.check-parent-commit.outputs.sha
uses: salsify/action-detect-and-tag-new-version@b1778166f13188a9d478e2d1198f993011ba9864 # v2.0.3
with:
version-command: |
cat pyproject.toml | grep "version = " | head -n 1 | awk -F'"' '{print $2}'
tag-template: 'awv{VERSION}'
- name: Install Dependencies
run: |
uv sync
- name: Bump version for developmental release
if: "!steps.check-version.outputs.tag"
id: get-dev-version
run: |
version=$(bumpver update --patch --tag=final --dry 2>&1 | grep "New Version" | awk '{print $NF}')
echo "version=$version.dev$(date +%s)" >> $GITHUB_OUTPUT
bumpver update --set-version $version.dev$(date +%s)
- name: Get release version
if: steps.check-version.outputs.tag
id: get-release-version
run: |
version=$(cat pyproject.toml | grep "version = " | head -n 1 | awk -F'"' '{print $2}')
echo "version=$version" >> $GITHUB_OUTPUT
- name: Build package
run: "uv build"
- name: Store the distribution packages
uses: actions/[email protected]
with:
name: python-package-distributions
path: dist/
publish-to-pypi:
name: Publish Python 🐍 distribution 📦 to PyPI
if: needs.build.outputs.is_release != ''
needs:
- check-repository
- build
- test-win64-exe
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/p/pdf2zh
permissions:
id-token: write
steps:
- name: Download all the dists
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
with:
name: python-package-distributions
path: dist/
- name: Publish distribution 📦 to PyPI
uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4
publish-to-testpypi:
name: Publish Python 🐍 distribution 📦 to TestPyPI
if: needs.build.outputs.is_release == ''
needs:
- check-repository
- build
- test-win64-exe
runs-on: ubuntu-latest
environment:
name: testpypi
url: https://test.pypi.org/p/pdf2zh
permissions:
id-token: write
steps:
- name: Download all the dists
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
with:
name: python-package-distributions
path: dist/
- name: Publish distribution 📦 to TestPyPI
uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4
with:
repository-url: https://test.pypi.org/legacy/
build-docker-image:
strategy:
fail-fast: false
matrix:
include:
- platform: linux/amd64
runner: ubuntu-latest
- platform: linux/arm64
runner: ubuntu-24.04-arm
runs-on: ${{ matrix.runner }}
needs:
- build
- check-repository
if: needs.check-repository.outputs.is_main_repo == 'true'
environment:
name: ${{ needs.build.outputs.is_release != '' && 'pypi' || 'testpypi' }}
url: ${{ needs.build.outputs.is_release != '' && 'https://hub.docker.com/r/byaidu/pdf2zh/tags?name=latest' || 'https://hub.docker.com/r/byaidu/pdf2zh/tags?name=dev' }}
permissions:
contents: read
packages: write
steps:
- name: Convert to lowercase
run: |
echo "GHCR_REPO_LOWER=$(echo ${{ env.GHCR_REPO }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
- name: Prepare
run: |
platform=${{ matrix.platform }}
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup uv with Python 3.12
uses: astral-sh/setup-uv@1edb52594c857e2b5b13128931090f0640537287 # v5.3.0
with:
python-version: "3.12"
enable-cache: true
cache-dependency-glob: "pyproject.toml"
- name: Set version from build job
if: needs.build.outputs.is_release == ''
run: |
uv tool install bumpver
echo "Using version: ${{ needs.build.outputs.version }}"
bumpver update --set-version ${{ needs.build.outputs.version }}
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ env.DOCKERHUB_REPO }}
${{ env.GHCR_REPO_LOWER }}
tags: |
type=raw,value=dev
type=raw,value=${{ needs.build.outputs.version }},enable=${{ needs.build.outputs.is_release != '' }}
type=raw,value=latest,enable=${{ needs.build.outputs.is_release != '' }}
- name: Login to Docker.io
uses: docker/login-action@v3
with:
registry: docker.io
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Login to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push by digest
id: build
uses: docker/build-push-action@v6
with:
platforms: ${{ matrix.platform }}
labels: ${{ steps.meta.outputs.labels }}
outputs: type=image,"name=${{ env.DOCKERHUB_REPO }},${{ env.GHCR_REPO_LOWER }}",push-by-digest=true,name-canonical=true,push=true
cache-from: ${{ matrix.platform == 'linux/amd64' && 'type=gha' || '' }}
cache-to: ${{ matrix.platform == 'linux/amd64' && 'type=gha,mode=max' || '' }}
- name: Export digest
run: |
mkdir -p ${{ runner.temp }}/digests
digest="${{ steps.build.outputs.digest }}"
touch "${{ runner.temp }}/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digests-${{ env.PLATFORM_PAIR }}
path: ${{ runner.temp }}/digests/*
if-no-files-found: error
retention-days: 1
merge-docker-image:
runs-on: ubuntu-latest
permissions:
packages: write
needs:
- build-docker-image
- check-repository
- test-win64-exe
- build
if: needs.check-repository.outputs.is_main_repo == 'true'
steps:
- name: Convert to lowercase
run: |
echo "GHCR_REPO_LOWER=$(echo ${{ env.GHCR_REPO }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
- name: Download digests
uses: actions/download-artifact@v4
with:
path: ${{ runner.temp }}/digests
pattern: digests-*
merge-multiple: true
- name: Login to Docker.io
uses: docker/login-action@v3
with:
registry: docker.io
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Login to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ env.DOCKERHUB_REPO }}
${{ env.GHCR_REPO_LOWER }}
tags: |
type=raw,value=dev
type=raw,value=${{ needs.build.outputs.version }},enable=${{ needs.build.outputs.is_release != '' && 'true' || 'false' }}
type=raw,value=latest,enable=${{ needs.build.outputs.is_release != '' && 'true' || 'false' }}
- name: Create manifest list and push
working-directory: ${{ runner.temp }}/digests
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.DOCKERHUB_REPO }}@sha256:%s ' *)
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.GHCR_REPO_LOWER }}@sha256:%s ' *)
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ env.DOCKERHUB_REPO }}:${{ steps.meta.outputs.version }}
docker buildx imagetools inspect ${{ env.GHCR_REPO_LOWER }}:${{ steps.meta.outputs.version }}
build-win64-exe:
runs-on: windows-latest
needs:
- check-repository
if: needs.check-repository.outputs.is_main_repo == 'true'
steps:
- name: 检出代码
uses: actions/checkout@v4
- name: Setup uv with Python ${{ env.WIN_EXE_PYTHON_VERSION }}
uses: astral-sh/setup-uv@1edb52594c857e2b5b13128931090f0640537287 # v5.3.0
with:
python-version: ${{ env.WIN_EXE_PYTHON_VERSION }}
enable-cache: true
cache-dependency-glob: "pyproject.toml"
- name: 执行所有任务(创建目录、下载、解压、复制文件、安装依赖)
shell: pwsh
run: |
Write-Host "==== 创建必要的目录 ===="
New-Item -Path "./build" -ItemType Directory -Force
New-Item -Path "./build/runtime" -ItemType Directory -Force
New-Item -Path "./dep_build" -ItemType Directory -Force
Write-Host "==== 复制代码到 dep_build ===="
Get-ChildItem -Path "./" -Exclude "dep_build", "build" | Copy-Item -Destination "./dep_build" -Recurse -Force
Write-Host "==== 下载并解压 Python ${{ env.WIN_EXE_PYTHON_VERSION }} ===="
Write-Host "pythonUrl: https://www.python.org/ftp/python/${{ env.WIN_EXE_PYTHON_VERSION }}/python-${{ env.WIN_EXE_PYTHON_VERSION }}-embed-amd64.zip"
$pythonUrl = "https://www.python.org/ftp/python/${{ env.WIN_EXE_PYTHON_VERSION }}/python-${{ env.WIN_EXE_PYTHON_VERSION }}-embed-amd64.zip"
$pythonZip = "./dep_build/python.zip"
Invoke-WebRequest -Uri $pythonUrl -OutFile $pythonZip
Expand-Archive -Path $pythonZip -DestinationPath "./build/runtime" -Force
Write-Host "==== 下载并解压 PyStand ===="
$pystandUrl = "https://github.com/skywind3000/PyStand/releases/download/1.1.4/PyStand-v1.1.4-exe.zip"
$pystandZip = "./dep_build/PyStand.zip"
Invoke-WebRequest -Uri $pystandUrl -OutFile $pystandZip
Expand-Archive -Path $pystandZip -DestinationPath "./dep_build/PyStand" -Force
Write-Host "==== 复制 PyStand.exe 到 build 并重命名 ===="
$pystandExe = "./dep_build/PyStand/PyStand-x64-CLI/PyStand.exe"
$destExe = "./build/pdf2zh.exe"
if (Test-Path $pystandExe) {
Copy-Item -Path $pystandExe -Destination $destExe -Force
} else {
Write-Host "错误: PyStand.exe 未找到!"
exit 1
}
Write-Host "==== 创建 Python venv 在 dep_build ===="
uv venv ./dep_build/venv
./dep_build/venv/Scripts/activate
Write-Host "==== 在 venv 环境中安装项目依赖 ===="
uv pip install . onnxruntime==1.16.1
Write-Host "==== 复制 venv/Lib/site-packages 到 build/ ===="
Copy-Item -Path "./dep_build/venv/Lib/site-packages" -Destination "./build/site-packages" -Recurse -Force
Write-Host "==== 复制 script/_pystand_static.int 到 build/ ===="
$staticFile = "./script/_pystand_static.int"
$destStatic = "./build/_pystand_static.int"
if (Test-Path $staticFile) {
Copy-Item -Path $staticFile -Destination $destStatic -Force
} else {
Write-Host "错误: script/_pystand_static.int 未找到!"
exit 1
}
- name: Upload build artifact
uses: actions/upload-artifact@v4
with:
name: win64-exe
path: ./build
if-no-files-found: error
compression-level: 1
include-hidden-files: true
test-win64-exe:
needs:
- build-win64-exe
- check-repository
if: needs.check-repository.outputs.is_main_repo == 'true'
runs-on: windows-latest
steps:
- name: 检出代码
uses: actions/checkout@v4
- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: win64-exe
path: ./build
- name: Test show version
run: |
./build/pdf2zh.exe --version
- name: Test - Translate a PDF file with plain text only
run: |
./build/pdf2zh.exe ./test/file/translate.cli.plain.text.pdf -o ./test/file
- name: Test - Translate a PDF file figure
run: |
./build/pdf2zh.exe ./test/file/translate.cli.text.with.figure.pdf -o ./test/file
- name: Upload test results
uses: actions/upload-artifact@v4
with:
name: test-results
path: ./test/file/
release-draft:
name: Release Draft Tasks
needs:
- check-repository
- build
- publish-to-pypi
- publish-to-testpypi
- merge-docker-image
- test-win64-exe
if: |
always() && needs.check-repository.outputs.is_main_repo == 'true' &&
(needs.publish-to-pypi.result == 'success' || needs.publish-to-testpypi.result == 'success') &&
needs.merge-docker-image.result == 'success' &&
needs.test-win64-exe.result == 'success'
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
outputs:
tag_name: ${{ steps.release-drafter.outputs.tag_name }}
steps:
- uses: actions/checkout@v4
with:
persist-credentials: true
fetch-depth: 2
token: ${{ secrets.GITHUB_TOKEN }}
- name: Publish the release notes
id: release-drafter
uses: release-drafter/release-drafter@b1476f6e6eb133afa41ed8589daba6dc69b4d3f5 # v6.1.0
with:
publish: ${{ needs.build.outputs.is_release != '' }}
tag: ${{ needs.build.outputs.is_release }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
upload-release:
needs: [release-draft, check-repository]
runs-on: ubuntu-latest
if: always() && needs.check-repository.outputs.is_main_repo == 'true' &&
needs.release-draft.result == 'success'
steps:
- name: 检出代码
uses: actions/checkout@v4
- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: win64-exe
path: ./build
- name: Create release zip
run: |
zip -9qr "pdf2zh-${{ needs.release-draft.outputs.tag_name }}-win64.zip" ./build/*
- name: Upload to latest release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Get the latest release (including drafts and pre-releases)
LATEST_RELEASE=${{ needs.release-draft.outputs.tag_name }}
echo "Latest release tag: $LATEST_RELEASE"
# Upload the zip file to the release
gh release upload "$LATEST_RELEASE" "pdf2zh-${{ needs.release-draft.outputs.tag_name }}-win64.zip" --clobber