I have an input.json
file that looks like below
{
"appname": "apple",
"category": "fruit",
"buildapp": [
{
"workdir": "work1",
"tesdir": "test1",
"setdata": {
"set1": "value1",
"set2": "value2"
}
},
{
"workdir": "work2",
"tesdir": "test2",
"setdata": {
"set1": "value3",
"set2": "value4"
}
}
]
}
I have to read it in GitHub Actions and loop through each buildapp section and print all the "key" and corresponding "value" pair.
You can use the matrix strategy to define inputs for your publishing step.
In this case, the workflow can be divided into two separate jobs: the first job should provide the output with a tags array and the second job should use this output as a matrix input:
name: 'Loop JSON'
on:
workflow_dispatch:
jobs:
read-json:
runs-on: ubuntu-latest
outputs:
json-content: ${{ steps.file.outputs.content }}
steps:
- uses: actions/checkout@v4
- name: Read the file
id: file
uses: juliangruber/read-file-action@v1
with:
path: ./file.json
loop-json:
runs-on: ubuntu-latest
needs: read-json
strategy:
matrix:
app: ${{ fromJSON(needs.read-json.outputs.json-content).buildapp }}
steps:
- run: echo ${{ matrix.app.workdir }}
Resulting workflow:
In this example, I used the Read file Action to read the json file from my repository.
Each job will print the value of the workdir
node. Following this example, you can access the other values.
The file:
{
"appname" : "apple",
"category" : "fruit",
"buildapp": [
{
"workdir": "work1",
"tesdir" : "test1",
"setdata" : {
"set1": "value1",
"set2": "value2"
}
},
{
"workdir": "work2",
"tesdir" : "test2",
"setdata" : {
"set1": "value3",
"set2": "value4"
}
}
]
}
Similar cases with the matrix
usage: