Updating JSON objects used in a GitHub matrix
I have been using GitHub workflows and found that I can dynamically build JSON objects and use them to run GitHub workflow job variations using the GitHub matrix strategy. Each new workflow and each dynamically built JSON object can require some troubleshooting, especially if customizations are made compared to other workflows.
Note
This is a follow up to the Updating JSON objects with JQ blog when using JSON objects within a GitHub workflow.
GitHub Actions Project Matrix¶
Some of my project Build Workflows use the GitHub matrix strategy to process individual builds.
Sometimes I will need to troubleshoot the PROJECT_GH_MATRIX variable locally; this post provides
information about using the PROJECT_GH_MATRIX variable locally when working with the JSON objects.
Capturing the Project Matrix from GitHub Actions
When you are reviewing the output within GitHub Actions, you may see the PROJECT_GH_MATRIX variable.
Copy the output from the variable to your clipboard.
Example converting the GitHub matrix output for local use¶
Given this output from a GitHub workflow:
PROJECT_GH_MATRIX: {"project":["my-test-abc","dev-jkl","primary-test-project-xyz"],"include":[{"project":"my-test-abc","files":["my-test-abc/foo.txt"]},{"project":"dev-jkl","files":["dev-jkl/foo.txt"]},{"project":"primary-test-project-xyz","files":["primary-test-project-xyz/foo.txt"]}]}
In order to convert it to use locally, echo the JSON object within a command and wrap the JSON object in single quotes ' and pipe the output to jq . -c; see the example below:
Storing the Project Matrix output for local use
Storing the output for local use:
PROJECT_GH_MATRIX=$(echo '{"project":["my-test-abc","dev-jkl","primary-test-project-xyz"],"include":[{"project":"my-test-abc","files":["my-test-abc/foo.txt","my-test-abc/bar.log"]},{"project":"dev-jkl","files":["dev-jkl/foo.txt","dev-jkl/bar.log","dev-jkl/baz.log"]},{"project":"primary-test-project-xyz","files":["primary-test-project-xyz/foo.txt","primary-test-project-xyz/bar.log","primary-test-project-xyz/baz.txt"]}]}' | jq . -c)
Then you can use the JSON object within jq
echo $PROJECT_GH_MATRIX | jq .
echo $PROJECT_GH_MATRIX | jq
echo $PROJECT_GH_MATRIX | jq -r '.project'
echo $PROJECT_GH_MATRIX | jq -c
Extract project names with a space delimiter¶
Extract project names with a space delimiter
Extract project names as individual lines¶
Extract project names as individual lines
# Extract project names as individual lines
echo "$PROJECT_GH_MATRIX" | jq -r '.project[]'
PROJECTS_LINE_DELIMITED=$(echo "$PROJECT_GH_MATRIX" | jq -r '.project[]')
# Use in a for loop with while read
while read -r project; do
echo "Processing project: $project"
# Your processing logic here
done <<< "$PROJECTS_LINE_DELIMITED"
Running the first command produces: