-
Notifications
You must be signed in to change notification settings - Fork 2
252 lines (217 loc) · 9.78 KB
/
Copy pathtest-module-container.yml
File metadata and controls
252 lines (217 loc) · 9.78 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
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
name: Module Container Tests
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
SLACK_CHANNEL_ID: ${{ secrets.SLACK_CHANNEL_ID }}
permissions:
contents: read
on:
push:
branches:
- main
paths:
- "src/modules/**"
- "src/server/core/**"
- ".github/workflows/test-module-container.yml"
workflow_dispatch:
jobs:
slack-start:
name: Slack start message
runs-on: ubuntu-latest
outputs:
ts: ${{ steps.slack-start.outputs.ts }}
steps:
- name: checkout repository
uses: actions/checkout@v6
- name: send slack start message
id: slack-start
uses: ./.github/actions/slack-run-start
with:
message_prefix: Workflow running
discover-modules:
name: Discover modules to test
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.discover.outputs.matrix }}
has_modules: ${{ steps.discover.outputs.has_modules }}
selected_modules: ${{ steps.discover.outputs.selected_modules }}
module_count: ${{ steps.discover.outputs.module_count }}
steps:
- name: checkout repository
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: set commit range
id: commit-range
shell: bash
run: |
base_sha="${{ github.event.before }}"
head_sha="${{ github.sha }}"
if [[ "$base_sha" =~ ^0+$ ]]; then
base_sha="$(git rev-parse "$head_sha^")"
fi
echo "base_sha=$base_sha" >> "$GITHUB_OUTPUT"
echo "head_sha=$head_sha" >> "$GITHUB_OUTPUT"
- name: discover changed and testable modules
id: discover
env:
BASE_SHA: ${{ steps.commit-range.outputs.base_sha }}
HEAD_SHA: ${{ steps.commit-range.outputs.head_sha }}
GITHUB_EVENT_NAME: ${{ github.event_name }}
run: |
node <<'NODE'
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
const baseSha = process.env.BASE_SHA;
const headSha = process.env.HEAD_SHA;
const eventName = process.env.GITHUB_EVENT_NAME;
let changedFiles = [];
try {
const diff = execSync(`git diff --name-only ${baseSha} ${headSha}`, { encoding: "utf8" });
changedFiles = diff.split("\n").map((s) => s.trim()).filter(Boolean);
} catch {
changedFiles = [];
}
const changedModules = Array.from(
new Set(
changedFiles
.map((file) => {
const match = file.match(/^src\/modules\/([^/]+)\//);
return match ? match[1] : null;
})
.filter(Boolean)
)
);
const coreChanged =
eventName === "workflow_dispatch" ||
changedFiles.some((file) => file.startsWith("src/server/core/"));
const modulesRoot = path.join("src", "modules");
const testableModules = [];
function hasTestFiles(dirPath) {
const stack = [dirPath];
while (stack.length > 0) {
const current = stack.pop();
const entries = fs.readdirSync(current, { withFileTypes: true });
for (const entry of entries) {
if (entry.name === "node_modules" || entry.name.startsWith(".")) {
continue;
}
const fullPath = path.join(current, entry.name);
if (entry.isDirectory()) {
stack.push(fullPath);
continue;
}
if (/\.(test|spec)\.[cm]?[jt]sx?$/.test(entry.name)) {
return true;
}
}
}
return false;
}
for (const moduleName of fs.readdirSync(modulesRoot)) {
const containerPath = path.join(modulesRoot, moduleName, "container");
const packagePath = path.join(modulesRoot, moduleName, "container", "package.json");
if (!fs.existsSync(packagePath)) {
continue;
}
try {
const pkg = JSON.parse(fs.readFileSync(packagePath, "utf8"));
if (pkg?.scripts?.test && hasTestFiles(containerPath)) {
testableModules.push(moduleName);
}
} catch {
// Skip invalid package.json files.
}
}
const selected = coreChanged
? testableModules
: testableModules.filter((moduleName) => changedModules.includes(moduleName));
const matrix = selected.map((moduleName) => ({
module: moduleName,
}));
const out = process.env.GITHUB_OUTPUT;
fs.appendFileSync(out, `matrix=${JSON.stringify(matrix)}\n`);
fs.appendFileSync(out, `has_modules=${matrix.length > 0}\n`);
fs.appendFileSync(out, `selected_modules=${selected.join(",")}\n`);
fs.appendFileSync(out, `module_count=${matrix.length}\n`);
NODE
module-tests:
name: Module Tests
needs: discover-modules
if: needs.discover-modules.outputs.has_modules == 'true'
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include: ${{ fromJson(needs.discover-modules.outputs.matrix) }}
steps:
- name: checkout repository
uses: actions/checkout@v6
- name: module under test
run: |
echo "Testing module: ${{ matrix.module }}"
- name: setup node
uses: actions/setup-node@v6
with:
node-version: 22
- name: ensure module test Dockerfile exists
run: |
dockerfile_path="src/modules/${{ matrix.module }}/container/Dockerfile.test"
if [[ -f "$dockerfile_path" ]]; then
echo "Using existing test Dockerfile at $dockerfile_path"
exit 0
fi
echo "No test Dockerfile found at $dockerfile_path, creating CI test Dockerfile"
cat > "$dockerfile_path" <<EOF_DOCKER
FROM node:22-alpine
WORKDIR /home/node/module
COPY src/server/core ./core
COPY src/modules/${{ matrix.module }}/container ./
RUN npm install
CMD ["npm", "run", "development"]
EOF_DOCKER
- name: run tests for module
run: |
cd "src/modules/${{ matrix.module }}/container"
npm ci
npm run test
no-modules-changed:
name: No module tests required
needs: discover-modules
if: needs.discover-modules.outputs.has_modules != 'true'
runs-on: ubuntu-latest
steps:
- name: print skip reason
run: |
echo "No changed modules with both a test script and test files were detected. Module count: ${{ needs.discover-modules.outputs.module_count }}"
slack-finish:
name: Slack final status
if: ${{ always() && needs.slack-start.result == 'success' && needs.slack-start.outputs.ts != '' }}
needs:
- slack-start
- discover-modules
- module-tests
- no-modules-changed
runs-on: ubuntu-latest
steps:
- name: checkout repository
uses: actions/checkout@v6
- name: set end timestamp
id: end-time
run: |
echo "value=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> "$GITHUB_OUTPUT"
- name: determine workflow status
id: workflow-status
run: |
workflow_status="success"
if [[ "${{ needs.discover-modules.result }}" == "failure" || "${{ needs.discover-modules.result }}" == "cancelled" || "${{ needs.module-tests.result }}" == "failure" || "${{ needs.module-tests.result }}" == "cancelled" || "${{ needs.no-modules-changed.result }}" == "failure" || "${{ needs.no-modules-changed.result }}" == "cancelled" ]]; then
workflow_status="failure"
fi
echo "status=$workflow_status" >> "$GITHUB_OUTPUT"
- name: update slack message
uses: ./.github/actions/slack-run-finish
with:
ts: ${{ needs.slack-start.outputs.ts }}
status: ${{ steps.workflow-status.outputs.status }}
message_prefix: Workflow
extra_text: ". Modules selected: ${{ needs.discover-modules.outputs.module_count }}"