Search code examples
githubgithub-actions

simplfying github actions ci.yml for version testing


My ci.yml for GitHub Actions has this line in it:

-   name: Make Tests Compatiable With PHPUnit 9+
    if: matrix.php-version != '5.3' && matrix.php-version != '5.4' && matrix.php-version != '5.5' && matrix.php-version != '5.6' && matrix.php-version != '7.0' && matrix.php-version != '7.1' && matrix.php-version != '7.2'
    run: php tests/make_compatible_with_phpunit9.php

My question is... is there a way to make that simpler?

I thought matrix.php-version < '7.3' might work but it doesn't.

matrix.php-version != ['5.3', '5.4', '5.5', '5.6', '7.0', '7.1', '7.2'] obviously won't work because matrix.php-version isn't an array. After reading https://docs.github.com/en/github-ae@latest/actions/learn-github-actions/expressions I thought if: contains(['5.3', '5.4', '5.5', '5.6', '7.0', '7.1', '7.2'], matrix.php-version) would work but that got me this error:

Unexpected symbol: '['. Located at position 10 within expression: contains(['5.3', '5.4', '5.5', '5.6', '7.0', '7.1', '7.2'], matrix.php-version)

I tried if: contains(fromJSON("['5.3', '5.4', '5.5', '5.6', '7.0', '7.1', '7.2']"), matrix.php-version) but that got me this error:

Unexpected symbol: '"'. Located at position 19 within expression: contains(fromJSON("['5.3', '5.4', '5.5', '5.6', '7.0', '7.1', '7.2']"), matrix.php-version)

Is what I had in my first code snippet the simplest that I can possibly make this or am I just doing something wrong?


Solution

  • According to jobs.<job_id>.steps[*].if:

    When you use expressions in an if conditional, you may omit the expression syntax (${{ }}) because GitHub automatically evaluates the if conditional as an expression. For more information, see "Expressions."

    And, from Expressions' Literals:

    string: You don't need to enclose strings in ${{ and }}. However, if you do, you must use single quotes (') around the string. To use a literal single quote, escape the literal single quote using an additional single quote (''). Wrapping with double quotes (") will throw an error.

    So, you need to use single quotes to enclose that array to pass as a string literal:

    if: contains(fromJSON('["5.3", "5.4", "5.5", "5.6", "7.0", "7.1", "7.2"]'), matrix.php-version)
    

    The example under contains function also highlights this:

    contains(fromJSON('["push", "pull_request"]'), github.event_name)
    

    You might want to lint your workflow(s) with https://rhysd.github.io/actionlint/ to identify issues much faster. See the linting result of your current workflow here.