-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolkadot-ci-ghact-build.cjs
executable file
·251 lines (199 loc) · 6.28 KB
/
polkadot-ci-ghact-build.cjs
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
#!/usr/bin/env node
// Copyright 2017-2021 @polkadot/dev authors & contributors
// SPDX-License-Identifier: Apache-2.0
// const { spawnSync } = require('child_process');
const fs = require('fs');
const os = require('os');
const path = require('path');
const argv = require('yargs')
.options({
'skip-beta': {
description: 'Do not increment as beta',
type: 'boolean'
}
})
.strict()
.argv;
const copySync = require('./copySync.cjs');
const execSync = require('./execSync.cjs');
const repo = `https://${process.env.GH_PAT}@github.com/${process.env.GITHUB_REPOSITORY}.git`;
console.log('$ polkadot-ci-ghact-build', process.argv.slice(2).join(' '));
function runClean () {
execSync('yarn polkadot-dev-clean-build');
}
function runCheck () {
execSync('yarn lint');
}
function runTest () {
execSync('yarn test');
// if [ -f "coverage/lcov.info" ] && [ -n "$COVERALLS_REPO_TOKEN" ]; then
// console.log('*** Submitting to coveralls.io');
// (cat coverage/lcov.info | yarn run coveralls) || true
// fi
}
function runBuild () {
execSync('yarn build');
}
function npmGetVersion () {
return JSON.parse(
fs.readFileSync(path.resolve(process.cwd(), 'package.json'), 'utf8')
).version;
}
function npmSetup () {
const registry = 'registry.npmjs.org';
fs.writeFileSync(path.join(os.homedir(), '.npmrc'), `//${registry}/:_authToken=${process.env.NPM_TOKEN}`);
}
function npmPublish () {
if (fs.existsSync('.skip-npm')) {
return;
}
['LICENSE', 'package.json']
.filter((file) => !fs.existsSync(path.join(process.cwd(), 'build', file)))
.forEach((file) => copySync(file, 'build'));
process.chdir('build');
const tag = npmGetVersion().includes('-') ? '--tag beta' : '';
let count = 1;
while (true) {
try {
execSync(`npm publish --access public ${tag}`);
break;
} catch (error) {
if (count < 5) {
const end = Date.now() + 15000;
console.error(`Publish failed on attempt ${count}/5. Retrying in 15s`);
count++;
while (Date.now() < end) {
// just spin our wheels
}
}
}
}
process.chdir('..');
}
function gitSetup () {
execSync('git config push.default simple');
execSync('git config merge.ours.driver true');
execSync('git config user.name "Github Actions"');
execSync('git config user.email "action@github.com"');
execSync('git checkout master');
}
// function createContributors () {
// const child = spawnSync('git', ['--no-pager', 'shortlog', '--summary', '--numbered', '--email'], {
// encoding: 'utf-8',
// stdio: ['inherit', 'pipe', 'pipe']
// });
// const contributors = child.stdout
// .split('\n')
// .filter((l) =>
// l.includes('\t') &&
// !(
// l.startsWith('Travis CI') ||
// l.startsWith('GitHub') ||
// l.includes('<>') ||
// l.includes('<action@github.com>') ||
// l.includes('[bot]')
// )
// )
// .map((l) => l.split('\t'))
// .map(([count, author]) => {
// const [name, email] = author.split(' <');
// return [parseInt(count.trim()), name, `<${email}`];
// })
// .reduce((all, [count, name, email]) => {
// const first = all.find(([, n, e]) => {
// if (e === email) {
// return true;
// } else if (n === name) {
// const [userA, providerA] = email.split('@');
// const [userB, providerB] = email.split('@');
// return userA === userB || providerA === providerB;
// }
// return false;
// });
// if (first) {
// first[0] += count;
// } else {
// all.push([count, name, email]);
// }
// return all;
// }, [])
// .sort((a, b) => b[0] - a[0])
// .map(([count, name, email]) => `${count.toString().padStart(8)} ${name} ${email}`)
// .join('\n');
// fs.writeFileSync('CONTRIBUTORS', `${contributors}\n`);
// }
function gitBump () {
const currentVersion = npmGetVersion();
const [version, tag] = currentVersion.split('-');
const [,, patch] = version.split('.');
if (argv['skip-beta'] || patch === '0') {
// don't allow beta versions
execSync('yarn polkadot-dev-version patch');
} else {
const triggerPath = path.join(process.cwd(), '.123trigger');
const available = fs.readFileSync(triggerPath, 'utf-8').split('\n');
if (tag || patch === '1' || available.includes(currentVersion)) {
// if we have a beta version, just continue the stream of betas
execSync('yarn polkadot-dev-version pre');
} else {
// manual setting of version, make some changes so we can commit
fs.appendFileSync(triggerPath, `\n${currentVersion}`);
}
}
// createContributors();
execSync('git add --all .');
}
function gitPush () {
const version = npmGetVersion();
let doGHRelease = false;
if (process.env.GH_RELEASE_GITHUB_API_TOKEN) {
const changes = fs.readFileSync('CHANGELOG.md', 'utf8');
if (changes.includes(`## ${version}`)) {
doGHRelease = true;
} else if (version.endsWith('.1')) {
throw new Error(`Unable to release, no CHANGELOG entry for ${version}`);
}
}
execSync('git add --all .');
if (fs.existsSync('docs/README.md')) {
execSync('git add --all -f docs');
}
// add the skip checks for GitHub ...
execSync(`git commit --no-status --quiet -m "[CI Skip] release/${version.includes('-') ? 'beta' : 'stable'} ${version}
skip-checks: true"`);
execSync(`git push ${repo} HEAD:${process.env.GITHUB_REF}`, true);
if (doGHRelease) {
const files = process.env.GH_RELEASE_FILES
? `--assets ${process.env.GH_RELEASE_FILES}`
: '';
execSync(`yarn polkadot-exec-ghrelease --draft ${files} --yes`);
}
}
function loopFunc (fn) {
if (fs.existsSync('packages')) {
fs
.readdirSync('packages')
.filter((dir) => {
const pkgDir = path.join(process.cwd(), 'packages', dir);
return fs.statSync(pkgDir).isDirectory() &&
fs.existsSync(path.join(pkgDir, 'package.json')) &&
fs.existsSync(path.join(pkgDir, 'build'));
})
.forEach((dir) => {
process.chdir(path.join('packages', dir));
fn();
process.chdir('../..');
});
} else {
fn();
}
}
gitSetup();
gitBump();
npmSetup();
runClean();
runCheck();
runTest();
runBuild();
gitPush();
loopFunc(npmPublish);