Create matrix with multiple OS and env for each one

I’m trying to create something that share the same commands, and each OS can configure the commands via env variables. Is such thing possible ? I’m trying with this approach but it’s failing:

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          os: macos-latest
            env:
              - TARGET: x86_64-apple-darwin
              - COMPILER: clang
              - LINKER: clang

          os: ubuntu-latest
            env:
              - TARGET: armv7-unknown-linux-musleabihf
              - COMPILER: arm-linux-gnueabihf-gcc-5
              - LINKER: gcc-5-arm-linux-gnueabihf

          os: ubuntu-latest
            env:
              - TARGET: x86_64-unknown-linux-musl
              - COMPILER: gcc
              - LINKER: gcc
2 Likes

I am very glad to see you here. According to your scenario, I would suggest you use including configurations in a matrix build.

 You can set os as matrix and then use include keyword to specify TARGET, COMPILER and LINKER. In steps, you can use ${{ matrix.TARGET }} to get the value. Please check the example, it will create three jobs::

name: test matrix
on: push
jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [macos-latest, ubuntu-latest, ubuntu-latest]
        include:
        - os: macos-latest
          TARGET: x86_64-apple-darwin
          COMPILER: clang
          LINKER: clang

        - os: ubuntu-latest
          TARGET: armv7-unknown-linux-musleabihf
          COMPILER: arm-linux-gnueabihf-gcc-5
          LINKER: gcc-5-arm-linux-gnueabihf

        - os: ubuntu-latest
          TARGET: x86_64-unknown-linux-musl
          COMPILER: gcc
          LINKER: gcc
    steps:
     - uses: actions/checkout@v1
     - run: echo ${{matrix.TARGET}}

For matrix syntax, please refer to this official document:  https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#example-including-configurations-in-a-matrix-build

8 Likes

Thank you very much @yanjingzhu 

This is good and satisfactory

Just tested this configuration and have to say it does not work. Take a look at screenshots:

Both jobs are x86_64-unknown-linux-musl

Correct workflow is:

name: test matrix
on: push
jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:

        include:
          - os: macos-latest
            TARGET: x86_64-apple-darwin
            COMPILER: clang
            LINKER: clang

          - os: ubuntu-latest
            TARGET: armv7-unknown-linux-musleabihf
            COMPILER: arm-linux-gnueabihf-gcc-5
            LINKER: gcc-5-arm-linux-gnueabihf

          - os: ubuntu-latest
            TARGET: x86_64-unknown-linux-musl
            COMPILER: gcc
            LINKER: gcc
    steps:
      - uses: actions/checkout@v1
      - run: echo ${{matrix.TARGET}}

Do not add os as proposed above

1 Like

As far as I’ve seen in a little experiment on my own, using the same key multiple times under include (here: ubuntu-latest) overwrites the job configuration and the last entry wins. That is, the matrix is configured for 1x macOS and 2x Ubuntu, but first Ubuntu configuration is overwritten by the second, so both end up with TARGET: x86_64-unknown-linux-musl etc.

@sergeyklay’s solution to use include only and no other keys under matrix is the best option IMO. Alternatively, one could use a unique key, but this solution doesn’t add anything over using include only:

build: [1, 2, 3]
include:
  - build: 1
    os: macos-latest
    ...

  - build: 2
    os: ubuntu-latest
    ...

  - build: 3
    os: ubuntu-latest
    ...
1 Like