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

如何让GitHub Actions矩阵作业全部完成测试后再执行发布步骤?

解决GitHub Action中等待所有矩阵测试完成后再发布的问题

问题描述

现有一个GitHub Action工作流,用于跨多平台和Python版本构建并发布Python包到PyPI。当前配置中,每个矩阵作业的测试步骤完成后会直接执行发布步骤,希望修改为所有矩阵作业的测试步骤全部成功后,再执行发布操作,避免单个作业测试失败时仍发布wheel包。

原缩写配置:

jobs:
  build:
    name: build py3.${{ matrix.python-version }} on ${{ matrix.platform || matrix.os }}
    strategy:
      fail-fast: true
      matrix:
        os:
          - ubuntu
          - macos
          - windows
        python-version:
          - "8"
          - "9"
          - "10"
        include:
          - os: ubuntu
            platform: linux
          - os: windows
            ls: dir

    runs-on: ${{ format('{0}-latest', matrix.os) }}
    steps:
      - uses: actions/checkout@v3

      # 所有矩阵作业必须成功完成的步骤
      - name: test
      ...

      # 我希望等待所有矩阵作业成功完成测试后再执行此步骤
      - name: release
      ...

实现方案

核心思路是将发布步骤从build矩阵作业中拆分出来,单独创建一个release作业,并通过needs关键字让这个新作业依赖所有build矩阵作业的成功完成。具体修改如下:

  1. 移除build作业中的release步骤,保留测试及构建相关步骤;
  2. 添加独立的release作业,通过needs: build指定依赖所有build矩阵作业;
  3. 通过工件上传/下载功能,将所有build作业生成的wheel包传递给release作业统一发布。

修改后的完整配置示例:

jobs:
  build:
    name: build py3.${{ matrix.python-version }} on ${{ matrix.platform || matrix.os }}
    strategy:
      fail-fast: true
      matrix:
        os:
          - ubuntu
          - macos
          - windows
        python-version:
          - "8"
          - "9"
          - "10"
        include:
          - os: ubuntu
            platform: linux
          - os: windows
            ls: dir

    runs-on: ${{ format('{0}-latest', matrix.os) }}
    steps:
      - uses: actions/checkout@v3

      # 构建wheel包(根据实际项目调整命令)
      - name: build wheel
        run: |
          python -m pip install --upgrade pip wheel
          python setup.py bdist_wheel

      # 测试步骤
      - name: test
        run: |
          # 替换为你的实际测试命令,比如pytest
          python -m pytest tests/

      # 上传构建好的wheel包作为工件,供release作业使用
      - name: upload wheel artifact
        uses: actions/upload-artifact@v3
        with:
          name: python-wheels
          path: dist/*.whl

  release:
    name: release to PyPI
    runs-on: ubuntu-latest
    # 依赖所有build矩阵作业,仅当全部成功时才触发
    needs: build
    steps:
      - uses: actions/checkout@v3

      # 下载所有build作业上传的wheel包
      - name: download wheel artifacts
        uses: actions/download-artifact@v3
        with:
          name: python-wheels
          path: dist/

      # 执行PyPI发布(根据实际工具调整,比如用twine)
      - name: release to PyPI
        run: |
          python -m pip install --upgrade pip twine
          twine upload dist/*.whl
        env:
          TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
          TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}

关键说明

  • needs: build:当build是矩阵作业时,needs会自动等待所有矩阵实例完成且成功,才会启动release作业;
  • 工件传递:通过upload-artifact和download-artifact实现跨作业的文件传递,确保发布的是所有平台的构建产物;
  • fail-fast: true:原配置中的该参数会在任一矩阵作业失败时立即终止其他作业,配合needs能快速终止流程,避免无效操作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 00:22:34