Skip to content

Commit 285e3a8

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

File tree

2 files changed

+61
-82
lines changed

2 files changed

+61
-82
lines changed

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

Lines changed: 60 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,103 @@ 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 }}
9377
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
78+
run: |
79+
echo "GEN_MATRIX_JSON is:"
80+
echo "$gen_matrix_output"
9581
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 }}
82+
final_matrix='[]'
10283
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
84+
echo "$gen_matrix_output" | jq -c '.include[]' | while read shard; do
85+
prefix=$(echo "$shard" | jq -r '.prefix')
86+
postfix=$(echo "$shard" | jq -r '.postfix')
87+
88+
echo "Processing shard -> prefix: $prefix, postfix: $postfix"
89+
90+
shard_output=$(PREFIX="$prefix" POSTFIX="$postfix" \
91+
nix run --accept-flake-config \
92+
github:metacraft-labs/nixos-modules/${MCL_BRANCH}#mcl ci_matrix)
11993
120-
ls */matrix-pre.json
121-
matrix="$(cat */matrix-pre.json | jq -cr '.include[]' | jq '[ select (.isCached == false) ]' | jq -s 'add' | jq -c '. | {include: .}')"
94+
echo "Shard output: $shard_output"
12295
123-
if [[ "$matrix" == '' ]] || [[ "$matrix" == '{}' ]] || [[ "$matrix" == '{"include":null}' ]] || [[ "$matrix" == '{"include":[]}' ]]; then
96+
final_matrix=$(echo "$final_matrix" | jq --argjson shardData "$shard_output" '. + [$shardData]')
97+
done
98+
99+
echo "Final combined matrix:"
100+
echo "$final_matrix"
101+
echo "$final_matrix" > matrix-pre.json
102+
echo "matrix=$final_matrix" >> "$GITHUB_OUTPUT"
103+
104+
- name: Merge matrices
105+
id: merge-matrix
106+
run: |
107+
set -eux
108+
# If you need jq and it's not available on the runner:
109+
# if ! command -v jq &> /dev/null; then
110+
# PATH="$(nix build --print-out-paths 'nixpkgs#jq^bin')/bin:$PATH"
111+
# export PATH
112+
# fi
113+
114+
if [ -f matrix-pre.json ]; then
115+
echo "Found matrix-pre.json. Merging..."
116+
matrix=$(jq -cr '.include' matrix-pre.json | jq -c '{include: .}')
117+
else
118+
# Fallback if the file doesn't exist
124119
matrix='{"include":[]}'
125120
fi
126121
127122
echo "---"
128123
echo "Matrix:"
129124
echo "$matrix" | jq
130125
echo "---"
131-
echo
132-
echo
133-
134-
fullMatrix="$(cat */matrix-pre.json | jq -cr '.include' | jq -s 'add' | jq -c '. | {include: .}')"
135126
136-
echo "---"
137-
echo "Full Matrix:"
138-
echo "$fullMatrix" | jq
139-
echo "---"
127+
# fullMatrix could be a superset of 'matrix'
128+
# or exactly the same, depending on your logic
129+
fullMatrix="$matrix"
140130
141-
echo "matrix=$matrix" >> $GITHUB_OUTPUT
142-
echo "fullMatrix=$fullMatrix" >> $GITHUB_OUTPUT
131+
echo "matrix=$matrix" >> "$GITHUB_OUTPUT"
132+
echo "fullMatrix=$fullMatrix" >> "$GITHUB_OUTPUT"
143133
144-
id: matrix
145134
- name: Post Comment
146-
id: print-matrix
147135
uses: metacraft-labs/nixos-modules/.github/print-matrix@main
148136
with:
149137
is-initial: 'true'
150138
cachix-cache: ${{ vars.CACHIX_CACHE }}
151139
cachix-auth-token: ${{ secrets.CACHIX_AUTH_TOKEN }}
152140
trusted-public-keys: ${{ vars.TRUSTED_PUBLIC_KEYS }}
153141
substituters: ${{ vars.SUBSTITUTERS }}
154-
precalc_matrix: ${{ steps.matrix.outputs.fullMatrix }}
142+
precalc_matrix: ${{ steps.merge-matrix.outputs.fullMatrix }}
155143
pr-comment-github-token: ${{ secrets.GITHUB_TOKEN }}
156144
nix-github-token: ${{ secrets.NIX_GITHUB_TOKEN }}
157145

158146
build:
159-
needs: slurp-matrix
160-
if: needs.slurp-matrix.outputs.matrix != '{"include":[]}'
161-
147+
needs: generate-matrices
148+
if: needs.generate-matrices.outputs.matrix != '{"include":[]}'
162149
strategy:
163150
fail-fast: false
164-
matrix: ${{fromJSON( needs.slurp-matrix.outputs.matrix )}}
165-
151+
matrix: ${{ fromJSON(needs.generate-matrices.outputs.matrix) }}
166152
name: ${{ matrix.name }} | ${{ matrix.system }}
167153
runs-on: ${{ matrix.os }}
168154
continue-on-error: ${{ matrix.allowedToFail }}
169-
170155
steps:
171156
- uses: actions/checkout@v4
172-
173157
- name: Install Nix
174158
uses: metacraft-labs/nixos-modules/.github/install-nix@main
175159
with:
@@ -178,20 +162,18 @@ jobs:
178162
trusted-public-keys: ${{ vars.TRUSTED_PUBLIC_KEYS }}
179163
substituters: ${{ vars.SUBSTITUTERS }}
180164
nix-github-token: ${{ secrets.NIX_GITHUB_TOKEN }}
181-
182165
- name: Build ${{ matrix.name }}
183166
run: |
184167
nix build -L --no-link --keep-going --show-trace \
185168
'.#${{ matrix.attrPath }}'
186-
187169
- name: Push to Cachix ${{ matrix.name }}
188170
run: |
189171
cachix push ${{ vars.CACHIX_CACHE }} ${{ matrix.output }}
190172
191173
results:
174+
needs: [build, generate-matrices]
192175
runs-on: ${{ fromJSON(inputs.runner) }}
193176
name: Final Results
194-
needs: [build, slurp-matrix]
195177
if: always()
196178
steps:
197179
- name: Post Comment
@@ -202,24 +184,21 @@ jobs:
202184
cachix-auth-token: ${{ secrets.CACHIX_AUTH_TOKEN }}
203185
trusted-public-keys: ${{ vars.TRUSTED_PUBLIC_KEYS }}
204186
substituters: ${{ vars.SUBSTITUTERS }}
205-
precalc_matrix: ${{ needs.slurp-matrix.outputs.fullMatrix }}
187+
precalc_matrix: ${{ needs.generate-matrices.outputs.fullMatrix }}
206188
pr-comment-github-token: ${{ secrets.GITHUB_TOKEN }}
207189
nix-github-token: ${{ secrets.NIX_GITHUB_TOKEN }}
208-
209190
- run: exit 1
210191
if: >-
211-
needs.slurp-matrix.outputs.matrix != '{"include":[]}'
192+
needs.generate-matrices.outputs.matrix != '{"include":[]}'
212193
&& contains(needs.*.result, 'failure')
213194
|| contains(needs.*.result, 'cancelled')
214-
215195
- uses: actions/checkout@v4
216196
if: inputs.run-cachix-deploy
217-
218197
- name: Deploy
219198
if: inputs.run-cachix-deploy
220199
env:
221200
CACHIX_CACHE: ${{ vars.CACHIX_CACHE }}
222201
CACHIX_AUTH_TOKEN: ${{ secrets.CACHIX_AUTH_TOKEN }}
223202
CACHIX_ACTIVATE_TOKEN: '${{ secrets.CACHIX_ACTIVATE_TOKEN }}'
224203
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
204+
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)