Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions scripts/github/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }…` );

Expand All @@ -20,7 +37,7 @@ const getConfig = ({ gitBranchName }) => {
const githubConfig = {
assets: [
{
path: `./release/${ repoName }.zip`,
path: releaseAssetPath,
label: `${ repoName }.zip`,
},
],
Expand Down Expand Up @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions scripts/github/verify-release-asset.js
Original file line number Diff line number Diff line change
@@ -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.'
);
}
Comment thread
adekbadek marked this conversation as resolved.
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.'
);
}
Comment thread
adekbadek marked this conversation as resolved.
utils.log( `Verified release asset at ${ releaseAssetPath }.` );
}

module.exports = { prepare };