joshtronic

Posted in Software Development #DevOps #Version Control #PHP

Only run step for specific version with GitHub Actions

With my recent exploration of GitHub Actions, I've had to figure out things that I've already figured out with other systems like GitLab Runners and Travis CI.

One such thing is conditionally running steps. For things like code coverage, I'm only interested in sending off the coverage report to the coverage analysis service for the most recent stable version of the platform I'm testing on.

In this situation, I'm testing against PHP versions 5.3 (old end-of-life version) through 7.4 (latest stable) and every version in between.

If I were to just let things fly on every version, I would be shipping off code coverage reports for nearly 10 different versions. Not only that, but since things run in parallel, there's no way of knowing which version was represented in the code coverage analysis.

All good though, as GitHub Actions has workflow syntax to conditionally run a step. The syntax is the if: property which can be fed a conditional.

For reference, my strategy and matrix looks something like this:

strategy:
  matrix:
    php-version: ['5.3', '...others...', '7.4']

Because I have matrix defined, my steps will run for each value in the php-version array. The specific version of PHP will be available in the matrix variable which can be used in the conditional like so:

- name: Upload Coverage
  if: ${{ matrix.php-version == '7.4' }}

Not sure if I could have gotten more creative and dropped the hard coded version in favor of referencing the last version in the array or not. This was sufficient enough though, and easy enough to update in the future.

Now if only I could figure out how to include PHP v8 (nightly) and have GitHub Actions treat it as a soft failure similar to Travis CI's allow_failures option.