diff --git a/scripts/github/release.js b/scripts/github/release.js index 391468e..1315be3 100644 --- a/scripts/github/release.js +++ b/scripts/github/release.js @@ -10,8 +10,25 @@ const { files, ...otherArgs } = require( 'yargs/yargs' )( const filesList = files.split( ',' ); -// Get repository name from GitHub Actions environment variable (format: owner/repo). -const repoName = process.env.GITHUB_REPOSITORY?.split( '/' )[ 1 ] || 'unknown'; +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 }…` ); @@ -20,7 +37,7 @@ const getConfig = ({ gitBranchName }) => { const githubConfig = { assets: [ { - path: `./release/${ repoName }.zip`, + path: releaseAssetPath, label: `${ repoName }.zip`, }, ], @@ -97,6 +114,11 @@ const getConfig = ({ gitBranchName }) => { }, ] ); + // 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; diff --git a/scripts/github/verify-release-asset.js b/scripts/github/verify-release-asset.js new file mode 100644 index 0000000..e637957 --- /dev/null +++ b/scripts/github/verify-release-asset.js @@ -0,0 +1,27 @@ +'use strict'; + +const fs = require( 'fs' ); +const path = require( 'path' ); + +const utils = require( '../utils/index.js' ); + +const repoName = process.env.GITHUB_REPOSITORY?.split( '/' )[ 1 ]; +const releaseAssetPath = path.resolve( `./release/${ repoName }.zip` ); + +async function prepare() { + if ( ! repoName ) { + throw new Error( + 'GITHUB_REPOSITORY is not set; cannot determine release asset path.' + ); + } + if ( ! fs.existsSync( releaseAssetPath ) ) { + throw new Error( + `Release asset not found at ${ releaseAssetPath }. ` + + 'The `release:archive` script must produce this file before ' + + 'semantic-release publishes the GitHub release.' + ); + } + utils.log( `Verified release asset at ${ releaseAssetPath }.` ); +} + +module.exports = { prepare };