Github Actions矩阵中Windows任务无输出,如何关联test_type变量?
问题分析与解决方案
错误原因
你当前的matrix配置中,include添加的Windows条目只指定了os和python-version,没有包含test_type变量。这会导致生成的Windows任务里不存在matrix.test_type,两个步骤的if判断条件都无法成立,自然不会执行任何输出步骤。
而你期望的是生成Windows × py3.8 × test-alpha和Windows × py3.8 × test-beta两个组合任务,所以必须让这两个Windows任务也带上对应的test_type值。
修复方案一:在include中添加对应test_type的条目
直接在include里为每个test_type单独配置Windows + py3.8的组合:
name: Print test type on: [push] jobs: build: strategy: fail-fast: false matrix: test_type: ['test-alpha', 'test-beta'] os: [ubuntu-latest] python-version: ['3.7', '3.9'] include: - os: windows-latest python-version: '3.8' test_type: 'test-alpha' - os: windows-latest python-version: '3.8' test_type: 'test-beta' runs-on: ${{ matrix.os }} steps: - name: Print Alpha if: ${{ matrix.test_type == 'test-alpha'}} run: echo "test type is alpha. the os name is ${{ runner.os }}" - name: Print Beta if: ${{ matrix.test_type == 'test-beta'}} run: echo "test type is beta. the os name is ${{ runner.os }}"
这样会生成6个任务:
- Ubuntu × py3.7 × test-alpha
- Ubuntu × py3.7 × test-beta
- Ubuntu × py3.9 × test-alpha
- Ubuntu × py3.9 × test-beta
- Windows × py3.8 × test-alpha
- Windows × py3.8 × test-beta
完全符合你的需求。
修复方案二:调整矩阵结构,用exclude+include实现
如果后续需要扩展更多test_type,这种方式更简洁:先让test_type与所有os、python-version组合,再排除Windows不需要的python版本,最后补充Windows对应的py3.8:
name: Print test type on: [push] jobs: build: strategy: fail-fast: false matrix: test_type: ['test-alpha', 'test-beta'] os: [ubuntu-latest, windows-latest] python-version: ['3.7', '3.9'] exclude: # 排除Windows与py3.7、py3.9的组合 - os: windows-latest python-version: '3.7' - os: windows-latest python-version: '3.9' include: # 补充Windows与py3.8的组合 - os: windows-latest python-version: '3.8' runs-on: ${{ matrix.os }} steps: - name: Print Alpha if: ${{ matrix.test_type == 'test-alpha'}} run: echo "test type is alpha. the os name is ${{ runner.os }}" - name: Print Beta if: ${{ matrix.test_type == 'test-beta'}} run: echo "test type is beta. the os name is ${{ runner.os }}"
这种方式生成的任务和方案一完全一致,且后续新增test_type时,不需要修改include部分,会自动生成对应的Windows任务。
内容的提问来源于stack exchange,提问作者arunppsg
相关产品推荐
相关产品推荐

