/
entrypoint.js
121 lines (105 loc) · 3.94 KB
/
entrypoint.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
const { spawnSync } = require('child_process');
const core = require('@actions/core');
const semanticRelease = require('semantic-release');
const JSON5 = require('json5');
const arrify = require('arrify');
const parseInput = input => {
try {
return JSON5.parse(input);
} catch (err) {
return input;
}
};
/**
* Install npm packages.
*
* @param {string|string[]} packages - List of packages to install.
* @returns {object} - Response from `child_process.spawnSync()`.
*/
const installPackages = packages => {
try {
const packagesArr = arrify(packages);
core.debug(`Installing additional packages: ${packagesArr}`);
const result = spawnSync('npm', ['install', '--no-save', ...packagesArr]);
core.debug(`Packages installed.`);
return result;
} catch (err) {
core.debug(`Error installing additional packages: ${packages}`);
throw err;
}
};
/**
* Run semantic-release.
*
* @see https://github.com/semantic-release/semantic-release/blob/master/docs/developer-guide/js-api.md
* @see https://github.com/semantic-release/semantic-release/blob/master/docs/usage/configuration.md#options
*/
async function run() {
const branch = parseInput(core.getInput('branch', { required: false }));
const branches = parseInput(core.getInput('branches', { required: false }));
const plugins = parseInput(core.getInput('plugins', { required: false }));
const additionalPackages = parseInput(core.getInput('additional_packages', { required: false }));
const extendsInput = parseInput(core.getInput('extends', { required: false }));
let dryRun = core.getInput('dry_run', { required: false });
dryRun = dryRun !== '' ? dryRun === 'true' : '';
const repositoryUrl = core.getInput('repository_url', { required: false });
const tagFormat = core.getInput('tag_format', { required: false });
core.debug(`branch input: ${branch}`);
core.debug(`branches input: ${branches}`);
core.debug(`plugins input: ${plugins}`);
core.debug(`extends input: ${extendsInput}`);
core.debug(`dry_run input: ${dryRun}`);
core.debug(`repository_url input: ${repositoryUrl}`);
core.debug(`tag_format input: ${tagFormat}`);
// install additional packages
if (additionalPackages) {
installPackages(additionalPackages);
}
// build options object
const branchOption = branch ? { branches: branch } : { branches };
const options = {
...branchOption,
plugins,
extends: extendsInput,
dryRun,
repositoryUrl,
tagFormat,
};
core.debug(`options before cleanup: ${JSON.stringify(options)}`);
// remove falsey options
Object.keys(options).forEach(
key => (options[key] === undefined || options[key] === '') && delete options[key],
);
core.debug(`options after cleanup: ${JSON.stringify(options)}`);
const result = await semanticRelease(options);
if (!result) {
core.debug('No release published');
// set outputs
core.exportVariable('NEW_RELEASE_PUBLISHED', 'false');
core.setOutput('new-release-published', 'false');
return;
}
const { lastRelease, nextRelease, commits } = result;
core.debug(
`Published ${nextRelease.type} release version ${nextRelease.version} containing ${commits.length} commits.`,
);
if (lastRelease.version) {
core.debug(`The last release was "${lastRelease.version}".`);
}
// set outputs
const { version, notes } = nextRelease;
const [major, minor, patch] = version.split('.');
core.exportVariable('NEW_RELEASE_PUBLISHED', 'true');
core.exportVariable('RELEASE_VERSION', version);
core.exportVariable('RELEASE_MAJOR', major);
core.exportVariable('RELEASE_MINOR', minor);
core.exportVariable('RELEASE_PATCH', patch);
core.exportVariable('RELEASE_NOTES', notes);
core.setOutput('new-release-published', 'true');
core.setOutput('release-version', version);
core.setOutput('release-major', major);
core.setOutput('release-minor', minor);
core.setOutput('release-patch', patch);
core.setOutput('release-notes', notes);
}
run().catch(core.setFailed);