GitHub action with matrix (parallel) jobs for each of the item in a JSON file
I have a file
deploy-list.json:
{
"apiList": [
"abc",
"def"
]
}
I would like to run a GitHub action with matrix (parallel) jobs for each of the item in the apiList above.
name: API Build
on:
workflow_dispatch:
inputs:
branch:
description: 'Github Release Branch Name'
required: true
default: 'release/1.5.3'
push:
branches:
- 'develop'
jobs:
build-matrix:
runs-on: ubuntu-latest
steps:
- name: Checkout Branch
uses: actions/checkout@v2
with:
ref: "${{ github.event.inputs.branch }}"
- name: Set Matrix
id: set-matrix
run: echo "::set-output name=api_matrix::$(cat deploy-list.json | jq -c '.apiList')"
outputs:
api_matrix: ${{ steps.set-matrix.outputs.api_matrix }}
build-api:
environment: PRD
needs: build-matrix
runs-on: ubuntu-latest
strategy:
matrix:
api_name: ${{ fromJson(needs.build-matrix.outputs.api_matrix) }}
steps:
- name: Checkout Branch
uses: actions/checkout@v2
with:
ref: "${{ github.event.inputs.branch }}"
- name: Building ${{ matrix.api_name }}
run: |
echo ${{ matrix.api_name }}
# Do something here
Two parallel jobs will be invoked for "abc" and "def" apis making the workflow complete in twice as fast build time. I also using maven caching to speed up the builds even further.