-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbuild-gha-workflows.ts
258 lines (235 loc) · 9.48 KB
/
build-gha-workflows.ts
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
// Recalculates the path filters for the dynamic CircleCI configuration
// Usage: node .circleci/refresh-path-filters.mjs
// Then copy the output into the CircleCI config files
// See https://circleci.com/docs/using-dynamic-configuration/
// See https://github.com/circle-makotom/circle-advanced-setup-workflow
type WorkflowMeta = {
filename: string;
displayName: string;
}
// BEGIN CONFIGURATION
const pathsFilterAction = 'dorny/paths-filter@v2';
const mainWorkflowMeta: WorkflowMeta = {
filename: 'ci-cd.yml',
displayName: 'CI/CD',
};
function getWorkspaceWorkflowMeta(workspace: Workspace): WorkflowMeta {
return {
filename: `workspace-${workspace.yamlName}.yml`,
displayName: `${workspace.name} (placeholder)`,
};
}
// END CONFIGURATION
import {exec} from 'child_process';
import fs from 'fs';
import {promisify} from 'util';
import path from 'path';
const execAsync = promisify(exec);
enum DependencyType {
Dependencies = 'dependencies',
DevDependencies = 'devDependencies',
PeerDependencies = 'peerDependencies'
}
type PackageJson = {
name: string;
location: string;
dependencies: {[name: string]: string};
devDependencies: {[name: string]: string};
peerDependencies: {[name: string]: string};
};
type Workspace = {
name: string;
location: string;
yamlName: string;
dependencies: string[];
devDependencies: string[];
peerDependencies: string[];
deepDependencies: string[];
};
type WorkspaceMap = {[name: string]: Workspace}
type WorkspaceList = Workspace[];
const workspaceTemplate = fs.readFileSync(path.join(__dirname, 'workspace-template.yml'), 'utf8');
/**
* Calculate dependencies between workspaces in this repository.
* @param workspaces The result of `npm query .workspace`.
* @param depTypes The dependency types to include in the calculation.
* @returns A map of workspace names to their dependencies.
*/
function calculateDependencies(workspaces: Array<PackageJson>, depTypes: DependencyType[]): WorkspaceMap {
const workspaceNames = workspaces.map(workspace => workspace.name);
const dependencies = workspaces.reduce((bag, workspace) => {
const workspaceEntry = depTypes.reduce((workspaceDeps, depType) => {
const deps = workspace[depType];
workspaceDeps[depType] = deps ? Object.keys(deps).filter(dep => workspaceNames.includes(dep)) : [];
return workspaceDeps;
}, <Workspace>({name: workspace.name}));
workspaceEntry.location = workspace.location;
workspaceEntry.yamlName = path.basename(workspace.location).replace(/@/g, '').replace(/[/.]/g, '-');
bag[workspace.name] = workspaceEntry;
return bag;
}, <WorkspaceMap>({}));
console.log('Calculating deep dependencies...');
const addDeepDependencies = (deepDependencies: string[], workspaceName: string, depType: DependencyType) => {
const depDeps = dependencies[workspaceName][depType] || [];
for (let dep of depDeps) {
if (deepDependencies.includes(dep)) {
continue;
}
deepDependencies.push(dep);
addDeepDependencies(deepDependencies, dep, depType);
}
};
for (let workspace of workspaces) {
const deepDependencies = dependencies[workspace.name].deepDependencies = [workspace.name];
for (let depType of depTypes) {
addDeepDependencies(deepDependencies, workspace.name, depType);
}
}
return dependencies;
};
/**
* Sort workspaces in dependency order.
* @param workspaces The workspace map to sort.
* @returns The workspaces sorted in dependency order.
*/
function sortWorkspaces(workspaces: WorkspaceMap): WorkspaceList {
// TODO: is this reliable? Do we need a full topo sort?
const sortedWorkspaces = Object.values(workspaces).sort((a, b) => {
if (a.deepDependencies.includes(b.name)) {
return 1;
}
if (b.deepDependencies.includes(a.name)) {
return -1;
}
return 0;
});
return sortedWorkspaces;
}
async function generateWorkflow(sortedWorkspaces: WorkspaceList, workspaces: WorkspaceMap): Promise<void> {
let workflowFileHandle: fs.promises.FileHandle | undefined;
let workflowStream: fs.WriteStream | undefined;
try {
workflowFileHandle = await fs.promises.open(path.join('.github', 'workflows', mainWorkflowMeta.filename), 'w');
workflowStream = workflowFileHandle.createWriteStream();
workflowStream.write([
`name: ${mainWorkflowMeta.displayName}`,
'',
'on:',
' push:',
' workflow_dispatch:',
'',
'concurrency:',
' group: "${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}"',
' cancel-in-progress: true',
'',
'jobs:',
].join('\n') + '\n');
generateChangesJob(workflowStream, sortedWorkspaces, workspaces);
generateCalls(workflowStream, sortedWorkspaces, workspaces);
} finally {
workflowStream?.end();
workflowStream?.close();
await workflowFileHandle?.close();
}
}
function generateChangesJob(workflowStream: fs.WriteStream, sortedWorkspaces: WorkspaceList, workspaces: WorkspaceMap) {
workflowStream.write([
' changes:',
' name: Detect affected workspaces',
' runs-on: ubuntu-latest',
' outputs:',
].join('\n') + '\n');
for (let workspace of sortedWorkspaces) {
workflowStream.write(` ${workspace.yamlName}: \${{ steps.filter.outputs.${workspace.yamlName} }}\n`);
}
workflowStream.write([
' steps:',
' - uses: actions/checkout@v4 # TODO: skip this for PRs',
` - uses: ${pathsFilterAction}`,
' id: filter',
' with:',
' filters: |',
' any-workspace:',
' - ".github/workflows/workspace-*.yml"',
' - "workspaces/**"',
].join('\n') + '\n');
for (let workspace of sortedWorkspaces) {
workflowStream.write([
` ${workspace.yamlName}:`,
` - ".github/workflows/workspace-${workspace.yamlName}.yml"`,
].join('\n') + '\n');
for (let dep of workspace.deepDependencies.sort()) {
workflowStream.write(` - "${workspaces[dep].location}/**"\n`);
}
}
workflowStream.write([
" - if: ${{ steps.filter.outputs.any-workspace == 'true' }}",
' uses: actions/setup-node@v3',
' with:',
' cache: npm',
' node-version-file: .nvmrc',
" - if: ${{ steps.filter.outputs.any-workspace == 'true' }}",
' uses: ./.github/actions/install-dependencies',
].join('\n') + '\n');
}
function generateCalls(workflowStream: fs.WriteStream, sortedWorkspaces: WorkspaceList, workspaces: WorkspaceMap) {
for (let workspace of sortedWorkspaces) {
workflowStream.write([
` ${workspace.yamlName}:`,
` uses: ./.github/workflows/workspace-${workspace.yamlName}.yml`,
// By default, this job will only run if the jobs it 'needs' have succeeded.
// Instead, run even if some of those are skipped, but not if they failed or if the workflow was cancelled.
` if: \${{ !failure() && !cancelled() && needs.changes.outputs.${workspace.yamlName} == 'true' }}`,
' needs:',
' - changes',
].join('\n') + '\n');
const deps = workspace.deepDependencies;
for (let dep of deps.sort()) {
if (dep == workspace.name) continue;
workflowStream.write(` - ${workspaces[dep].yamlName}\n`);
}
}
}
async function generateWorkspaceWorkflow(workspace: Workspace): Promise<void> {
const workflowMeta = getWorkspaceWorkflowMeta(workspace);
const workflowPath = path.join('.github', 'workflows', workflowMeta.filename);
if (fs.existsSync(workflowPath)) {
console.log(`Not overwriting existing workflow: ${workflowMeta.filename}`);
return;
}
let workflowFileHandle: fs.promises.FileHandle | undefined;
try {
workflowFileHandle = await fs.promises.open(workflowPath, 'w');
const workspaceWorkflow = workspaceTemplate
.replace(/WS_NAME/g, workspace.name)
.replace(/WS_LOCATION/g, workspace.location);
workflowFileHandle.write(Buffer.from(workspaceWorkflow, 'utf8'));
} finally {
workflowFileHandle?.close();
}
}
const main = async () => {
console.log('Querying workspaces...');
const packages = JSON.parse((await execAsync('npm query .workspace')).stdout) as Array<PackageJson>;
console.log('Calculating dependencies...');
const workspaces = calculateDependencies(packages, [DependencyType.Dependencies]);
console.log('Sorting modules in dependency order...');
const sortedWorkspaces = sortWorkspaces(workspaces);
console.log('Generating main workflow...');
fs.mkdirSync(path.join('.github', 'workflows'), {recursive: true});
await generateWorkflow(sortedWorkspaces, workspaces);
console.log('Generating stub workflows for workspaces...');
for (let workspace of sortedWorkspaces) {
await generateWorkspaceWorkflow(workspace);
}
};
main().then(
() => {
console.log('Done.');
process.exitCode = 0;
},
e => {
console.error(e);
process.exitCode = 1;
}
);