Skip to content

Commit 82c7699

Browse files
committed
refactor(flake-checks-ci-matrix): Merge matrix generation jobs into a single one
1 parent e15b065 commit 82c7699

File tree

2 files changed

+62
-82
lines changed

2 files changed

+62
-82
lines changed

.github/workflows/reusable-flake-checks-ci-matrix.yml

Lines changed: 61 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ jobs:
3838
3939
This comment will be updated automatically with the status of each package.
4040
41-
generate-matrix-matrix:
42-
name: Generate Matrix of Matrices
41+
generate-matrices:
42+
name: Generate Matrices
4343
runs-on: ${{ fromJSON(inputs.runner) }}
44+
outputs:
45+
matrix: ${{ steps.merge-matrix.outputs.matrix }}
46+
fullMatrix: ${{ steps.merge-matrix.outputs.fullMatrix }}
4447
steps:
4548
- name: Install Nix
4649
uses: metacraft-labs/nixos-modules/.github/install-nix@main
@@ -54,122 +57,104 @@ jobs:
5457
- uses: actions/checkout@v4
5558

5659
- name: Generate Matrix for Matrix
57-
id: generate-matrix
60+
id: generate-matrix-of-matrices
5861
env:
5962
CACHIX_CACHE: ${{ vars.CACHIX_CACHE }}
6063
CACHIX_AUTH_TOKEN: ${{ secrets.CACHIX_AUTH_TOKEN }}
6164
MCL_BRANCH: ${{ github.repository == 'metacraft-labs/nixos-modules' && github.sha || 'main' }}
62-
run: nix run --accept-flake-config github:metacraft-labs/nixos-modules/${{ env.MCL_BRANCH }}#mcl shard_matrix
63-
outputs:
64-
gen_matrix: ${{ steps.generate-matrix.outputs.gen_matrix }}
65-
66-
generate-matrix:
67-
needs: generate-matrix-matrix
68-
runs-on: ${{ fromJSON(inputs.runner) }}
69-
strategy:
70-
matrix: ${{fromJSON(needs.generate-matrix-matrix.outputs.gen_matrix)}}
71-
name: Generate Matrix ${{ matrix.digit != -1 && matrix.digit || '' }}
72-
steps:
73-
- name: Install Nix
74-
uses: metacraft-labs/nixos-modules/.github/install-nix@main
75-
with:
76-
cachix-cache: ${{ vars.CACHIX_CACHE }}
77-
cachix-auth-token: ${{ secrets.CACHIX_AUTH_TOKEN }}
78-
trusted-public-keys: ${{ vars.TRUSTED_PUBLIC_KEYS }}
79-
substituters: ${{ vars.SUBSTITUTERS }}
80-
nix-github-token: ${{ secrets.NIX_GITHUB_TOKEN }}
65+
run: |
66+
gen_matrix_output=$(nix run --accept-flake-config \
67+
github:metacraft-labs/nixos-modules/${{ env.MCL_BRANCH}}#mcl shard_matrix)
8168
82-
- uses: actions/checkout@v4
69+
echo "gen_matrix=$gen_matrix_output" >> "$GITHUB_OUTPUT"
8370
8471
- name: Generate CI Matrix
8572
id: generate-matrix
86-
shell: bash
8773
env:
8874
IS_INITIAL: 'true'
8975
CACHIX_CACHE: ${{ vars.CACHIX_CACHE }}
9076
CACHIX_AUTH_TOKEN: ${{ secrets.CACHIX_AUTH_TOKEN }}
91-
FLAKE_PRE: ${{ matrix.prefix }}
92-
FLAKE_POST: ${{ matrix.postfix }}
77+
GEN_MATRIX_JSON: ${{ steps.generate-matrix-of-matrices.outputs.gen_matrix }}
9378
MCL_BRANCH: ${{ github.repository == 'metacraft-labs/nixos-modules' && github.sha || 'main' }}
94-
run: nix run --accept-flake-config github:metacraft-labs/nixos-modules/${{ env.MCL_BRANCH }}#mcl ci_matrix
79+
run: |
80+
echo "GEN_MATRIX_JSON is:"
81+
echo "$GEN_MATRIX_JSON"
9582
96-
- uses: actions/upload-artifact@v4
97-
with:
98-
name: matrix-pre${{ matrix.digit != -1 && format('-{0}', matrix.digit) || '' }}
99-
path: matrix-pre.json
100-
outputs:
101-
matrix: ${{ steps.generate-matrix.outputs.matrix }}
83+
final_matrix='[]'
10284
103-
slurp-matrix:
104-
runs-on: ${{ fromJSON(inputs.runner) }}
105-
needs: generate-matrix
106-
name: Merge matrices
107-
outputs:
108-
matrix: ${{ steps.matrix.outputs.matrix }}
109-
fullMatrix: ${{ steps.matrix.outputs.fullMatrix }}
110-
steps:
111-
- uses: actions/download-artifact@v4
112-
- name: Merge matrices
113-
run: |
114-
# Check if jq is installed
115-
if ! command -v jq &> /dev/null; then
116-
PATH="$(nix build --print-out-paths 'nixpkgs#jq^bin')/bin:$PATH"
117-
export PATH
118-
fi
85+
echo "$GEN_MATRIX_JSON" | jq -c '.include[]' | while read shard; do
86+
prefix=$(echo "$shard" | jq -r '.prefix')
87+
postfix=$(echo "$shard" | jq -r '.postfix')
88+
89+
echo "Processing shard -> prefix: $prefix, postfix: $postfix"
90+
91+
shard_output=$(PREFIX="$prefix" POSTFIX="$postfix" \
92+
nix run --accept-flake-config \
93+
github:metacraft-labs/nixos-modules/${MCL_BRANCH}#mcl ci_matrix)
11994
120-
ls */matrix-pre.json
121-
matrix="$(cat */matrix-pre.json | jq -cr '.include[]' | jq '[ select (.isCached == false) ]' | jq -s 'add' | jq -c '. | {include: .}')"
95+
echo "Shard output: $shard_output"
12296
123-
if [[ "$matrix" == '' ]] || [[ "$matrix" == '{}' ]] || [[ "$matrix" == '{"include":null}' ]] || [[ "$matrix" == '{"include":[]}' ]]; then
97+
final_matrix=$(echo "$final_matrix" | jq --argjson shardData "$shard_output" '. + [$shardData]')
98+
done
99+
100+
echo "Final combined matrix:"
101+
echo "$final_matrix"
102+
echo "$final_matrix" > matrix-pre.json
103+
echo "matrix=$final_matrix" >> "$GITHUB_OUTPUT"
104+
105+
- name: Merge matrices
106+
id: merge-matrix
107+
run: |
108+
set -eux
109+
# If you need jq and it's not available on the runner:
110+
# if ! command -v jq &> /dev/null; then
111+
# PATH="$(nix build --print-out-paths 'nixpkgs#jq^bin')/bin:$PATH"
112+
# export PATH
113+
# fi
114+
115+
if [ -f matrix-pre.json ]; then
116+
echo "Found matrix-pre.json. Merging..."
117+
matrix=$(jq -cr '.include' matrix-pre.json | jq -c '{include: .}')
118+
else
119+
# Fallback if the file doesn't exist
124120
matrix='{"include":[]}'
125121
fi
126122
127123
echo "---"
128124
echo "Matrix:"
129125
echo "$matrix" | jq
130126
echo "---"
131-
echo
132-
echo
133-
134-
fullMatrix="$(cat */matrix-pre.json | jq -cr '.include' | jq -s 'add' | jq -c '. | {include: .}')"
135127
136-
echo "---"
137-
echo "Full Matrix:"
138-
echo "$fullMatrix" | jq
139-
echo "---"
128+
# fullMatrix could be a superset of 'matrix'
129+
# or exactly the same, depending on your logic
130+
fullMatrix="$matrix"
140131
141-
echo "matrix=$matrix" >> $GITHUB_OUTPUT
142-
echo "fullMatrix=$fullMatrix" >> $GITHUB_OUTPUT
132+
echo "matrix=$matrix" >> "$GITHUB_OUTPUT"
133+
echo "fullMatrix=$fullMatrix" >> "$GITHUB_OUTPUT"
143134
144-
id: matrix
145135
- name: Post Comment
146-
id: print-matrix
147136
uses: metacraft-labs/nixos-modules/.github/print-matrix@main
148137
with:
149138
is-initial: 'true'
150139
cachix-cache: ${{ vars.CACHIX_CACHE }}
151140
cachix-auth-token: ${{ secrets.CACHIX_AUTH_TOKEN }}
152141
trusted-public-keys: ${{ vars.TRUSTED_PUBLIC_KEYS }}
153142
substituters: ${{ vars.SUBSTITUTERS }}
154-
precalc_matrix: ${{ steps.matrix.outputs.fullMatrix }}
143+
precalc_matrix: ${{ steps.merge-matrix.outputs.fullMatrix }}
155144
pr-comment-github-token: ${{ secrets.GITHUB_TOKEN }}
156145
nix-github-token: ${{ secrets.NIX_GITHUB_TOKEN }}
157146

158147
build:
159-
needs: slurp-matrix
160-
if: needs.slurp-matrix.outputs.matrix != '{"include":[]}'
161-
148+
needs: generate-matrices
149+
if: needs.generate-matrices.outputs.matrix != '{"include":[]}'
162150
strategy:
163151
fail-fast: false
164-
matrix: ${{fromJSON( needs.slurp-matrix.outputs.matrix )}}
165-
152+
matrix: ${{ fromJSON(needs.generate-matrices.outputs.matrix) }}
166153
name: ${{ matrix.name }} | ${{ matrix.system }}
167154
runs-on: ${{ matrix.os }}
168155
continue-on-error: ${{ matrix.allowedToFail }}
169-
170156
steps:
171157
- uses: actions/checkout@v4
172-
173158
- name: Install Nix
174159
uses: metacraft-labs/nixos-modules/.github/install-nix@main
175160
with:
@@ -178,20 +163,18 @@ jobs:
178163
trusted-public-keys: ${{ vars.TRUSTED_PUBLIC_KEYS }}
179164
substituters: ${{ vars.SUBSTITUTERS }}
180165
nix-github-token: ${{ secrets.NIX_GITHUB_TOKEN }}
181-
182166
- name: Build ${{ matrix.name }}
183167
run: |
184168
nix build -L --no-link --keep-going --show-trace \
185169
'.#${{ matrix.attrPath }}'
186-
187170
- name: Push to Cachix ${{ matrix.name }}
188171
run: |
189172
cachix push ${{ vars.CACHIX_CACHE }} ${{ matrix.output }}
190173
191174
results:
175+
needs: [build, generate-matrices]
192176
runs-on: ${{ fromJSON(inputs.runner) }}
193177
name: Final Results
194-
needs: [build, slurp-matrix]
195178
if: always()
196179
steps:
197180
- name: Post Comment
@@ -202,24 +185,21 @@ jobs:
202185
cachix-auth-token: ${{ secrets.CACHIX_AUTH_TOKEN }}
203186
trusted-public-keys: ${{ vars.TRUSTED_PUBLIC_KEYS }}
204187
substituters: ${{ vars.SUBSTITUTERS }}
205-
precalc_matrix: ${{ needs.slurp-matrix.outputs.fullMatrix }}
188+
precalc_matrix: ${{ needs.generate-matrices.outputs.fullMatrix }}
206189
pr-comment-github-token: ${{ secrets.GITHUB_TOKEN }}
207190
nix-github-token: ${{ secrets.NIX_GITHUB_TOKEN }}
208-
209191
- run: exit 1
210192
if: >-
211-
needs.slurp-matrix.outputs.matrix != '{"include":[]}'
193+
needs.generate-matrices.outputs.matrix != '{"include":[]}'
212194
&& contains(needs.*.result, 'failure')
213195
|| contains(needs.*.result, 'cancelled')
214-
215196
- uses: actions/checkout@v4
216197
if: inputs.run-cachix-deploy
217-
218198
- name: Deploy
219199
if: inputs.run-cachix-deploy
220200
env:
221201
CACHIX_CACHE: ${{ vars.CACHIX_CACHE }}
222202
CACHIX_AUTH_TOKEN: ${{ secrets.CACHIX_AUTH_TOKEN }}
223203
CACHIX_ACTIVATE_TOKEN: '${{ secrets.CACHIX_ACTIVATE_TOKEN }}'
224204
MCL_BRANCH: ${{ github.repository == 'metacraft-labs/nixos-modules' && github.sha || 'main' }}
225-
run: nix run --accept-flake-config github:metacraft-labs/nixos-modules/${{ env.MCL_BRANCH }}#mcl deploy_spec
205+
run: nix run --accept-flake-config github:metacraft-labs/nixos-modules/${MCL_BRANCH}#mcl deploy_spec

packages/mcl/src/src/mcl/commands/shard_matrix.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void saveShardMatrix(ShardMatrix matrix, Params params)
143143
const matrixJson = matrix.toJSON();
144144
const matrixString = matrixJson.toString();
145145
infof("Shard matrix: %s", matrixJson.toPrettyString);
146-
const envLine = "gen_matrix=" ~ matrixString;
146+
const envLine = matrixString;
147147
if (params.githubOutput != "")
148148
{
149149
params.githubOutput.append(envLine);

0 commit comments

Comments
 (0)