forked from joshjohanning-org/dynamic-matrix-example
-
Notifications
You must be signed in to change notification settings - Fork 0
342 lines (299 loc) · 10.8 KB
/
dynamic-matrix-subvalues.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
name: dynamic-matrix-test-json
on:
push:
workflow_dispatch:
permissions: read-all
jobs:
env-1:
runs-on: ubuntu-latest
# environment: prod
steps:
- run: echo "hi"
- run: |
var="69$fi"
echo "::add-mask::$var"
echo $var
env-2:
runs-on: ubuntu-latest
if: always()
needs: env-1
steps:
- run: echo "${{ needs.env-1.result }}"
env-3:
runs-on: ubuntu-latest
environment: prod2
if: always()
needs: env-2
steps:
- run: echo "hiiii"
prepare-1:
runs-on: ubuntu-latest
outputs:
repositories: ${{ steps.json.outputs.repositories }}
steps:
- name: Check out repository
uses: actions/checkout@v4
# works
- name: Build matrix
uses: actions/github-script@v7
id: json
with:
script: |
const fs = require('fs');
const path = require('path');
// Define the path to the JSON file
const filePath = path.join(process.env.GITHUB_WORKSPACE, 'index.json');
// Read the JSON file
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading the file:', err);
return;
}
try {
// Parse the JSON data
const jsonData = JSON.parse(data);
// Log the parsed data
console.log('Parsed JSON data:', jsonData);
// Set the output for GitHub Actions
// core.setOutput('repositories', JSON.stringify(jsonData.repositories));
core.setOutput('repositories', JSON.stringify(jsonData));
} catch (parseErr) {
console.error('Error parsing JSON:', parseErr);
}
});
prepare-2:
runs-on: ubuntu-latest
outputs:
repositories: ${{ steps.json.outputs.repositories }}
steps:
- name: Check out repository
uses: actions/checkout@v4
# works
- name: Build matrix
uses: actions/github-script@v7
id: json
with:
script: |
const fs = require('fs');
const path = require('path');
// Define the path to the JSON file
const filePath = path.join(process.env.GITHUB_WORKSPACE, 'index2.json');
// Read the JSON file
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading the file:', err);
return;
}
try {
// Parse the JSON data
const jsonData = JSON.parse(data);
// Log the parsed data
console.log('Parsed JSON data:', jsonData);
// Set the output for GitHub Actions
// core.setOutput('repositories', JSON.stringify(jsonData.repositories));
core.setOutput('repositories', JSON.stringify(jsonData));
} catch (parseErr) {
console.error('Error parsing JSON:', parseErr);
}
});
# works
# - name: build matrix 2
# id: json
# run: |
# jq '{"repositories": [.repositories[].name]}' index.json
# jq=$(jq '{"repositories": [.repositories[].name]}' index.json)
# # Process the JSON file and extract repository names
# jq_output=$(jq -c '{"repositories": [.repositories[].name]}' index.json)
# # Append the result to the GitHub Actions output
# echo "repositories=$jq_output" >> $GITHUB_OUTPUT
# # works
# - name: build matrix 3
# id: json
# run: |
# jq -c '{repositories: .organization.repositories}' index.json
# # Process the JSON file and extract repository names
# jq_output=$(jq -c '{repositories: .organization.repositories}' index2.json)
# # Append the result to the GitHub Actions output
# echo "repositories=$jq_output" >> $GITHUB_OUTPUT
run-matrix-1:
needs: prepare-1
name: ${{ matrix.repositories.name }}
runs-on: ubuntu-latest
strategy:
matrix: "${{ fromJson(needs.prepare-1.outputs.repositories) }}"
fail-fast: false
max-parallel: 15
outputs:
is-failure: ${{ steps.is-failure.outputs.is-failure }}
steps:
- run: echo "${{ toJson(matrix) }}"
- name: Dump matrix context
env:
MATRIX_CONTEXT: ${{ toJson(matrix) }}
run: echo "$MATRIX_CONTEXT"
- if: ${{ matrix.repositories.name == 'repo2' }}
run: sleep 2s && exit 1
- name: create failures sometimes
run: |
milliseconds=$(date +%s%N | cut -b1-13)
if (( milliseconds % 2 == 0 )); then
exit 1
# exit 0
fi
- name: create output var if failure
id: is-failure
if: failure()
run: echo "is-failure=true" >> "$GITHUB_OUTPUT"
- name: upload status
if: always()
run: echo "${{ matrix.repositories.name }}, ${{ job.status }}" > ${{ matrix.repositories.name }}.txt
- uses: actions/upload-artifact@v4
if: always()
with:
name: ${{ matrix.repositories.name }}-1
path: ${{ matrix.repositories.name }}.txt
if-failure:
needs: [prepare-1, run-matrix-1]
runs-on: ubuntu-latest
if: always() && needs.run-matrix-1.outputs.is-failure == 'true'
steps:
- run: echo "${{ needs.run-matrix-1.outputs.is-failure }}"
run-matrix-2:
needs: prepare-2
name: ${{ matrix.repositories.name }}
runs-on: ubuntu-latest
strategy:
matrix: "${{ fromJson(needs.prepare-2.outputs.repositories).organization.myorg }}"
fail-fast: false
max-parallel: 15
steps:
- run: echo "${{ toJson(matrix) }}"
- name: Dump matrix context
env:
MATRIX_CONTEXT: ${{ toJson(matrix) }}
run: echo "$MATRIX_CONTEXT"
- name: create failures sometimes
run: |
milliseconds=$(date +%s%N | cut -b1-13)
if (( milliseconds % 2 == 0 )); then
# exit 1
exit 0
fi
- name: upload status
if: always()
run: echo "${{ matrix.repositories.name }}, ${{ job.status }}" > ${{ matrix.repositories.name }}.txt
- uses: actions/upload-artifact@v4
if: always()
with:
name: ${{ matrix.repositories.name }}-2
path: ${{ matrix.repositories.name }}.txt
test-output:
needs: [prepare-1, prepare-2]
runs-on: ubuntu-latest
steps:
- run: echo ${{ needs.prepare-1.outputs.repositories }}
- run: echo ${{ needs.prepare-2.outputs.repositories }}
# post:
# if: always()
# needs: run-matrix
# runs-on: ubuntu-latest
# steps:
# - uses: actions/download-artifact@v3
# - name: loop thru
# id: get-status
# working-directory: statuses
# continue-on-error: true
# run: |
# ISFAILED=0
# failed_repos=""
# successful_repos=""
# for file in ./*.txt; do
# # Read each line in the file
# while IFS=, read -r repo status; do
# echo "Repo: $repo, Status: $status"
# if [ "$status" != "success" ]; then
# # Add the repo to the failed list
# failed_repos+="$repo\n"
# else
# # Add the repo to the successful list
# successful_repos+="$repo\n"
# fi
# done < "$file"
# done
# echo -e "Failed repos: \n$failed_repos"
# echo "failed-repos=$failed_repos" >> $GITHUB_OUTPUT
# echo -e "Successful repos: \n$successful_repos"
# echo "successful-repos=$successful_repos" >> $GITHUB_OUTPUT
# if [ $ISFAILED = 1 ]; then
# "isfailed=true" >> $GITHUB_OUTPUT
# else
# "isfailed=false" >> $GITHUB_OUTPUT
# fi
# - name: print failed repos
# run: echo -e "${{ steps.get-status.outputs.failed-repos }}"
# - name: or we could use the api to get failed jobs
# env:
# GH_TOKEN: ${{ github.token }}
# run: |
# gh api /repos/${{ github.repository }}/actions/runs/${{ github.run_id }}/jobs --jq '.jobs[] | select(.conclusion != "success" and (.name | startswith("run-matrix"))) | { name: .name, conclusion: .conclusion}'
# - name: and the api to get success jobs
# env:
# GH_TOKEN: ${{ github.token }}
# run: |
# gh api /repos/${{ github.repository }}/actions/runs/${{ github.run_id }}/jobs --jq '.jobs[] | select(.conclusion == "success" and (.name | startswith("run-matrix"))) | { name: .name, conclusion: .conclusion}'
# # success, handling canceled / incomplete cases
# - uses: actions/github-script@v7
# if: ${{ !steps.get-status.outputs.isfailed && steps.get-status.outputs.failed-repos != ''}}
# with:
# script: |
# let commentBody
# commentBody = `Success`
# await github.rest.issues.create({
# owner: context.repo.owner,
# repo: context.repo.repo,
# issue_number: context.issue.number,
# title: 'new issue',
# body: commentBody.replace(/ +/g, ''),
# labels: ['migration', 'gei']
# })
# # failure, handling canceled / incomplete cases
# - uses: actions/github-script@v7
# if: ${{ steps.get-status.outputs.isfailed && steps.get-status.outputs.failed-repos != ''}}
# with:
# script: |
# let commentBody
# commentBody = `Failure`
# await github.rest.issues.create({
# owner: context.repo.owner,
# repo: context.repo.repo,
# issue_number: context.issue.number,
# title: 'new issue',
# body: commentBody.replace(/ +/g, ''),
# labels: ['migration', 'gei']
# })
# - name: Dump GitHub context
# env:
# GITHUB_CONTEXT: ${{ toJSON(github) }}
# run: echo "$GITHUB_CONTEXT"
# - name: Dump job context
# run: echo '${{ toJSON(job) }}'
# - name: Dump steps context
# run: echo '${{ toJSON(steps) }}'
# - name: Dump runner context
# run: echo '${{ toJSON(runner) }}'
# - name: Dump strategy context
# run: echo '${{ toJSON(strategy) }}'
# - name: Dump matrix context
# run: echo '${{ toJSON(matrix) }}'
# - run: echo ${{ matrix.repository }}
deploy-1:
runs-on: ubuntu-latest
environment: test
steps:
- run: echo "hi"
deploy-2:
runs-on: ubuntu-latest
environment: test
needs: deploy-1
steps:
- run: echo "hi"