如何在GitHub Actions中获取最低Glibc版本的Runner适配旧Linux
我用PyInstaller打包Python库为可执行文件,通过GitHub Actions自动化构建Linux二进制文件。PyInstaller要求Linux构建环境使用尽可能低版本的Glibc,以保证向前兼容性,适配更多旧版Linux系统。但GitHub Actions默认的Node20依赖最低Glibc 2.28,满足不了需求。我试过用Docker容器运行Actions,指定ubuntu-18.04和node:16镜像,Glibc版本还是过高。当前的GitHub Actions配置如下:
jobs: container-test-job: runs-on: ubuntu-18.04 container: node:16 timeout-minutes: 20 steps: - name: Install dependencies into minimal dockerfile run: | # ubuntu dockerfile is very minimal (only 122 packages are installed) # need to install updated git (from official git ppa) apt update apt install -y software-properties-common apt-get install python3-launchpadlib -y add-apt-repository ppa:git-core/ppa -y # install dependencies expected by other steps apt update apt install -y git \ curl \ ca-certificates \ wget \ bzip2 \ zip \ unzip \ xz-utils \ maven \ ant sudo locales # set Locale to en_US.UTF-8 (avoids hang during compilation) locale-gen en_US.UTF-8 echo "LANG=en_US.UTF-8" >> $GITHUB_ENV echo "LANGUAGE=en_US.UTF-8" >> $GITHUB_ENV echo "LC_ALL=en_US.UTF-8" >> $GITHUB_ENV - name: Checkout code uses: actions/checkout@v3 with: fetch-depth: 0 - uses: mamba-org/setup-micromamba@v1 with: micromamba-version: '1.5.6-0' micromamba-binary-path: ${{ runner.temp }}/bin/micromamba environment-file: conda_environment_binary-old-linux.yaml environment-name: copernicusmarine-binary-old-linux condarc-file: .condarc cache-environment: true post-cleanup: 'all'
请问能不能把Docker镜像的Glibc版本固定到需要的低版本?或者有没有其他自动化构建二进制文件的方法?(已经试过自托管Runner,但现有设备的Glibc版本也比较新)
一、更换更低版本的基础Docker镜像
直接选择自带低版本Glibc的官方基础镜像,这类镜像的Glibc版本固定且足够低,能覆盖绝大多数旧版Linux系统的兼容需求:
debian:stretch:Debian 9镜像,内置Glibc 2.24,兼容性覆盖多数主流旧系统centos:7:CentOS 7镜像,内置Glibc 2.17,是目前能找到的Glibc版本较低的通用镜像manylinux2014_x86_64:专为Python包兼容设计的镜像,基于CentOS 7,预装了低Glibc和编译依赖
修改GitHub Actions配置中的container字段,比如换成debian:stretch,然后手动安装Node.js 16(基础镜像不带Node环境):
jobs: container-test-job: runs-on: ubuntu-18.04 container: debian:stretch timeout-minutes: 20 steps: - name: Install Node.js 16 run: | curl -fsSL https://deb.nodesource.com/setup_16.x | bash - apt-get install -y nodejs # 后续安装依赖、代码拉取、micromamba配置步骤保持不变
如果需要直接带Node环境的低Glibc镜像,可以使用node:16-stretch-slim,它基于Debian 9,内置Node.js 16和Glibc 2.24。
二、自定义Docker镜像固定Glibc版本
如果找不到现成的适配镜像,可以自行构建包含指定Glibc版本、Node.js、Python环境的镜像,推送到Docker Hub或GitHub容器仓库后,在Actions中使用:
- 编写Dockerfile,基于低版本基础镜像预装所需依赖:
FROM debian:stretch # 安装Node.js 16 RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash - && \ apt-get install -y nodejs # 安装编译及运行依赖 RUN apt-get update && apt-get install -y \ git \ curl \ ca-certificates \ wget \ bzip2 \ zip \ unzip \ xz-utils \ maven \ ant \ sudo \ locales && \ locale-gen en_US.UTF-8 # 设置Locale环境变量 ENV LANG=en_US.UTF-8 \ LANGUAGE=en_US.UTF-8 \ LC_ALL=en_US.UTF-8
- 构建并推送镜像:
docker build -t your-username/custom-low-glibc-image:v1 . docker push your-username/custom-low-glibc-image:v1
- 在GitHub Actions中使用自定义镜像:
jobs: container-test-job: runs-on: ubuntu-18.04 container: your-username/custom-low-glibc-image:v1 timeout-minutes: 20 steps: - name: Checkout code uses: actions/checkout@v3 with: fetch-depth: 0 - uses: mamba-org/setup-micromamba@v1 with: micromamba-version: '1.5.6-0' micromamba-binary-path: ${{ runner.temp }}/bin/micromamba environment-file: conda_environment_binary-old-linux.yaml environment-name: copernicusmarine-binary-old-linux condarc-file: .condarc cache-environment: true post-cleanup: 'all'
三、静态编译可执行文件
如果你的Python库支持静态编译,可以通过PyInstaller的静态打包选项,将所有依赖(包括Glibc)直接打包进可执行文件,彻底摆脱对系统Glibc的依赖:
在PyInstaller命令中添加--static参数(注意:部分Python扩展库不支持静态编译,需提前测试):
pyinstaller --static your-script.py
也可以在micromamba环境中安装静态编译的Python及依赖,再进行打包,进一步提升兼容性。
内容的提问来源于stack exchange,提问作者Oriol Ricart Vilarrubias.

