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

如何在GitHub Action中复用构建环境并行运行测试

实现GitHub Actions中测试任务并行并复用构建环境

问题场景

现有GitHub Actions工作流,build任务包含安装依赖、缓存第三方库、构建项目,之后串行执行test1和test2。希望将test1和test2改为并行执行,同时复用build阶段的构建产物和依赖环境。

此前尝试拆分出独立的test1、test2任务并设置needs: [build],但未成功——原因不仅是缺少runs-on配置,更核心的是每个Job都是独立的虚拟机实例,needs仅控制执行顺序,不会自动共享前序Job的环境和文件。

最简解决方案:用工件传递构建产物+缓存复用依赖

步骤1:修改build任务,上传构建产物为工件

在build任务的最后添加步骤,将构建产物上传为GitHub Actions工件,这样后续测试任务可以下载使用:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      # 原有依赖安装、缓存libsodium、构建步骤保持不变
      - name: install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libsystemd-dev

      - name: cache libsodium-1.0.18
        id: libsodium
        uses: actions/cache@v2
        with:
          path: ~/libsodium-stable
          key: ${{ runner.os }}-libsodium-1.0.18

      - name: Install cache libsodium-1.0.18
        if: steps.libsodium.outputs.cache-hit == 'true'
        run: cd ~/libsodium-stable && ./configure && make -j2 && sudo make install

      # 省略其他安装、构建步骤...
      - name: build project
        run: <build the project> # 替换为实际构建命令

      # 新增:上传构建产物作为工件
      - name: Upload build artifacts
        uses: actions/upload-artifact@v3
        with:
          name: build-output
          path: |
            dist/ # 替换为你的实际构建产物目录
            # 可添加测试需要的其他文件,如配置、测试依赖等

步骤2:定义并行的test1、test2任务

每个测试任务需要:

  • 指定runs-on(和build一致的系统)
  • 复用build的依赖缓存(避免重复安装)
  • 下载build上传的工件
  • 执行测试命令
test1:
    runs-on: ubuntu-latest
    needs: build # 等待build任务完成后执行
    steps:
      # 复用build的依赖安装逻辑,配合缓存减少耗时
      - name: install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libsystemd-dev

      - name: cache libsodium-1.0.18
        id: libsodium
        uses: actions/cache@v2
        with:
          path: ~/libsodium-stable
          key: ${{ runner.os }}-libsodium-1.0.18

      - name: Install cache libsodium-1.0.18
        if: steps.libsodium.outputs.cache-hit == 'true'
        run: cd ~/libsodium-stable && ./configure && make -j2 && sudo make install

      # 下载build阶段的构建产物
      - name: Download build artifacts
        uses: actions/download-artifact@v3
        with:
          name: build-output
          path: ./ # 下载到当前工作目录

      - name: run test1
        run: <run test1> # 替换为实际测试命令

  test2:
    runs-on: ubuntu-latest
    needs: build
    steps:
      # 和test1完全一致的依赖安装、缓存、下载步骤
      - name: install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libsystemd-dev

      - name: cache libsodium-1.0.18
        id: libsodium
        uses: actions/cache@v2
        with:
          path: ~/libsodium-stable
          key: ${{ runner.os }}-libsodium-1.0.18

      - name: Install cache libsodium-1.0.18
        if: steps.libsodium.outputs.cache-hit == 'true'
        run: cd ~/libsodium-stable && ./configure && make -j2 && sudo make install

      - name: Download build artifacts
        uses: actions/download-artifact@v3
        with:
          name: build-output
          path: ./

      - name: run test2
        run: <run test2> # 替换为实际测试命令

优化:用可复用工作流减少代码冗余

如果测试任务的依赖安装、缓存、下载步骤完全一致,可以抽成可复用工作流,避免代码重复:

  1. 创建.github/workflows/reusable-test-setup.yml:
name: Reusable Test Setup
on:
  workflow_call:

jobs:
  test-setup:
    runs-on: ubuntu-latest
    steps:
      - name: install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libsystemd-dev

      - name: cache libsodium-1.0.18
        id: libsodium
        uses: actions/cache@v2
        with:
          path: ~/libsodium-stable
          key: ${{ runner.os }}-libsodium-1.0.18

      - name: Install cache libsodium-1.0.18
        if: steps.libsodium.outputs.cache-hit == 'true'
        run: cd ~/libsodium-stable && ./configure && make -j2 && sudo make install

      - name: Download build artifacts
        uses: actions/download-artifact@v3
        with:
          name: build-output
          path: ./
  1. 在主工作流中调用:
jobs:
  build:
    # 原有的build任务(包含上传工件步骤)...

  test1:
    runs-on: ubuntu-latest
    needs: build
    uses: ./.github/workflows/reusable-test-setup.yml
    steps:
      - name: run test1
        run: <run test1>

  test2:
    runs-on: ubuntu-latest
    needs: build
    uses: ./.github/workflows/reusable-test-setup.yml
    steps:
      - name: run test2
        run: <run test2>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 07:00:59