Matrix Build#

这是在你进行并行执行时候传递参数用的. 例如你的 Job 有两个参数 OS, 和 Python version. 而你需要同时在 2 个 OS (windows / linux), 以及 3 个 Python version (3.8, 3.9, 3.10) 上运行. 这时就可以用 Matrix build 来实现对 Job 并行执行.

Sample Workflow Definition#

 1# ------------------------------------------------------------------------------
 2# Demonstrate how to run parameterized job in parallel using matrix
 3#
 4# Reference:
 5#
 6# - Using a matrix for your jobs: https://docs.github.com/en/actions/using-jobs/using-a-matrix-for-your-jobs
 7# ------------------------------------------------------------------------------
 8name: 01_07_matrix_build
 9
10on:
11  workflow_dispatch:
12
13jobs:
14  tests:
15    strategy:
16      # Using a matrix for your jobs: https://docs.github.com/en/actions/using-jobs/using-a-matrix-for-your-jobs
17      matrix:
18        # for all available VM runtime, see this: https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources
19        os: ["ubuntu-latest", "windows-latest"]
20        # for all available Python version, see this: https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners/about-github-hosted-runners#supported-software
21        python-version: ["3.9", "3.10"]
22    name: "${{ matrix.os }} Python ${{ matrix.python-version }}"
23    runs-on: "${{ matrix.os }}"
24    steps:
25      - uses: "actions/checkout@v3" # https://github.com/marketplace/actions/checkout
26      - uses: "actions/setup-python@v4" # https://github.com/marketplace/actions/setup-python
27        with:
28          python-version: "${{ matrix.python-version }}"
29      - name: "Install common dependencies"
30        run: |
31          echo "Install common dependencies"
32      # Sometimes you need special handling for different OS, or Python version
33      # you can use if statement to do that
34      - name: "Install special dependencies on Linux"
35        if: matrix.os == 'ubuntu-latest'
36        run: |
37          echo "Install special dependencies on Linux"
38      - name: "Install dependencies on Windows"
39        if: matrix.os == 'windows-latest'
40        run: |
41          echo "Install dependencies on Windows"

Sample Workflow Run#

../../_images/matrix_build.png