forked from SkriptLang/Skript
-
Notifications
You must be signed in to change notification settings - Fork 0
85 lines (76 loc) · 3.21 KB
/
parallelize-tests.yml
File metadata and controls
85 lines (76 loc) · 3.21 KB
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
name: Parallelize Tests
on:
workflow_call:
inputs:
environments:
required: true
type: string
parallel_jobs:
required: false
type: number
default: 2
java_version:
required: true
type: number
outputs:
matrix:
description: "Generated test matrix"
value: ${{ jobs.prepare.outputs.matrix }}
jobs:
prepare:
name: ""
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Generate matrix
id: set-matrix
run: |
# Use environment variables
N=${{ inputs.parallel_jobs }}
JAVA_VERSION=${{ inputs.java_version }}
# Convert to array and transform
IFS=',' read -ra ENVS <<< "${{ inputs.environments }}"
TRANSFORMED=()
for env in "${ENVS[@]}"; do
TRANSFORMED+=("java${JAVA_VERSION}/paper-${env}")
done
TOTAL=${#TRANSFORMED[@]}
# avoid creating more jobs than needed
JOBS=$((TOTAL < N ? TOTAL : N))
# environments per job
BASE=$((TOTAL / JOBS))
EXTRA=$((TOTAL % JOBS))
# Build matrix JSON
# Format is:
# [{"id":1,"envs":"java21/paper-1.20.6,java21/paper-1.21.3","display":"1.20.6, 1.21.3"},...]
# Where "envs" is the actual environment strings and "display" is for easier identification
MATRIX="["
INDEX=0
for ((i=0; i<JOBS; i++)); do
# Determine count for this job (BASE + 1 if i < EXTRA)
COUNT=$BASE
if [ $i -lt $EXTRA ]; then
COUNT=$((COUNT + 1))
fi
# Build envs and display strings
GROUP=""
DISPLAY_GROUP=""
for ((j=0; j<COUNT; j++)); do
if [ -n "$GROUP" ]; then
GROUP="$GROUP,"
DISPLAY_GROUP="$DISPLAY_GROUP, "
fi
GROUP="$GROUP${TRANSFORMED[$INDEX]}"
DISPLAY_GROUP="$DISPLAY_GROUP${ENVS[$INDEX]}"
INDEX=$((INDEX + 1))
done
# add to matrix with separating comma if needed
if [ $i -gt 0 ]; then
MATRIX="$MATRIX,"
fi
MATRIX="$MATRIX{\"id\":$((i+1)),\"envs\":\"$GROUP\",\"display\":\"$DISPLAY_GROUP\"}"
done
MATRIX="$MATRIX]"
echo "matrix={\"include\":$MATRIX}" >> $GITHUB_OUTPUT
echo "Generated matrix: {\"include\":$MATRIX}"