-
Notifications
You must be signed in to change notification settings - Fork 221
/
genMatrix.js
68 lines (51 loc) · 2.06 KB
/
genMatrix.js
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
'use strict';
const path = require('path');
const fs = require('fs');
const testFiles = [
'genMatrix.js',
'.github/workflows/build-test.yml',
'compose.yml',
];
const rcDirRegex = /^\d+\.\d+$/;
const areTestFilesChanged = (changedFiles) => changedFiles
.some((file) => testFiles.includes(file) || file.includes('templates/'));
// Returns a list of the child directories in the given path
const getChildDirectories = (parent) => fs.readdirSync(parent, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map(({ name }) => path.resolve(parent, name));
const getRocketChatVersionDirs = (base) => getChildDirectories(base)
.filter((childPath) => rcDirRegex.test(path.basename(childPath)));
// Returns the paths of Dockerfiles that are at: base/*/Dockerfile
const getDockerfilesInChildDirs = (base) => path.resolve(base, 'Dockerfile');
const getAllDockerfiles = (base) => getRocketChatVersionDirs(base)
.flatMap(getDockerfilesInChildDirs);
const getAffectedDockerfiles = (filesAdded, filesModified, filesRenamed) => {
const files = [
...filesAdded,
...filesModified,
...filesRenamed,
];
// If the test files were changed, include everything
if (areTestFilesChanged(files)) {
console.log('Test files changed so scheduling all Dockerfiles');
return getAllDockerfiles(__dirname);
}
return files.filter((file) => file.endsWith('/Dockerfile'));
};
const getFullRocketChatVersionFromDockerfile = (file) => fs.readFileSync(file, 'utf8')
.match(/^ENV RC_VERSION=(\d*\.*\d*\.\d*)/m)[1];
const getDockerfileMatrixEntry = (file) => {
const version = getFullRocketChatVersionFromDockerfile(file);
return {
version,
};
};
const generateBuildMatrix = (filesAdded, filesModified, filesRenamed) => {
const dockerfiles = [...new Set(getAffectedDockerfiles(filesAdded, filesModified, filesRenamed))];
const entries = dockerfiles.map(getDockerfileMatrixEntry);
// Return null if there are no entries so we can skip the matrix step
return entries.length
? { include: entries }
: null;
};
module.exports = generateBuildMatrix;