You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitHub Actions跨平台构建作业如何共享重复执行步骤?

跨平台构建步骤复用方案

GitHub Actions原生提供两种无外部依赖的方案,可替代YAML锚点实现跨作业步骤复用:

本地复合动作(推荐,适配当前单项目场景)

将两组作业完全重复的步骤封装为项目本地的复合动作,无需发布到外部仓库,两个构建作业直接调用并传入矩阵参数即可。

操作步骤

  • 在项目根目录新建路径.github/actions/build-common/action.yml,内容如下:
    name: 公共构建流程
    description: 适配Ubuntu、macOS的通用构建步骤
    inputs:
      cc:
        description: 编译器版本(仅Ubuntu需要传入)
        required: false
        default: ""
      use_burrow:
        description: 是否禁用Burrow依赖
        required: true
      debrel:
        description: 构建配置类型
        required: true
    runs:
      using: "composite"
      steps:
        - name: macOS安装基础依赖
          run: brew install cmake ninja coreutils
          shell: bash
          if: ${{ runner.os == 'macOS' }}
        - name: 拉取项目代码
          uses: actions/checkout@v2
        - name: 缓存vcpkg依赖
          uses: actions/cache@v2
          with:
            path: |
              build-vcpkg
              build/vcpkg_installed
              $HOME/.cache/vcpkg
            key: ${{ runner.os }}-vcpkg-${{ github.sha }}
            restore-keys: ${{ runner.os }}-vcpkg-
        - name: 构建vcpkg依赖
          run: ./vcpkg/build_iroha_deps.sh $PWD/build-vcpkg
          shell: bash
        - name: CMake配置
          run: |
            if [ -n "${{ inputs.cc }}" ]; then
              export CC=${{ inputs.cc }}
              export CXX=$(echo ${{ inputs.cc }} | sed -e 's,gcc,g++,g' -e 's,clang,clang++,g')
            fi
            cmake -B build -DCMAKE_TOOLCHAIN_FILE=$PWD/build-vcpkg/scripts/buildsystems/vcpkg.cmake \
               ${{ inputs.use_burrow }} -GNinja
          shell: bash
        - name: CMake编译
          run: cmake --build build --config ${{ inputs.debrel }}
          shell: bash
    
  • 替换原有两个构建作业的重复步骤为调用上述复合动作的逻辑,修改后的作业配置如下:
    build-iroha-ubuntu:
      needs: Docker-iroha-builder
      runs-on: ubuntu-latest
      container: ikyb/iroha:builder
      strategy:
        fail-fast: false
        matrix:
          cc: [ gcc-9, gcc-10, clang ]
          USE_BURROW: [ -DUSE_BURROW=OFF ]
          debrel: [ Debug ]
      steps:
        - name: 调用公共构建步骤
          uses: ./.github/actions/build-common
          with:
            cc: ${{ matrix.cc }}
            use_burrow: ${{ matrix.USE_BURROW }}
            debrel: ${{ matrix.debrel }}
    
    build-iroha-macos:
      runs-on: macos-latest
      strategy:
        fail-fast: false
        matrix:
          USE_BURROW: [ -DUSE_BURROW=OFF ]
          debrel: [ Debug, Release ]
      steps:
        - name: 调用公共构建步骤
          uses: ./.github/actions/build-common
          with:
            use_burrow: ${{ matrix.USE_BURROW }}
            debrel: ${{ matrix.debrel }}
    

可复用工作流(适配多项目共享场景)

如果后续多个仓库需要复用这套构建流程,可将公共逻辑抽成独立的可复用工作流,各项目直接调用即可。当前单项目场景下复合动作轻量性更优。

同步修正原有配置的问题

  • 原配置中Homebrew步骤的判断条件runner.os == 'MacOS'大小写错误,GitHub返回的系统标识为macOS,修正后才能正确触发macOS环境下的依赖安装。
  • 原macOS作业的矩阵未定义cc变量,原CMake配置中的${{ matrix.cc }}会出现空值,封装后的复合动作已对该参数做判空处理,避免运行报错。

内容的提问来源于stack exchange,提问作者kyb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.22 14:54:07