-
Notifications
You must be signed in to change notification settings - Fork 2
376 lines (312 loc) · 12.1 KB
/
Copy pathbuild.yml
File metadata and controls
376 lines (312 loc) · 12.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
name: Build C programs and libraries, publish to PyPI and create a release
on:
workflow_dispatch:
push:
tags:
- "v*.*.*"
jobs:
build: # 编译C库
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
- os: windows-2022
arch: x86_64 # windows x86_6
- os: ubuntu-22.04
arch: x86_64 # Ubuntu x86_64
- os: macos-13
arch: x86_64 # macOS Intel
- os: macos-14
arch: arm64 # macOS Apple Silicon
fail-fast: true
defaults:
run:
shell: bash
steps:
- name: Set MSYS2 (Windows) # windows平台使用MSYS2工具编译程序
if: contains(matrix.os, 'windows')
uses: msys2/setup-msys2@v2
with:
msystem: UCRT64
install: >-
git
mingw-w64-ucrt-x86_64-gcc
make
- name: Configured git attributes for line endings (Windows) # 防止接下来在windows平台checkout代码时,文本文件的换行符发生变化,导致MSYS2工作出错
if: contains(matrix.os, 'windows')
run: git config --global core.autocrlf input
- name: Checkout code # 下载库代码
uses: actions/checkout@v3
with:
fetch-depth: 1
# --------------------- 检查文件中的版本名是否和tag名相符 --------------------
- name: Check version
run: |
TAG_NAME=${{ github.ref_name }}
PYVERSION_FILE="pyfmm/_version.py"
# 提取 Python 版本号
PYTHON_VERSION=$(sed -n 's/^__version__ = "\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)"/\1/p' ${PYVERSION_FILE})
if [ -z "$PYTHON_VERSION" ]; then
echo "Error: Failed to extract version from ${PYVERSION_FILE}"
exit 1
fi
# 检查 Python 版本与 Tag 是否匹配
if [ "$PYTHON_VERSION" != "${TAG_NAME#v}" ]; then
echo "Error: Version mismatch between tag (${TAG_NAME}) and ${PYVERSION_FILE} (${PYTHON_VERSION})"
exit 1
fi
echo "Version check passed:"
echo " Tag: ${TAG_NAME}"
echo " Python version: ${PYTHON_VERSION}"
# --------------------------- 安装依赖 ------------------------------------------
# - name: Install dependencies (Ubuntu)
# if: contains(matrix.os, 'ubuntu')
# run: |
# sudo apt update
# sudo apt install -y build-essential
# 在 ubuntu 系统上创建低版本 glibc (2.17) 的环境进行编译,以提供向后兼容能力
- name: Create CentOS 7.7 container (Ubuntu)
if: contains(matrix.os, 'ubuntu')
run: |
CONTAINER_ID=$(docker create -v ${{ github.workspace }}:/workspace -w /workspace centos:7.7.1908 sleep infinity)
echo "CONTAINER_ID=$CONTAINER_ID" >> $GITHUB_ENV
docker start $CONTAINER_ID
- name: Update yum in container (Ubuntu)
if: contains(matrix.os, 'ubuntu')
run: |
docker exec ${{ env.CONTAINER_ID }} bash -c "
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup && \
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo && \
yum clean all -y && yum makecache -y && yum install -y gcc make
"
- name: Install dependencies (macOS)
# 匹配包含 "macos" 的操作系统
if: contains(matrix.os, 'macos')
run: |
brew install libomp
# ----------------- 编译C库和C程序 ----------------------------------
- name: Build the project (macOS)
if: contains(matrix.os, 'macos')
working-directory: ./pyfmm/C_extension
run: |
make ARCH="-arch ${{ matrix.arch }}" \
CC=gcc-14
make cleanbuild
otool -L lib/*.so
# - name: Build the project (Ubuntu)
# if: contains(matrix.os, 'ubuntu')
# working-directory: ./pyfmm/C_extension
# run: |
# make
# make cleanbuild
# ldd lib/*.so
- name: Build the project in container (Ubuntu)
if: contains(matrix.os, 'ubuntu')
working-directory:
run: |
docker exec ${{ env.CONTAINER_ID }} bash -c "cd pyfmm/C_extension && ls && make && make cleanbuild && cd -"
cd ./pyfmm/C_extension
ldd lib/*.so
cd -
- name: Cleanup container (Ubuntu)
if: contains(matrix.os, 'ubuntu')
run: |
docker stop ${{ env.CONTAINER_ID }}
docker rm ${{ env.CONTAINER_ID }}
- name: Build the project (Windows)
if: contains(matrix.os, 'windows')
shell: msys2 {0}
working-directory: ./pyfmm/C_extension
run: |
make
make cleanbuild
ldd lib/*.so
# ------------------------ 定义接下来打包程序命名时的系统名后缀 ---------------
- name: Define the package OS suffix
run: |
# 符合pypi命名规范,否则上传失败
if [[ "${{ matrix.os }}" == *"ubuntu"* ]]; then
SUFFIX_PLAT_NAME="manylinux2014_x86_64"
elif [[ "${{ matrix.os }}" == *"macos"* && "${{ matrix.arch }}" == *"x86_64"* ]]; then
SUFFIX_PLAT_NAME="macosx_10_9_x86_64"
elif [[ "${{ matrix.os }}" == *"macos"* && "${{ matrix.arch }}" == *"arm64"* ]]; then
SUFFIX_PLAT_NAME="macosx_11_0_arm64"
elif [[ "${{ matrix.os }}" == *"windows"* ]]; then
SUFFIX_PLAT_NAME="win_amd64"
else
echo " Unsupported OS: ${{ matrix.os }} (${{ matrix.arch }})"
exit 1
fi
echo "SUFFIX_PLAT_NAME=$SUFFIX_PLAT_NAME" >> $GITHUB_ENV
# --------------------------- 打包整个程序 ---------------------
- name: Package the binary
run: |
PACK_NAME=pyfmm_kit-${{ github.ref_name }}-${{ env.SUFFIX_PLAT_NAME }}
echo "PACK_NAME=$PACK_NAME" >> $GITHUB_ENV
FILE_CONTENT=$(ls -A)
mkdir -p $PACK_NAME
cp -r ${FILE_CONTENT} $PACK_NAME/
tar -czvf $PACK_NAME.tar.gz $PACK_NAME
rm -rf $PACK_NAME
# -------------------- upload artifacts -----------------------
- name: Upload artifact (*.tar.gz)
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.os }}-${{ matrix.arch }}_tar
path: ${{ env.PACK_NAME }}.tar.gz
# =======================================================================================
test_project: # 在全新系统上测试程序,不安装其它依赖,看能否运行
runs-on: ${{ matrix.os }}
needs: build
strategy:
matrix:
include:
- os: windows-2022
arch: x86_64 # windows x86_6
- os: ubuntu-22.04
arch: x86_64 # Ubuntu x86_64
- os: macos-13
arch: x86_64 # macOS Intel
- os: macos-14
arch: arm64 # macOS Apple Silicon
fail-fast: true
defaults:
run:
shell: bash
steps:
- name: Download artifacts
uses: actions/download-artifact@v4
with:
name: ${{ matrix.os }}-${{ matrix.arch }}_tar
path: artifacts
- name: Display structure of downloaded files, and Uncompress
run: |
ls -R artifacts
echo "------------------- tar output -----------------------------"
tar -xzvf artifacts/*.tar.gz
echo "------------------------------------------------------------"
# 获得压缩包解压出来的文件夹名
PACK_NAME=$(ls | grep pyfmm_kit)
echo "PACK_NAME=$PACK_NAME" >> $GITHUB_ENV
# 从解压出的文件夹命名来推断${{ env.SUFFIX_PLAT_NAME }}
SUFFIX_PLAT_NAME=$(echo $PACK_NAME | sed 's/.*-\(.*\)/\1/')
echo "SUFFIX_PLAT_NAME=$SUFFIX_PLAT_NAME" >> $GITHUB_ENV
echo $PACK_NAME
echo $SUFFIX_PLAT_NAME
# --------------------------- 安装依赖 ------------------------------------------
# 实际使用时可能需要安装libomp
# - name: Install libomp (Ubuntu)
# if: contains(matrix.os, 'ubuntu')
# run: |
# sudo apt install -y libomp-dev
# - name: Set alias (MacOS)
# if: contains(matrix.os, 'macos')
# run: |
# brew install coreutils
# echo "alias timeout=gtimeout" >> ~/.bashrc
# --------------------搭建python环境,开始测试,并制作wheel文件 ------------------------------
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install dependencies
working-directory: ${{ env.PACK_NAME }}
run: |
python -m pip install --upgrade pip
pip install --upgrade setuptools wheel build
pip install -v .
- name: Clean up build and egg-info directories
working-directory: ${{ env.PACK_NAME }}
run: |
# 清理临时文件
rm -rf build/
rm -rf pyfmm_kit.egg-info/
- name: Test
working-directory: ${{ env.PACK_NAME }}/.github/tests
run: |
python uniform.py
# --------------------------- 制作wheels ---------------------
- name: Build the Python Wheel
working-directory: ${{ env.PACK_NAME }}
run: |
python setup.py bdist_wheel --plat-name=${{ env.SUFFIX_PLAT_NAME }} # 只制作wheel,这里不打包源码
- name: Upload artifact (*.whl)
uses: actions/upload-artifact@v4
with:
name: ${{ env.PACK_NAME }}_whl
path: ${{ env.PACK_NAME }}/dist/*.whl
# ===============================================================================
publish_pypi: # 上传pypi
runs-on: ubuntu-latest
needs: test_project
permissions:
id-token: write
contents: read
defaults:
run:
shell: bash
steps:
- name: Download artifacts
uses: actions/download-artifact@v4
with:
pattern: "*_whl"
path: artifacts
- name: Display structure of downloaded files
run: ls -R artifacts
- name: Move wheel files to dist/
run: |
mkdir -p dist
mv artifacts/*/*.whl dist/
- name: Pypi Publish
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: dist/
# ===============================================================================
release: # 创建Release
runs-on: ubuntu-latest
needs: [build, test_project]
permissions:
contents: write
defaults:
run:
shell: bash
steps:
- name: Download artifacts
uses: actions/download-artifact@v4
with:
path: artifacts
pattern: "*_tar"
- name: Download artifacts (*.whl)
uses: actions/download-artifact@v4
with:
path: artifacts
pattern: "*_whl"
- name: Display structure of downloaded files
run: ls -R artifacts
# 对artifacts中的每个tar.gz文件,根据其内部的文件夹名重新命名压缩包
- name: Rename tar.gz files
run: |
mkdir -p release
for file in artifacts/*/*.tar.gz; do
TEMP_FILE=tmp
# 这里不能把tar合并到下方的管道中,在github actions中会报错
tar -tzf $file > $TEMP_FILE
PACK_NAME=$(head -n 1 $TEMP_FILE | cut -f 1 -d '/')
mv $file release/${PACK_NAME}.tar.gz
rm $TEMP_FILE
done
ls -R release
- name: Move wheel files to dist/
run: |
mkdir -p dist
mv artifacts/*/*.whl dist/
- name: Create Release
id: create_release
uses: softprops/action-gh-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GH_PAT }}
with:
draft: true
files: |
release/*.tar.gz
dist/*.whl