I’ve been having more fun with GitHub actions after Automating Code Changes via GitHub Actions Making Pull Requests. Let’s generate a job matrix from a list of files.
Why would I need that? In opensearch-project/opensearch-build we create manifest files that are used to produce an OpenSearch distribution. These files are created manually, one for every version. Each needs to be sanity-checked when created or changed.
These checks can be executed in parallel, so we can create a GitHub Actions matrix like so.
We’ll definitely forget to update the matrix when a new file is created, so let’s just list those files dynamically, and generate a matrix from the list.
Here’s how this works.
- A shell command
ls manifests/*.ymllists all .yml files.
- A pipe to
| jq -R -s -c 'split("\n")[:-1]'transforms the file list into a JSON array (from StackOverflow#10234327). Note that jq is installed on all GHA Linux images.
matrixoutput is set to the JSON array of files using set-output with
echo "::set-output name=matrix::value.
manifestvalues are loaded from the JSON array using fromJson and become part of the updated workflow.
This is so awesome that it generates the matrix during the build!
See opensearch-project/opensearch-build#386 for a working example.