forked from fengmk2/github-actions
/
index.js
78 lines (64 loc) · 2.74 KB
/
index.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
const path = require('path');
const fs = require('fs');
const core = require('@actions/core');
const { getExecOutput } = require('@actions/exec');
const semanticRelease = require('semantic-release');
const { request } = require('undici');
async function execGit(cmd) {
const { stdout } = await getExecOutput(cmd);
return stdout.trim();
}
async function run() {
const mainRepoPath = process.cwd();
const pkgInfo = require(`${mainRepoPath}/package.json`);
const registry = pkgInfo.publishConfig?.registry || 'https://registry.npmjs.org';
core.setOutput('name', pkgInfo.name);
core.setOutput('registry', registry);
const lastCommitId = await execGit(`git log -n1 --format="%h"`);
try {
const configFiles = [
path.join(__dirname, 'release.config.js'),
path.join(mainRepoPath, 'release.config.js'),
].filter(file => fs.existsSync(file));
core.info(`Using config files: ${configFiles.join(', ')}`);
const result = await semanticRelease({
dryRun: process.env.DRYRUN === 'true',
extends: configFiles,
});
const { nextRelease, lastRelease } = result;
if (!nextRelease) {
core.notice('No release need to be published.');
core.summary.addRaw('No release need to be published.');
await core.summary.write();
} else {
core.info(`Published release: ${nextRelease.version}`);
core.setOutput('release_version', nextRelease.version);
// cnpm sync
const res = await request(`https://registry.npmmirror.com/-/package/${pkgInfo.name}/syncs`, { method: 'PUT' });
const { id } = await res.body.json();
const logUrl = `https://registry.npmmirror.com/-/package/${pkgInfo.name}/syncs/${id}/log`;
core.setOutput('cnpm_sync_url', logUrl);
core.info(`cnpm sync log url: ${logUrl}`);
// write summary
core.summary.addRaw(`## [${pkgInfo.name}](https://github.com/${process.env.GITHUB_REPOSITORY})\n`);
core.summary.addRaw(`- Release: ${lastRelease?.version ?? ''} -> ${nextRelease.version}\n`);
core.summary.addRaw(`- Registry: ${registry}\n`);
core.summary.addRaw(`- CNPM Sync: ${logUrl}\n`);
core.summary.addRaw(`- DryRun: ${process.env.DRYRUN}\n`);
core.summary.addRaw(nextRelease.notes);
await core.summary.write();
}
console.log('Result:', result);
} catch (error) {
console.error('> Rollback to last commit');
const currentCommitId = await execGit(`git log -n1 --format="%h"`);
const tagId = await execGit(`git tag --contains ${currentCommitId}`);
await execGit(`git push --delete origin ${tagId}`);
await execGit(`git reset --hard ${lastCommitId}`);
await execGit(`git push --force`);
console.error('> Rollback finished');
// console.error(error);
core.setFailed(error);
}
}
run();