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

GitHub Actions并行测试构建,仅测试成功后部署的实现

GitHub Actions 优化:并行测试与构建,独立部署并传递构建数据

核心需求实现步骤

  1. 并行执行测试与构建:移除build任务对tests的依赖,让两者同时启动;新增deploy任务,仅在tests所有实例和build所有实例全部成功后执行。
  2. 传递构建数据到部署任务:在build任务中定义输出变量,将镜像标签、应用名称等关键数据暴露出来,供deploy任务获取。

修改后的完整配置

jobs:
  tests:
    name: run tests
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres

    strategy:
      matrix:
        org: [web, api]
        include:
          - org: web
            appname: web
          - org: api
            appname: api
    steps:
      - name: run tests
        env:
          TEST_DATABASE_URL: "postgres://postgres:postgres@localhost:5432/postgres"
        run: yarn rw test ${{ matrix.appname }} --no-watch

  build:
    name: build-and-push-image
    runs-on: ubuntu-latest
    # 移除对tests的依赖,实现并行执行
    # needs: tests
    strategy:
      matrix:
        org: [web, api]
        include:
          - org: web
            dockerfile: ./web/Dockerfile
            appname: web
            apptokensecret: DEVWEB_APP_TOKEN
            image: dockerregistry.xyz/ffy/FFYBuildFormWeb
          - org: api
            dockerfile: ./api/Dockerfile
            appname: api
            apptokensecret: DEVAPI_APP_TOKEN
            image: dockerregistry.xyz/ffy/FFYBuildFormApi
    # 定义任务输出,供deploy任务获取构建数据
    outputs:
      appname_${{ matrix.appname }}: ${{ matrix.appname }}
      image_tags_${{ matrix.appname }}: ${{ steps.meta.outputs.tags }}
    steps:
      - name: Log in to the Container registry
        uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
        with:
          registry: ${{ secrets.REGISTRY_DOMAIN }}
          username: ${{ secrets.REGISTRY_USER }}
          password: ${{ secrets.REGISTRY_PASSWORD }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
        with:
          images: ${{ matrix.image }}

      - name: Build and push Docker image
        uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
        with:
          context: .
          file: ${{ matrix.dockerfile }}
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

  # 新增独立的部署任务
  deploy:
    name: deploy-images
    runs-on: ubuntu-latest
    # 等待所有测试和构建实例完成后执行
    needs: [tests, build]
    strategy:
      matrix:
        app: [web, api]
    steps:
      - name: Deploy image
        uses: floms/action-caprover@v1
        with:
          host: "${{ secrets.CAPROVER_SERVER }}"
          password: "${{ secrets.CAPROVER_PASSWORD }}"
          app: ${{ steps.branch-name.outputs.current_branch }}${{ needs.build.outputs.appname_${{ matrix.app }} }}
          image: ${{ needs.build.outputs.image_tags_${{ matrix.app }} }}

关键改动说明

  • 并行执行优化:删除build任务中的needs: tests配置,tests和build会同时触发执行,大幅缩短整体流水线耗时;deploy任务通过needs: [tests, build]确保仅在所有测试和构建环节都成功后才启动。
  • 构建数据传递:在build任务的outputs中,用${{ matrix.appname }}作为输出键的后缀(如appname_web、image_tags_api),确保每个矩阵实例的构建数据能被唯一识别;deploy任务通过needs.build.outputs的动态引用,精准获取对应应用的构建数据。
  • 独立部署逻辑:deploy任务沿用矩阵遍历web和api,复用原有部署逻辑,保证两个应用的部署操作独立执行,避免耦合。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 09:07:11