forked from codfish/semantic-release-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.js
141 lines (125 loc) · 4.68 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import * as childProcess from 'child_process';
import core from '@actions/core';
import semanticRelease from 'semantic-release';
import JSON5 from 'json5';
import arrify from '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 spawn = childProcess.spawnSync('npm', ['install', '--no-save', ...packagesArr], {
stdio: ['inherit', 'inherit', 'pipe'],
});
if (spawn.status !== 0) {
throw new Error(spawn.stderr);
}
core.debug(`Packages installed.`);
return spawn;
} 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(`additional_packages input: ${additionalPackages}`);
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 plugins/configurations
if (extendsInput) {
additionalPackages.push(...arrify(extendsInput));
}
if (additionalPackages.length) {
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, type, channel, gitHead, gitTag, name } = 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.exportVariable('RELEASE_TYPE', type);
core.exportVariable('RELEASE_CHANNEL', channel);
core.exportVariable('RELEASE_GIT_HEAD', gitHead);
core.exportVariable('RELEASE_GIT_TAG', gitTag);
core.exportVariable('RELEASE_NAME', name);
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);
core.setOutput('type', type);
core.setOutput('channel', channel);
core.setOutput('git-head', gitHead);
core.setOutput('git-tag', gitTag);
core.setOutput('name', name);
}
run().catch(core.setFailed);