build(ci): 优化构建发布流程与环境配置 (v1.1.2) #72
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build and Release | |
| on: | |
| push: | |
| branches: | |
| - master | |
| tags: | |
| - 'v*' | |
| pull_request: | |
| workflow_dispatch: | |
| inputs: | |
| publish_npm: | |
| description: 'Publish to npm' | |
| required: false | |
| type: boolean | |
| default: false | |
| jobs: | |
| linux-gnu-x64: | |
| runs-on: ubuntu-24.04 | |
| container: | |
| image: alibaba-cloud-linux-3-registry.cn-hangzhou.cr.aliyuncs.com/alinux3/alinux3:latest | |
| env: | |
| ZIG_TARGET: x86_64-linux-gnu | |
| steps: | |
| - name: Install prerequisites | |
| run: yum install -y git curl wget xz tar unzip rsync which python3 python3-pip python3-setuptools autoconf automake libtool pkgconfig gcc gcc-c++ make ninja-build cmake file vim procps-ng iputils | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Install Node.js | |
| run: | | |
| curl -fsSL https://rpm.nodesource.com/setup_20.x | bash - | |
| yum install -y nodejs | |
| - name: Setup Zig | |
| uses: mlugg/setup-zig@v2 | |
| with: | |
| version: 0.15.2 | |
| - name: Configure Zig environment | |
| run: | | |
| ZIG_BIN=$(command -v zig) | |
| if [ -z "$ZIG_BIN" ]; then | |
| echo "zig binary not found" >&2 | |
| exit 1 | |
| fi | |
| echo "ZIG_BIN=$ZIG_BIN" >> "$GITHUB_ENV" | |
| echo "CC=$ZIG_BIN cc -target $ZIG_TARGET -fno-sanitize=undefined" >> "$GITHUB_ENV" | |
| echo "CXX=$ZIG_BIN c++ -target $ZIG_TARGET -fno-sanitize=undefined" >> "$GITHUB_ENV" | |
| echo "CFLAGS=-fPIC -fno-sanitize=undefined" >> "$GITHUB_ENV" | |
| echo "CXXFLAGS=-fPIC -fno-sanitize=undefined" >> "$GITHUB_ENV" | |
| echo "LDFLAGS=-static-libstdc++ -static-libgcc -fno-sanitize=undefined" >> "$GITHUB_ENV" | |
| - name: Cache npm dependencies | |
| id: cache-npm | |
| uses: actions/cache@v4 | |
| with: | |
| path: node_modules | |
| key: npm-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('package-lock.json') }} | |
| restore-keys: | | |
| npm-${{ runner.os }}-${{ env.ZIG_TARGET }}- | |
| - name: Cache RocketMQ native deps | |
| uses: actions/cache@v4 | |
| with: | |
| path: | | |
| deps/rocketmq/bin | |
| deps/rocketmq/libs/signature/lib | |
| deps/rocketmq/tmp_down_dir | |
| key: rocketmq-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('deps/rocketmq/build.sh', 'deps/rocketmq/CMakeLists.txt') }} | |
| restore-keys: | | |
| rocketmq-${{ runner.os }}-${{ env.ZIG_TARGET }}- | |
| - name: Cache addon build | |
| id: cache-addon | |
| uses: actions/cache@v4 | |
| with: | |
| path: build/rocketmq.node | |
| key: addon-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('lib/**', 'CMakeLists.txt', 'package.json') }} | |
| restore-keys: | | |
| addon-${{ runner.os }}-${{ env.ZIG_TARGET }}- | |
| - name: Install npm dependencies | |
| if: steps.cache-npm.outputs.cache-hit != 'true' | |
| run: npm install --no-audit --prefer-offline | |
| - name: Build RocketMQ native deps | |
| if: steps.cache-addon.outputs.cache-hit != 'true' | |
| run: ./deps/rocketmq/build.sh | |
| - name: Build addon | |
| if: steps.cache-addon.outputs.cache-hit != 'true' | |
| run: npx cmake-js compile --CDCMAKE_BUILD_TYPE=Release | |
| - name: Package artifact | |
| run: | | |
| mkdir -p Release | |
| strip --strip-all build/rocketmq.node | |
| cp build/rocketmq.node Release/linux-x86_64-gnu-rocketmq.node | |
| - name: Upload artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: linux-x86_64-gnu | |
| path: Release/linux-x86_64-gnu-rocketmq.node | |
| linux-gnu-arm64: | |
| runs-on: ubuntu-24.04-arm | |
| container: | |
| image: alibaba-cloud-linux-3-registry.cn-hangzhou.cr.aliyuncs.com/alinux3/alinux3:latest | |
| env: | |
| ZIG_TARGET: aarch64-linux-gnu | |
| steps: | |
| - name: Install prerequisites | |
| run: yum install -y git curl wget xz tar unzip rsync which python3 python3-pip python3-setuptools autoconf automake libtool pkgconfig gcc gcc-c++ make ninja-build cmake file vim procps-ng iputils | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Install Node.js | |
| run: | | |
| curl -fsSL https://rpm.nodesource.com/setup_20.x | bash - | |
| yum install -y nodejs | |
| - name: Setup Zig | |
| uses: mlugg/setup-zig@v2 | |
| with: | |
| version: 0.15.2 | |
| - name: Configure Zig environment | |
| run: | | |
| ZIG_BIN=$(command -v zig) | |
| if [ -z "$ZIG_BIN" ]; then | |
| echo "zig binary not found" >&2 | |
| exit 1 | |
| fi | |
| echo "ZIG_BIN=$ZIG_BIN" >> "$GITHUB_ENV" | |
| echo "CC=$ZIG_BIN cc -target $ZIG_TARGET -fno-sanitize=undefined" >> "$GITHUB_ENV" | |
| echo "CXX=$ZIG_BIN c++ -target $ZIG_TARGET -fno-sanitize=undefined" >> "$GITHUB_ENV" | |
| echo "CFLAGS=-fPIC -fno-sanitize=undefined" >> "$GITHUB_ENV" | |
| echo "CXXFLAGS=-fPIC -fno-sanitize=undefined" >> "$GITHUB_ENV" | |
| echo "LDFLAGS=-static-libstdc++ -static-libgcc -fno-sanitize=undefined" >> "$GITHUB_ENV" | |
| - name: Cache npm dependencies | |
| id: cache-npm | |
| uses: actions/cache@v4 | |
| with: | |
| path: node_modules | |
| key: npm-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('package-lock.json') }} | |
| restore-keys: | | |
| npm-${{ runner.os }}-${{ env.ZIG_TARGET }}- | |
| - name: Cache RocketMQ native deps | |
| uses: actions/cache@v4 | |
| with: | |
| path: | | |
| deps/rocketmq/bin | |
| deps/rocketmq/libs/signature/lib | |
| deps/rocketmq/tmp_down_dir | |
| key: rocketmq-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('deps/rocketmq/build.sh', 'deps/rocketmq/CMakeLists.txt') }} | |
| restore-keys: | | |
| rocketmq-${{ runner.os }}-${{ env.ZIG_TARGET }}- | |
| - name: Cache addon build | |
| id: cache-addon | |
| uses: actions/cache@v4 | |
| with: | |
| path: build/rocketmq.node | |
| key: addon-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('lib/**', 'CMakeLists.txt', 'package.json') }} | |
| restore-keys: | | |
| addon-${{ runner.os }}-${{ env.ZIG_TARGET }}- | |
| - name: Install npm dependencies | |
| if: steps.cache-npm.outputs.cache-hit != 'true' | |
| run: npm install --no-audit --prefer-offline | |
| - name: Build RocketMQ native deps | |
| if: steps.cache-addon.outputs.cache-hit != 'true' | |
| run: ./deps/rocketmq/build.sh | |
| - name: Build addon | |
| if: steps.cache-addon.outputs.cache-hit != 'true' | |
| run: npx cmake-js compile --CDCMAKE_BUILD_TYPE=Release | |
| - name: Package artifact | |
| run: | | |
| mkdir -p Release | |
| strip --strip-all build/rocketmq.node | |
| cp build/rocketmq.node Release/linux-aarch64-gnu-rocketmq.node | |
| - name: Upload artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: linux-aarch64-gnu | |
| path: Release/linux-aarch64-gnu-rocketmq.node | |
| linux-musl-x64: | |
| runs-on: ubuntu-24.04 | |
| container: | |
| image: node:20-alpine | |
| env: | |
| ZIG_TARGET: x86_64-linux-musl | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Install system dependencies | |
| run: apk add --no-cache bash build-base cmake ninja python3 curl unzip git autoconf automake libtool pkgconfig | |
| - name: Setup Zig | |
| uses: mlugg/setup-zig@v2 | |
| with: | |
| version: 0.15.2 | |
| - name: Configure Zig environment | |
| run: | | |
| ZIG_BIN=$(command -v zig) | |
| if [ -z "$ZIG_BIN" ]; then | |
| echo "zig binary not found" >&2 | |
| exit 1 | |
| fi | |
| echo "ZIG_BIN=$ZIG_BIN" >> "$GITHUB_ENV" | |
| echo "CC=$ZIG_BIN cc -target $ZIG_TARGET -fno-sanitize=undefined" >> "$GITHUB_ENV" | |
| echo "CXX=$ZIG_BIN c++ -target $ZIG_TARGET -fno-sanitize=undefined" >> "$GITHUB_ENV" | |
| echo "CFLAGS=-fPIC -fno-sanitize=undefined" >> "$GITHUB_ENV" | |
| echo "CXXFLAGS=-fPIC -fno-sanitize=undefined" >> "$GITHUB_ENV" | |
| echo "LDFLAGS=-static-libstdc++ -static-libgcc -fno-sanitize=undefined" >> "$GITHUB_ENV" | |
| - name: Cache npm dependencies | |
| id: cache-npm | |
| uses: actions/cache@v4 | |
| with: | |
| path: node_modules | |
| key: npm-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('package-lock.json') }} | |
| restore-keys: | | |
| npm-${{ runner.os }}- | |
| - name: Cache RocketMQ native deps | |
| uses: actions/cache@v4 | |
| with: | |
| path: | | |
| deps/rocketmq/bin | |
| deps/rocketmq/libs/signature/lib | |
| deps/rocketmq/tmp_down_dir | |
| key: rocketmq-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('deps/rocketmq/build.sh', 'deps/rocketmq/CMakeLists.txt') }} | |
| restore-keys: | | |
| rocketmq-${{ runner.os }}- | |
| - name: Cache addon build | |
| id: cache-addon | |
| uses: actions/cache@v4 | |
| with: | |
| path: build/rocketmq.node | |
| key: addon-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('lib/**', 'CMakeLists.txt', 'package.json') }} | |
| restore-keys: | | |
| addon-${{ runner.os }}- | |
| - name: Install npm dependencies | |
| if: steps.cache-npm.outputs.cache-hit != 'true' | |
| run: npm install --no-audit --prefer-offline | |
| - name: Build RocketMQ native deps | |
| if: steps.cache-addon.outputs.cache-hit != 'true' | |
| run: ./deps/rocketmq/build.sh | |
| - name: Build addon | |
| if: steps.cache-addon.outputs.cache-hit != 'true' | |
| run: npx cmake-js compile --CDCMAKE_BUILD_TYPE=Release | |
| - name: Package artifact | |
| run: | | |
| mkdir -p Release | |
| strip --strip-all build/rocketmq.node | |
| cp build/rocketmq.node Release/linux-x86_64-musl-rocketmq.node | |
| - name: Upload artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: linux-x86_64-musl | |
| path: Release/linux-x86_64-musl-rocketmq.node | |
| linux-musl-arm64: | |
| runs-on: ubuntu-24.04-arm | |
| env: | |
| ZIG_TARGET: aarch64-linux-musl | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 | |
| - name: Install system dependencies | |
| run: sudo apt-get update && sudo apt-get install -y build-essential cmake ninja-build python3 curl unzip git autoconf automake libtool pkg-config | |
| - name: Setup Zig | |
| uses: mlugg/setup-zig@v2 | |
| with: | |
| version: 0.15.2 | |
| - name: Configure Zig environment | |
| run: | | |
| ZIG_BIN=$(command -v zig) | |
| if [ -z "$ZIG_BIN" ]; then | |
| echo "zig binary not found" >&2 | |
| exit 1 | |
| fi | |
| echo "ZIG_BIN=$ZIG_BIN" >> "$GITHUB_ENV" | |
| echo "CC=$ZIG_BIN cc -target $ZIG_TARGET -fno-sanitize=undefined" >> "$GITHUB_ENV" | |
| echo "CXX=$ZIG_BIN c++ -target $ZIG_TARGET -fno-sanitize=undefined" >> "$GITHUB_ENV" | |
| echo "CFLAGS=-fPIC -fno-sanitize=undefined" >> "$GITHUB_ENV" | |
| echo "CXXFLAGS=-fPIC -fno-sanitize=undefined" >> "$GITHUB_ENV" | |
| echo "LDFLAGS=-static-libstdc++ -static-libgcc -fno-sanitize=undefined" >> "$GITHUB_ENV" | |
| - name: Cache npm dependencies | |
| id: cache-npm | |
| uses: actions/cache@v4 | |
| with: | |
| path: node_modules | |
| key: npm-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('package-lock.json') }} | |
| restore-keys: | | |
| npm-${{ runner.os }}- | |
| - name: Cache RocketMQ native deps | |
| uses: actions/cache@v4 | |
| with: | |
| path: | | |
| deps/rocketmq/bin | |
| deps/rocketmq/libs/signature/lib | |
| deps/rocketmq/tmp_down_dir | |
| key: rocketmq-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('deps/rocketmq/build.sh', 'deps/rocketmq/CMakeLists.txt') }} | |
| restore-keys: | | |
| rocketmq-${{ runner.os }}- | |
| - name: Cache addon build | |
| id: cache-addon | |
| uses: actions/cache@v4 | |
| with: | |
| path: build/rocketmq.node | |
| key: addon-${{ runner.os }}-${{ env.ZIG_TARGET }}-${{ hashFiles('lib/**', 'CMakeLists.txt', 'package.json') }} | |
| restore-keys: | | |
| addon-${{ runner.os }}- | |
| - name: Install npm dependencies | |
| if: steps.cache-npm.outputs.cache-hit != 'true' | |
| run: npm install --no-audit --prefer-offline | |
| - name: Build RocketMQ native deps | |
| if: steps.cache-addon.outputs.cache-hit != 'true' | |
| run: ./deps/rocketmq/build.sh | |
| - name: Build addon | |
| if: steps.cache-addon.outputs.cache-hit != 'true' | |
| run: npx cmake-js compile --CDCMAKE_BUILD_TYPE=Release | |
| - name: Package artifact | |
| run: | | |
| mkdir -p Release | |
| strip --strip-all build/rocketmq.node | |
| cp build/rocketmq.node Release/linux-aarch64-musl-rocketmq.node | |
| - name: Upload artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: linux-aarch64-musl | |
| path: Release/linux-aarch64-musl-rocketmq.node | |
| macos-universal: | |
| runs-on: macos-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 | |
| - name: Install build tools | |
| run: brew install cmake ninja xz autoconf automake libtool pkg-config | |
| - name: Cache npm dependencies | |
| id: cache-npm | |
| uses: actions/cache@v4 | |
| with: | |
| path: node_modules | |
| key: npm-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('package-lock.json') }} | |
| restore-keys: | | |
| npm-${{ runner.os }}- | |
| - name: Cache RocketMQ native deps | |
| uses: actions/cache@v4 | |
| with: | |
| path: | | |
| deps/rocketmq/bin | |
| deps/rocketmq/libs/signature/lib | |
| deps/rocketmq/tmp_down_dir | |
| key: rocketmq-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('deps/rocketmq/build.sh', 'deps/rocketmq/CMakeLists.txt') }} | |
| restore-keys: | | |
| rocketmq-${{ runner.os }}- | |
| - name: Cache addon build | |
| id: cache-addon | |
| uses: actions/cache@v4 | |
| with: | |
| path: build/rocketmq.node | |
| key: addon-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('lib/**', 'CMakeLists.txt', 'package.json') }} | |
| restore-keys: | | |
| addon-${{ runner.os }}- | |
| - name: Install npm dependencies | |
| if: steps.cache-npm.outputs.cache-hit != 'true' | |
| run: npm install --no-audit --prefer-offline | |
| - name: Build RocketMQ native deps | |
| if: steps.cache-addon.outputs.cache-hit != 'true' | |
| run: ./deps/rocketmq/build.sh | |
| - name: Build addon | |
| if: steps.cache-addon.outputs.cache-hit != 'true' | |
| run: | | |
| npx cmake-js compile --CDCMAKE_BUILD_TYPE=Release \ | |
| --CDCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \ | |
| --CDCMAKE_OSX_DEPLOYMENT_TARGET=11 \ | |
| --CDCMAKE_SKIP_DEPENDENCY_TRACKING=ON | |
| - name: Package artifact | |
| run: | | |
| mkdir -p Release | |
| strip -S -x build/rocketmq.node | |
| cp build/rocketmq.node Release/darwin-universal-rocketmq.node | |
| - name: Upload artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: darwin-universal | |
| path: Release/darwin-universal-rocketmq.node | |
| release: | |
| needs: [linux-gnu-x64, linux-gnu-arm64, linux-musl-x64, linux-musl-arm64, macos-universal] | |
| runs-on: ubuntu-latest | |
| if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') | |
| permissions: | |
| contents: write | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Download all artifacts | |
| uses: actions/download-artifact@v4 | |
| with: | |
| path: artifacts | |
| - name: Prepare Release directory | |
| run: | | |
| mkdir -p Release | |
| find artifacts -name "*.node" -exec cp {} Release/ \; | |
| ls -lh Release/ | |
| - name: Create Release | |
| uses: softprops/action-gh-release@v1 | |
| with: | |
| tag_name: ${{ github.ref_name }} | |
| name: ${{ github.ref_name }} | |
| draft: false | |
| prerelease: false | |
| files: Release/*.node | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| publish-npm: | |
| needs: release | |
| runs-on: ubuntu-latest | |
| if: (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')) || (github.event_name == 'workflow_dispatch' && inputs.publish_npm) | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 | |
| registry-url: 'https://registry.npmjs.org' | |
| cache: 'npm' | |
| cache-dependency-path: package-lock.json | |
| - name: Download all artifacts | |
| uses: actions/download-artifact@v4 | |
| with: | |
| path: artifacts | |
| - name: Prepare Release directory | |
| run: | | |
| mkdir -p Release | |
| find artifacts -name "*.node" -exec cp {} Release/ \; | |
| ls -lh Release/ | |
| - name: Verify version consistency | |
| if: startsWith(github.ref, 'refs/tags/v') | |
| run: | | |
| PKG_VERSION=$(node -p "require('./package.json').version") | |
| TAG_VERSION=${GITHUB_REF#refs/tags/v} | |
| if [ "$PKG_VERSION" != "$TAG_VERSION" ]; then | |
| echo "❌ Version mismatch: package.json=$PKG_VERSION, tag=$TAG_VERSION" | |
| exit 1 | |
| fi | |
| echo "✅ Version consistent: $PKG_VERSION" | |
| - name: Install dependencies (skip native build) | |
| run: npm ci --include=dev --ignore-scripts | |
| env: | |
| NODE_ENV: development | |
| NPM_CONFIG_PRODUCTION: "false" | |
| - name: Build TypeScript | |
| run: npm run build:ts | |
| - name: Validate build output | |
| run: | | |
| echo "Validating TypeScript build..." | |
| if [ ! -f "dist/index.js" ]; then | |
| echo "❌ dist/index.js not found" | |
| exit 1 | |
| fi | |
| if [ ! -f "dist/index.d.ts" ]; then | |
| echo "❌ dist/index.d.ts not found" | |
| exit 1 | |
| fi | |
| echo "✅ TypeScript build verified" | |
| echo "Validating native addons..." | |
| for f in linux-x86_64-gnu linux-aarch64-gnu linux-x86_64-musl linux-aarch64-musl darwin-universal; do | |
| if [ ! -f "Release/${f}-rocketmq.node" ]; then | |
| echo "❌ Missing: ${f}-rocketmq.node" | |
| exit 1 | |
| fi | |
| done | |
| echo "✅ All native addons present" | |
| - name: Verify package contents | |
| run: | | |
| echo "=== Package contents ===" | |
| npm pack --dry-run | |
| echo "" | |
| echo "=== Release binaries ===" | |
| ls -lh Release/ | |
| - name: Publish to npm | |
| run: npm publish --ignore-scripts | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |