Skip to content

Commit eb0004b

Browse files
authored
fix(release): fail when GitHub release asset is missing
1 parent b4fa032 commit eb0004b

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

scripts/github/release.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,25 @@ const { files, ...otherArgs } = require( 'yargs/yargs' )(
1010

1111
const filesList = files.split( ',' );
1212

13-
// Get repository name from GitHub Actions environment variable (format: owner/repo).
14-
const repoName = process.env.GITHUB_REPOSITORY?.split( '/' )[ 1 ] || 'unknown';
13+
if ( ! process.env.GITHUB_REPOSITORY ) {
14+
console.error(
15+
'GITHUB_REPOSITORY is not set. This script must run inside GitHub Actions ' +
16+
'so the release asset path can be resolved. Aborting before semantic-release ' +
17+
'publishes a release without an attached ZIP.'
18+
);
19+
process.exit( 1 );
20+
}
21+
22+
const [ repoOwner, repoName ] = process.env.GITHUB_REPOSITORY.split( '/' );
23+
if ( ! repoOwner || ! repoName ) {
24+
console.error(
25+
`GITHUB_REPOSITORY must be in "owner/repo" format; received "${ process.env.GITHUB_REPOSITORY }". ` +
26+
'Aborting before semantic-release publishes a release with an invalid asset path.'
27+
);
28+
process.exit( 1 );
29+
}
30+
31+
const releaseAssetPath = `./release/${ repoName }.zip`;
1532

1633
utils.log( `Releasing ${ repoName }…` );
1734

@@ -20,7 +37,7 @@ const getConfig = ({ gitBranchName }) => {
2037
const githubConfig = {
2138
assets: [
2239
{
23-
path: `./release/${ repoName }.zip`,
40+
path: releaseAssetPath,
2441
label: `${ repoName }.zip`,
2542
},
2643
],
@@ -97,6 +114,11 @@ const getConfig = ({ gitBranchName }) => {
97114
},
98115
] );
99116

117+
// Verify the release archive exists on disk after `release:archive` ran.
118+
// `@semantic-release/github` silently ignores missing assets, so without this
119+
// guard a misconfigured archive step would publish a release with no ZIP.
120+
config.prepare.push( require.resolve( './verify-release-asset.js' ) );
121+
100122
// Unless on a hotfix or epic branch, add a commit that updates the files.
101123
if ( [ 'hotfix', 'epic' ].indexOf( branchType ) === -1 ) {
102124
let assets = filesList;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const fs = require( 'fs' );
4+
const path = require( 'path' );
5+
6+
const utils = require( '../utils/index.js' );
7+
8+
const repoName = process.env.GITHUB_REPOSITORY?.split( '/' )[ 1 ];
9+
const releaseAssetPath = path.resolve( `./release/${ repoName }.zip` );
10+
11+
async function prepare() {
12+
if ( ! repoName ) {
13+
throw new Error(
14+
'GITHUB_REPOSITORY is not set; cannot determine release asset path.'
15+
);
16+
}
17+
if ( ! fs.existsSync( releaseAssetPath ) ) {
18+
throw new Error(
19+
`Release asset not found at ${ releaseAssetPath }. ` +
20+
'The `release:archive` script must produce this file before ' +
21+
'semantic-release publishes the GitHub release.'
22+
);
23+
}
24+
utils.log( `Verified release asset at ${ releaseAssetPath }.` );
25+
}
26+
27+
module.exports = { prepare };

0 commit comments

Comments
 (0)