I am using matrix to trigger workflow jobs in github action. The apps
from affected-apps
output gives me the array including all affected app name. There could be a hundred apps returned in this array.
But I just want to select a few of them. I tried to use if: ${{ contains(fromJson('["app1", "app2"]'), matrix.app) }}
on job root level but it gives me error. It only works if I put the condition under stpes
. But I have to put this condition on every step which is not good.
Is there a way to only select a few matched matrix on job level?
I know there is exclude
on matrix can do the job, but I have a long list of exclude. I'd like to choose only include
on matrix. How can I achieve that?
jobs:
example_matrix:
runs-on: ubuntu-latest
strategy:
matrix:
app: ${{ fromJson(needs.affected-apps.outputs.apps) }}
if: ${{ contains(fromJson('["app1", "app2"]'), matrix.app) }}
steps:
- uses: actions/setup-node@v3
- uses: ...
- uses: ...
- uses: ...
You can introduce temporary step where you use jq to filter out not needed variations like here:
jobs:
affected-apps:
runs-on: ubuntu-latest
name: A job to say hello
outputs:
apps: ${{ steps.step1.outputs.test}}
steps:
- uses: actions/checkout@v3
- id: step1
run: echo 'test=["app1","app2", "app3"]' >> "$GITHUB_OUTPUT"
example_matrix:
runs-on: ubuntu-latest
needs: affected-apps
strategy:
matrix:
app: ${{ fromJson(needs.affected-apps.outputs.apps) }}
steps:
- uses: actions/setup-node@v3
- run: echo "${{ matrix.app }}"
filter_matrix:
runs-on: ubuntu-latest
needs: affected-apps
outputs:
apps: ${{ steps.step1.outputs.filtered }}
steps:
- id: step1
run: |
echo "$APPS"
RESULT=$(echo $APPS | jq -c --argjson toRemove '["app1", "app2"]' 'map(select(. as $v | $toRemove | index($v) | not))')
echo "$RESULT"
echo "filtered=$RESULT" >> "$GITHUB_OUTPUT"
env:
APPS: ${{ needs.affected-apps.outputs.apps }}
filtered_matrix:
runs-on: ubuntu-latest
needs: filter_matrix
strategy:
matrix:
app: ${{ fromJson(needs.filter_matrix.outputs.apps) }}
steps:
- uses: actions/setup-node@v3
- run: echo "${{ matrix.app }}"