-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrelease.js
More file actions
187 lines (163 loc) · 5.16 KB
/
Copy pathrelease.js
File metadata and controls
187 lines (163 loc) · 5.16 KB
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
'use strict';
const utils = require( '../utils/index.js' );
const semanticRelease = require( 'semantic-release' );
const { files, ...otherArgs } = require( 'yargs/yargs' )(
process.argv.slice( 2 )
).parse();
const filesList = files.split( ',' );
if ( ! process.env.GITHUB_REPOSITORY ) {
console.error(
'GITHUB_REPOSITORY is not set. This script must run inside GitHub Actions ' +
'so the release asset path can be resolved. Aborting before semantic-release ' +
'publishes a release without an attached ZIP.'
);
process.exit( 1 );
}
const [ repoOwner, repoName ] = process.env.GITHUB_REPOSITORY.split( '/' );
if ( ! repoOwner || ! repoName ) {
console.error(
`GITHUB_REPOSITORY must be in "owner/repo" format; received "${ process.env.GITHUB_REPOSITORY }". ` +
'Aborting before semantic-release publishes a release with an invalid asset path.'
);
process.exit( 1 );
}
const releaseAssetPath = `./release/${ repoName }.zip`;
utils.log( `Releasing ${ repoName }…` );
const getConfig = ({ gitBranchName }) => {
const branchType = gitBranchName.split("/")[0];
const githubConfig = {
assets: [
{
path: releaseAssetPath,
label: `${ repoName }.zip`,
},
],
releasedLabels: false,
};
// Only post GH PR comments for alpha, hotfix/*, and release branches.
if ( ! ["alpha", "hotfix", "release"].includes(branchType) ) {
githubConfig.successComment = false;
githubConfig.failComment = false;
}
// Only publish alpha and release branches to NPM.
const shouldPublishOnNPM = Boolean( process.env.NPM_TOKEN ) && ["alpha", "release"].includes(branchType);
if ( shouldPublishOnNPM ) {
utils.log( `Will publish to npm.` );
}
const config = {
dryRun: otherArgs.dryRun,
ci: otherArgs.ci,
debug: otherArgs.debug,
branches: [
// `release` branch is published on the main distribution channel (a new version on GH).
"release",
// `alpha` branch – for regular pre-releases.
{
name: "alpha",
prerelease: true,
},
// `hotfix/*` branches – for releases outside of the release schedule.
{
name: "hotfix/*",
// With `prerelease: true`, the `name` would be used for the pre-release tag. A name with a `/`
// is not valid, though. See https://semver.org/#spec-item-9.
prerelease: '${name.replace(/\\//g, "-")}',
},
// `epic/*` branches – for beta testing/QA pre-release builds.
{
name: "epic/*",
// With `prerelease: true`, the `name` would be used for the pre-release tag. A name with a `/`
// is not valid, though. See https://semver.org/#spec-item-9.
prerelease: '${name.replace(/\\//g, "-")}',
},
],
prepare: ["@semantic-release/changelog", "@semantic-release/npm"],
plugins: [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
[
// Whether to publish on npm.
"@semantic-release/npm",
{
npmPublish: shouldPublishOnNPM,
},
],
"semantic-release-version-bump",
// Add the built ZIP archive to GH release.
[
"@semantic-release/github",
githubConfig,
],
],
};
// Bump the semver and prepare a build package.
config.prepare.push( [
// Increment the version in additional files, and the create the release archive.
'semantic-release-version-bump',
{
files: filesList,
callback: 'npm run release:archive',
},
] );
// Verify the release archive exists on disk after `release:archive` ran.
// `@semantic-release/github` silently ignores missing assets, so without this
// guard a misconfigured archive step would publish a release with no ZIP.
config.prepare.push( require.resolve( './verify-release-asset.js' ) );
// Unless on a hotfix or epic branch, add a commit that updates the files.
if ( [ 'hotfix', 'epic' ].indexOf( branchType ) === -1 ) {
let assets = filesList;
// These assets should be added to source control after a release.
if ( branchType === 'release' ) {
assets = [
...filesList,
'package.json',
'package-lock.json',
'CHANGELOG.md',
];
}
utils.log(
`On ${ branchType } branch, following files will be updated: ${ assets.join(
', '
) }`
);
config.prepare.push( {
path: '@semantic-release/git',
assets,
message:
'chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}',
} );
} else {
utils.log(
`This branch is ${ branchType }, plugin files and the changelog will *not* be updated.`
);
}
return config;
};
const run = async () => {
try {
const gitBranch = await utils.getGitBranch();
const result = await semanticRelease.default(
getConfig( { gitBranchName: gitBranch } )
);
if ( result ) {
const { lastRelease, commits, nextRelease, releases } = result;
utils.log(
`Published ${ nextRelease.type } release version ${ nextRelease.version } containing ${ commits.length } commits.`
);
if ( lastRelease.version ) {
utils.log( `The last release was "${ lastRelease.version }".` );
}
for ( const release of releases ) {
utils.log(
`The release was published with plugin "${ release.pluginName }".`
);
}
} else {
utils.log( 'No release published.' );
}
} catch ( err ) {
console.error( 'The automated release failed with %O', err );
process.exit( 1 );
}
};
run();