@@ -246,6 +246,33 @@ export async function tryUploadAllAvailableDebugArtifacts(
246246 }
247247}
248248
249+ /**
250+ * When a build matrix is used, multiple different jobs arising from the matrix may attempt to upload
251+ * workflow artifacts with the same base name. In that case, only one of the uploads will succeed and
252+ * the others will fail. This function inspects the matrix object to compute a suffix for the artifact
253+ * name that uniquely identifies the matrix values of the current job to avoid name clashes.
254+ *
255+ * @param matrix A stringified JSON value, usually the value of the `matrix` input.
256+ * @returns A suffix that uniquely identifies the `matrix` value for the current job, or `""` if there
257+ * is no matrix value.
258+ */
259+ export function getArtifactSuffix ( matrix : string | undefined ) : string {
260+ let suffix = "" ;
261+ if ( matrix ) {
262+ try {
263+ for ( const [ , matrixVal ] of Object . entries (
264+ JSON . parse ( matrix ) as any [ ] [ ] ,
265+ ) . sort ( ) )
266+ suffix += `-${ matrixVal } ` ;
267+ } catch {
268+ core . info (
269+ "Could not parse user-specified `matrix` input into JSON. The debug artifact will not be named with the user's `matrix` input." ,
270+ ) ;
271+ }
272+ }
273+ return suffix ;
274+ }
275+
249276export async function uploadDebugArtifacts (
250277 logger : Logger ,
251278 toUpload : string [ ] ,
@@ -279,21 +306,7 @@ export async function uploadDebugArtifacts(
279306 core . exportVariable ( "CODEQL_ACTION_ARTIFACT_SCAN_FINISHED" , "true" ) ;
280307 }
281308
282- let suffix = "" ;
283- const matrix = getOptionalInput ( "matrix" ) ;
284- if ( matrix ) {
285- try {
286- for ( const [ , matrixVal ] of Object . entries (
287- JSON . parse ( matrix ) as any [ ] [ ] ,
288- ) . sort ( ) )
289- suffix += `-${ matrixVal } ` ;
290- } catch {
291- core . info (
292- "Could not parse user-specified `matrix` input into JSON. The debug artifact will not be named with the user's `matrix` input." ,
293- ) ;
294- }
295- }
296-
309+ const suffix = getArtifactSuffix ( getOptionalInput ( "matrix" ) ) ;
297310 const artifactUploader = await getArtifactUploaderClient ( logger , ghVariant ) ;
298311
299312 try {
0 commit comments