-
Notifications
You must be signed in to change notification settings - Fork 20
/
entrypoint.js
196 lines (178 loc) · 6.34 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
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
import * as childProcess from 'child_process';
import core from '@actions/core';
import semanticRelease from 'semantic-release';
import JSON5 from 'json5';
import arrify from 'arrify';
import { cosmiconfig } from 'cosmiconfig';
const parseInput = (input, defaultValue = '') => {
try {
return JSON5.parse(input);
} catch (err) {
return defaultValue || 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', '--no-audit', '--no-fund', '--force', ...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;
}
};
/**
* Sets the github workspace as a safe directory in the global git config.
*
* @returns {object} - Response from `child_process.spawnSync()`.
*/
const setGitConfigSafeDirectory = () => {
try {
core.debug(`Enabling github workspace as a git safe directory`);
const spawn = childProcess.spawnSync('git', [
'config',
'--global',
'--add',
'safe.directory',
process.env.GITHUB_WORKSPACE,
]);
if (spawn.status !== 0) {
throw new Error(spawn.stderr);
}
core.debug(`Set ${process.env.GITHUB_WORKSPACE} as a safe directory.`);
return spawn;
} catch (err) {
core.debug(`Error setting ${process.env.GITHUB_WORKSPACE} as a safe directory.`);
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 workingDirectory =
parseInput(core.getInput('working-directory', { required: false })) || '.';
const configFile = await cosmiconfig('release')
.search(workingDirectory)
.then((result) => result?.config);
const branch = parseInput(core.getInput('branch', { required: false }));
// Branches are parsed in this order:
// 1. Input from the action
// 2. Config file
// 3. Default branches set in this action = semantic-release's default branches with the addition of `main`.
const branches = parseInput(
core.getInput('branches', { required: false }),
configFile?.branches || [
'master',
'main',
'next',
'next-major',
'+([0-9])?(.{+([0-9]),x}).x',
{ name: 'beta', prerelease: true },
{ name: 'alpha', prerelease: true },
{ name: 'canary', prerelease: true },
],
);
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}`);
core.debug(`working-directory input: ${workingDirectory}`);
setGitConfigSafeDirectory();
// 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, { cwd: workingDirectory });
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);