-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpipeline.js
198 lines (183 loc) · 5.79 KB
/
pipeline.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
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
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const cp = require('child_process');
const BASE_PROTOCOL = 'base://';
const DCC_PROTOCOL = 'dcc://';
const PIPELINE_FILE = '.github/workflows/publish.yaml';
function languages() {
const skipLanguagesRaw =
process.env.npm_config_skip_languages ||
JSON.parse(fs.readFileSync('package.json')).config?.['skip-languages'];
const skipLanguages = skipLanguagesRaw?.split(',') || [];
return fs
.readdirSync('examples')
.filter(
(x) =>
x.endsWith('.yaml') && fs.statSync(path.join('examples', x)).isFile()
)
.map((x) => x.replace(/\.yaml$/, ''))
.filter((x) => !skipLanguages.includes(x))
.map((x) => DCC_PROTOCOL + x);
}
function bases() {
return fs.readdirSync('base-images').map((x) => BASE_PROTOCOL + x);
}
function toFile(ref) {
if (ref.startsWith(BASE_PROTOCOL)) {
return 'base-images/' + ref.slice(BASE_PROTOCOL.length);
} else if (ref.startsWith(DCC_PROTOCOL)) {
return 'examples/' + ref.slice(DCC_PROTOCOL.length) + '.yaml';
}
}
function readDependency(file) {
if (file.startsWith('examples/')) {
const languageYaml = yaml.load(fs.readFileSync(file, 'utf8'));
return languageYaml.extends || BASE_PROTOCOL + 'debian';
} else if (file.startsWith('base-images/')) {
const devcontainerJson = JSON.parse(
fs.readFileSync(path.join(file, '.devcontainer.json'), 'utf8')
);
let lookupImage;
if (devcontainerJson.image) {
lookupImage = devcontainerJson.image;
} else if (devcontainerJson.build && devcontainerJson.build.dockerfile) {
const dockerfilePath = path.join(file, devcontainerJson.build.dockerfile);
const dockerfile = fs.readFileSync(dockerfilePath, 'utf8');
const match = dockerfile.match(/^FROM (.*)$/m);
if (match) {
lookupImage = match[1];
} else {
throw new Error(`Could not find FROM statement in ${dockerfilePath}`);
}
}
const baseImage = lookupImage.replace(
'ghcr.io/dhhyi/dcc-base-',
BASE_PROTOCOL
);
if (baseImage === lookupImage) {
return 'features';
} else {
return baseImage;
}
}
}
function buildJob(ref) {
return ref
.replace(/:\/\/(\w)/, (_, p1) => p1.toUpperCase())
.replace(/-([a-z])/g, (_, p1) => p1.toUpperCase());
}
const workflow = {
name: 'Publish',
on: {
push: { branches: ['main'] },
},
jobs: {
calc: {
'runs-on': 'ubuntu-latest',
outputs: {
buildLanguages: '${{ steps.calculate.outputs.buildLanguages }}',
buildBase: '${{ steps.calculate.outputs.buildBase }}',
...languages().reduce((acc, lang) => {
acc[buildJob(lang)] =
'${{ steps.calculate.outputs.' + buildJob(lang) + ' }}';
return acc;
}, {}),
},
// find commits: https://github.com/orgs/community/discussions/25797
// find commit length: https://github.com/orgs/community/discussions/27125
env: {
commits: '${{ toJSON(github.event.commits) }}',
},
steps: [
{ uses: 'actions/checkout@master', with: { 'fetch-depth': 0 } },
{
run: 'npm exec pnpm -- i --ignore-scripts',
name: 'npm install',
},
{
run: 'npm run lint\nnpm run format\nnpm run synchronize-ci\ngit diff --exit-code',
name: 'Check Integrity',
},
// {
// name: 'Debug',
// run: 'echo "${{ toJSON(github) }}"',
// },
{
run: 'noOfCommits=$(echo $commits | jq ". | length")\ngit diff --name-only HEAD~$noOfCommits',
name: 'Report Changed Files',
},
{
id: 'calculate',
run: 'noOfCommits=$(echo $commits | jq ". | length")\ngit diff --name-only HEAD~$noOfCommits | node check-lang-ci.js >> $GITHUB_OUTPUT\ncat $GITHUB_OUTPUT',
name: 'Calculate Required Jobs',
},
],
},
bundle: {
uses: './.github/workflows/bundle.yaml',
secrets: 'inherit',
},
features: {
needs: ['calc'],
if: '${{ ! failure() && ! cancelled() && always() }}',
uses: './.github/workflows/features.yaml',
with: {
skip: "${{ needs.calc.outputs.buildBase == 'false' }}",
},
secrets: 'inherit',
},
},
};
bases().forEach((ref) => {
const file = toFile(ref);
const dependency = readDependency(file);
workflow.jobs[buildJob(ref)] = {
needs: ['calc', buildJob(dependency)],
if: '${{ ! failure() && ! cancelled() && always() }}',
name: ref,
uses: './.github/workflows/base.yaml',
with: {
name: ref.replace(BASE_PROTOCOL, ''),
skip: "${{ needs.calc.outputs.buildBase == 'false' }}",
},
secrets: 'inherit',
};
});
languages().forEach((ref) => {
const file = toFile(ref);
const dependency = readDependency(file);
workflow.jobs[buildJob(ref)] = {
needs: ['calc', 'bundle', buildJob(dependency)],
if: '${{ ! failure() && ! cancelled() && always() }}',
name: ref,
uses: './.github/workflows/dcc.yaml',
with: {
name: ref.replace(DCC_PROTOCOL, ''),
skip: '${{ needs.calc.outputs.' + buildJob(ref) + " == 'false' }}",
},
secrets: 'inherit',
};
});
workflow.jobs.examples = {
needs: ['calc', ...languages().map((x) => buildJob(x))],
if: '${{ ! failure() && ! cancelled() && always() }}',
uses: './.github/workflows/examples.yaml',
with: {
skip: "${{ needs.calc.outputs.buildLanguages == 'false' }}",
},
secrets: 'inherit',
};
fs.writeFileSync(
PIPELINE_FILE,
'# This file is generated by pipeline.js\n' +
yaml.dump(workflow, { quotingType: '"', noCompatMode: true })
);
try {
cp.execSync(`git diff --exit-code --raw -p --stat ${PIPELINE_FILE}`, {
stdio: 'inherit',
});
} catch (_error) {
console.log('ci pipeline was not in sync, I updated it for you');
process.exit(1);
}