Using 'if' in GitHub actions workflow

I am trying to run a job on different runners using matrix configuration. Runners names are given as matrix parameters. I want to run a step only for a particular runner

I am using a if statement checking the runners name but the step is run even when the if is false. Example code is:

name: Testing if
on: push
jobs:
    Build_testing_with_if:
      strategy:
            matrix:
                platform: [ubuntu-latest, ubuntu-18.04]
      runs-on: ${{ matrix.platform }}
      steps:
        - name: Check out repository
          uses: actions/checkout@v2
          
        - if: ${{matrix.platform}} == 'ubuntu-latest'
          name: Print          
          run: echo "matrix platform is" ${{matrix.platform}}

I expect the print to happen when running on ‘ubuntu-latest’ only but it is printed for both runners. How can I run the step only for one runner?

The problem is that you have only matrix.platform wrapped in ${{ ... }}. That marks it as an expression to evaluate, and the rest of the line is implicitly treated as text. And a non-empty string evaluates to “true”.

So, either wrap the whole expression you want to be evaluated:

- if: ${{ matrix.platform == 'ubuntu-latest' }}

Or (special rule for if) just leave the ${{ ... }} and the whole thing will be treated as an expression:

- if: matrix.platform == 'ubuntu-latest'
2 Likes