Skip to content

Commit a44265e

Browse files
committed
fix(repo,versioner): harden npm publish
- Fail if pnpm pack emits != 1 tarball. - Add --registry option (default: https://registry.npmjs.org/) and log it. - Release workflow: fetch full git history and publish the triggering SHA.
1 parent 3697854 commit a44265e

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ jobs:
2626
- name: Checkout
2727
uses: actions/checkout@v4
2828
with:
29-
fetch-depth: 100
29+
fetch-depth: 0
3030
fetch-tags: true
31-
ref: master
3231

3332
- name: Setup Node
3433
uses: actions/setup-node@v4

packages/versioner/src/versioner.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {
1818
dry: dryRun,
1919
publish: doPublish,
2020
push: doPush,
21+
registry: registryOverride,
2122
shortName: shortNameOverride,
2223
tag: doTag
2324
} = argv;
@@ -27,6 +28,7 @@ const parserOptions = {
2728
};
2829
const reBreaking = new RegExp(`(${parserOptions.noteKeywords.join(')|(')})`);
2930
const NPM_CLI_SPEC = 'npm@11.5.1';
31+
const DEFAULT_NPM_REGISTRY = 'https://registry.npmjs.org/';
3032

3133
type Commit = parser.Commit<string | number | symbol>;
3234

@@ -147,23 +149,32 @@ const getRepoUrls = async () => {
147149
}
148150
};
149151

150-
const publish = async (cwd: string) => {
152+
const publish = async (cwd: string, registry: string) => {
151153
if (dryRun || doPublish === false) {
152154
log.warn(chalk`{yellow Skipping Publish}`);
153155
return;
154156
}
155157

156158
log.info(chalk`\n{cyan Publishing to NPM}`);
159+
log.info(chalk`{grey Registry:} ${registry}`);
157160

158161
const packDir = mkdtempSync(join(tmpdir(), 'versioner-pack-'));
159162
try {
160163
await execa('pnpm', ['pack', '--pack-destination', packDir], { cwd, stdio: 'inherit' });
161164

162-
const tarballs = readdirSync(packDir).filter((file) => file.endsWith('.tgz'));
163-
const [tarball] = tarballs;
164-
if (!tarball) throw new Error(`Could not find packed tarball in: ${packDir}`);
165+
const tarballs = readdirSync(packDir)
166+
.filter((file) => file.endsWith('.tgz'))
167+
.sort();
165168

166-
const tarballPath = join(packDir, tarball);
169+
if (tarballs.length !== 1) {
170+
throw new Error(
171+
`Expected exactly 1 packed tarball in: ${packDir} (found ${
172+
tarballs.length
173+
}): ${tarballs.join(', ')}`
174+
);
175+
}
176+
177+
const tarballPath = join(packDir, tarballs[0]);
167178
const hasOidcEnv =
168179
!!process.env.ACTIONS_ID_TOKEN_REQUEST_URL && !!process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN;
169180
const provenanceArgs = hasOidcEnv ? ['--provenance'] : [];
@@ -172,7 +183,15 @@ const publish = async (cwd: string) => {
172183

173184
await execa(
174185
'pnpm',
175-
['dlx', NPM_CLI_SPEC, 'publish', '--no-git-checks', ...provenanceArgs, tarballPath],
186+
[
187+
'dlx',
188+
NPM_CLI_SPEC,
189+
'publish',
190+
'--no-git-checks',
191+
`--registry=${registry}`,
192+
...provenanceArgs,
193+
tarballPath
194+
],
176195
{ cwd, stdio: 'inherit' }
177196
);
178197
} finally {
@@ -350,7 +369,7 @@ const updatePackage = async (cwd: string, pkg: RepoPackage, version: string) =>
350369
await commitChanges(cwd, shortName, newVersion);
351370
// Note: We want to pull here in case there's an error, so nothing gets published
352371
await pull();
353-
await publish(cwd);
372+
await publish(cwd, registryOverride || DEFAULT_NPM_REGISTRY);
354373
await tag(cwd, shortName, newVersion);
355374
await push();
356375
} catch (e) {

0 commit comments

Comments
 (0)