GitHub Actions Day 14: Conditionals with a Matrix
This is day 14 of my GitHub Actions Advent Calendar. If you want to see the whole list of tips as they're published, see the index.
GitHub Actions has a lot of components that are powerful on their own -- but when you start to use them together, that's when things start to get really powerful. For example: matrix workflows let you easily expand a simple workflow across multiple different jobs. And conditional execution lets you limit the execution of steps within a job.
These two features combine pretty naturally -- when you're building a matrix across different operating systems, platforms, or language versions, you might need to run some steps on only a subset of that matrix. For example: you might need to install a different compiler when you're running on Linux, or you might need to install slightly different dependencies for the different operating systems.
I can combine a few previous concepts to build a workflow for one of my projects, a systems library in C. It will use matrix workflows across platforms and tool installation to perform build and test steps for my CI.
The goal is to install the Ninja build system, then use CMake to
create build scripts to take advantage of that -- CMake and Ninja
work great together to produce fast, cross-platform native builds.
Finally, we'll run the build with
cmake and run our tests with
When this runs, the conditionals ensure that only the appropriate "Install Dependencies" step will run for the particular platform. The other steps for the other platforms will be skipped.
Now we're starting to see how we can combine the simple pieces of GitHub Actions into more complex and powerful workflows.