Hey guys, I have another tricky scenario re: gith...
# github-actions
s
Hey guys, I have another tricky scenario re: github actions: I have this job which is a terraform-apply job (let’s call it
job1
) and gives an output that looks like this:
Copy code
{
  "A": "123",
  "B": "234",
  "C": "345"
}
I would like to have another job, say
job2
that runs after
job1
and consumes the output from
job1
for use in a matrix. I want the matrix for job 2 to look like this:
Copy code
job2:
  needs: ["job1"]
  strategy:
    matrix:
      env:
        [
          { environment: production, item: [ "A", "B", "C" ] }, # Note that A, B and C are the keys from job1's output JSON
          { environment: development, item: [ "A", "B", "C" ] }} }
        ]
I shall be needing `job1`'s output as a complete JSON in
job2
as I shall be using
jq
to retrieve the values, for each key in
matrix.env.item
What’s the best way to go about this?
g
@Igor Rodionov
s
I managed to do this using an intermediate job that processes the output from
job1
using
jq  -c 'keys'
and then sends the output (an array of the keys: A, B & C) to
job2
👍 2
i
@Saichovsky Hello. in your case I’d suggest pattern like that
Copy code
job2:
  needs: ["job1"]
  strategy:
    matrix:
      include:
        - environment: production
          item: ${{ needs.job1.outputs.items }}
        - environment: development
          item: ${{ needs.job1.outputs.items }}
or
Copy code
job2:
  needs: ["job1"]
  strategy:
    matrix:
      environment: ["production", development"]
      items: [${{ toJSON(needs.job1.outputs.items) }}]
and
s
Thanks @Igor Rodionov I used the second approach after someone pointed me to the
fromJSON
function. But thanks for the two examples 🤝🏽
👍 1
i
this is off-topic, but have you seen our GHA that helps to deal with matrixes ?
👀 1
like
and also there is two actions that looks promissing