How to share matrix between jobs #26284
-
I’m trying to build a regression test suite that basically builds a client and a server binary from different release versions, and then runs them against each other.
As you can see, I have repeat the build matrix three times in my configuration, which is error prone when I remove / add versions there. My first idea was to just use YAML anchors here, but it turns out that GitHub Actions doesn’t support those for whatever reason. I also tried to define the matrix as an output in the
And then use it in the
This just results in a “A mapping was not expected” error message. Any idea how I can solve this? |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments
-
Hi @marten-seemann, Glad to see you in Github Community Forum! To share matrix between jobs, please follow the usage of doc here , in your code the runner job cannot get the value from ${{ toJson(matrix) }} since format issue. Please try below code for your case:
You can refer to my workflow for the details. Thanks |
Beta Was this translation helpful? Give feedback.
-
Hi @weide-zhou, thank you for your reply. I was hoping to avoid the
This code is not very readable, especially when I want to add 10 or so more configurations there. |
Beta Was this translation helpful? Give feedback.
-
Hi @marten-seemann, Since the job outputs must be string format(doc here), i’m afraid you have to pull all configurations in a line. Thanks |
Beta Was this translation helpful? Give feedback.
-
Hi @weide-zhou, I’m wondering if it’s somehow possible to use the
Unfortunately, this generates an error: And it looks like there’s no way to inspect the |
Beta Was this translation helpful? Give feedback.
-
Hi @marten-seemann, You cannot use
Hence, please put all configuration in a line with Thanks |
Beta Was this translation helpful? Give feedback.
-
Does that mean it’s not possible to have matrix jobs generated by a script? That seems like… a pretty basic thing for a CI, and I’d surprised to hear that it’s not possible on GitHub Actions. I was thinking of putting the jobs in a separate file (as JSON), and read that file from disk. But that escaping requirement sounds like I can’t do that either (at least not without a script that would add the |
Beta Was this translation helpful? Give feedback.
-
I think I figured out a way to do this: I place the configuration in a separate file,
The config uses a separate
The Note that this solution also works if the matrix is dynamically generated. For example, it would be trivial now to write a script that outputs JSON, which is then used as a matrix in a later step. |
Beta Was this translation helpful? Give feedback.
-
Actually this can be made to work, you just need a couple of minor adjustments to the code:
At the moment such abuse of the |
Beta Was this translation helpful? Give feedback.
-
https://vs-blogs.medium.com/dynamic-matrix-in-github-action-e516019cfdf5 (Approach 3) helped me |
Beta Was this translation helpful? Give feedback.
-
It took me a while to figure the best approach and here is what I propose to todo to reuse a workflow where the configuration (YAML -> JSON) is parsed and saved as "matrix" key
https://github.com/ch007m/test-github-action/blob/main/.github/workflows/consume-config-matrix.yml
Result in action: https://github.com/ch007m/test-github-action/actions/runs/5833089159 Remark: There is one caveat as we cannot copy/paste the fields defined under the steps/jobs as yaml config file should include the matrix exploded Wrong (see array declaration)
Good (array removed)
Feel free to use this trick and to share it with others :-) |
Beta Was this translation helpful? Give feedback.
-
Yaml anchors would be a standard way to do this, but they're unfortunately not supported in github's execution. See thread https://github.com/orgs/community/discussions/4501 and issue actions/runner#1182 |
Beta Was this translation helpful? Give feedback.
I think I figured out a way to do this: I place the configuration in a separate file,
matrix.json
:The config uses a separate
matrix
step to read this JSON file from disk: