What is the correct if condition syntax for checking matrix.os version?

Hi,

I previously used travis with a build matrix and could check in if condtions for the os-version/type, however in GithubActions I couldn’t figure out what the correct syntax is:

I tested both with and without expression braces:

- if: matrix.os == 'ubuntu-latest'
      run: |
        ionic cordova platform add android
        ionic cordova build android

and something like:

- if: ${{ matrix.os == 'macOS-10.14'}}
run: | ionic cordova platform add ios
              ionic cordova build ios

Both give me errors:

- Your workflow file was invalid: .github/workflows/main.yml (Line: 33, Col: 11): Unrecognized named-value: 'matrix'. Located at position 1 within expression: matrix.os == 'macOS-10.14'

I figured it out and now it works:

- if: matrix.os == 'macOS-10.14'
      run: |
        ionic cordova platform add ios
        ionic cordova build ios

 - if: matrix.os == 'ubuntu-latest'
      run: |
        ionic cordova platform add android
        ionic cordova build android
4 Likes

Does’t work.  I get “You have an error in your yaml syntax on line 30” with this:

      - name: Install Depencies unless Windows

        - if: matrix.os != ‘windows-latest’

          run: cpanm -iqn BSD::Resource

Of course, it’s not helpful that Github doesn’t tell you what the syntax error is, or the place in the line that the error occured.

- name: run on mac
      if: startsWith(matrix.os, 'macOS')
      run: |
        ionic cordova platform add ios
        ionic cordova build ios
        mkdir -pv output
    - name: run on linux
      if: startsWith(matrix.os,'ubuntu')
      run: |
        npm run test-headless
        ionic cordova platform add android
        ionic cordova build android
        mkdir -pv output
        readlink -f output

This works fine for me. Maybe your indentation is wrong?

3 Likes

I checked and double checked the indentation.  Looks right to me.  And there are no tabs either.

Looking more, the indentation isn’t clear to me.  In your answer the ‘run’ is under the ‘if’; whereas in the ‘solved’ response it’s indented by four spaces.  I indented by two.

That was it.  It wants no indend, the “solved” solution is wrong.

1 Like

I think it could be a copy paste problem with the forum here which killed the right indentations