Skip to content

Commit 8082c28

Browse files
committed
refactor(release): pass tarball directly to finalization
1 parent 36225f8 commit 8082c28

9 files changed

Lines changed: 113 additions & 209 deletions

File tree

‎.github/workflows/release.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,11 @@ It checks the exact version on every attempt, restores the original registry tar
386386
when present, skips npm publication, and returns its local path for assets so the
387387
remaining release steps can resume. Registry and integrity errors still fail the job.
388388
389+
Pass that `package-tarball-path` directly to `release/finalize`, with the desired
390+
`.tgz` filename as `asset-name`. Finalization checks the existing asset's digest
391+
and stages the file only if it is missing. Callers do not need to copy the tarball
392+
into a predefined directory.
393+
389394
npm publication across several packages cannot be atomic. Check exact package
390395
versions before retrying; published versions cannot be overwritten. Restore original
391396
published bytes for release assets, verify integrity and existing asset digests, and

‎.github/workflows/release.yml‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,8 @@ jobs:
8080
version: ${{ fromJSON(inputs.packages)[0].version }}
8181
recover-existing: "true"
8282
tag: latest
83-
- name: Prepare release asset
84-
env:
85-
PACKAGE_TARBALL: ${{ steps.publish.outputs.package-tarball-path }}
86-
ASSET_NAME: ${{ fromJSON(inputs.packages)[0].slug }}-${{ fromJSON(inputs.packages)[0].version }}.tgz
87-
run: |
88-
mkdir -p "$RUNNER_TEMP/release-assets"
89-
cp "$PACKAGE_TARBALL" "$RUNNER_TEMP/release-assets/$ASSET_NAME"
9083
- uses: $/actions/release/finalize
9184
with:
9285
tag: ${{ github.ref_name }}
86+
package-tarball-path: ${{ steps.publish.outputs.package-tarball-path }}
87+
asset-name: ${{ fromJSON(inputs.packages)[0].slug }}-${{ fromJSON(inputs.packages)[0].version }}.tgz

‎actions/release/finalize/README.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@
2222

2323
## Overview
2424

25-
Verify published npm tarballs staged in RUNNER_TEMP/release-assets against existing assets, then upload missing assets and publish the draft with HoverKraft release/update using GITHUB_TOKEN.
25+
Verify a published npm tarball against the named release asset, then stage it only if missing and publish the draft with HoverKraft release/update using GITHUB_TOKEN.
2626

2727
<!-- overview:end -->
2828
<!-- inputs:start -->
2929

3030
## Inputs
3131

32-
| **Input** | **Description** | **Required** | **Default** |
33-
| --------- | ---------------------- | ------------ | ----------- |
34-
| **`tag`** | Committed release tag. | **true** | - |
32+
| **Input** | **Description** | **Required** | **Default** |
33+
| -------------------------- | ---------------------------------------------------------------------------------------------------------- | ------------ | ----------- |
34+
| **`tag`** | Committed release tag. | **true** | - |
35+
| **`package-tarball-path`** | Local tarball path returned by package/publish, including the original npm bytes restored during recovery. | **true** | - |
36+
| **`asset-name`** | Plain .tgz filename for the GitHub release asset. | **true** | - |
3537

3638
<!-- inputs:end -->
3739
<!-- secrets:start -->

‎actions/release/finalize/action.yml‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: Finalize Node.js release
2-
description: Verify published npm tarballs staged in RUNNER_TEMP/release-assets against existing assets, then upload missing assets and publish the draft with HoverKraft release/update using GITHUB_TOKEN.
2+
description: Verify a published npm tarball against the named release asset, then stage it only if missing and publish the draft with HoverKraft release/update using GITHUB_TOKEN.
33
author: hoverkraft
44
branding:
55
icon: tag
@@ -8,6 +8,12 @@ inputs:
88
tag:
99
description: Committed release tag.
1010
required: true
11+
package-tarball-path:
12+
description: Local tarball path returned by package/publish, including the original npm bytes restored during recovery.
13+
required: true
14+
asset-name:
15+
description: Plain .tgz filename for the GitHub release asset.
16+
required: true
1117
runs:
1218
using: composite
1319
steps:
@@ -16,11 +22,17 @@ runs:
1622
env:
1723
ACTION_PATH: ${{ github.action_path }}
1824
RELEASE_TAG: ${{ inputs.tag }}
19-
ASSET_DIRECTORY: ${{ runner.temp }}/release-assets
25+
PACKAGE_TARBALL_PATH: ${{ inputs.package-tarball-path }}
26+
ASSET_NAME: ${{ inputs.asset-name }}
27+
RUNNER_TEMP: ${{ runner.temp }}
2028
with:
2129
script: |
2230
const releaseAssets = require(`${process.env.ACTION_PATH}/index.js`);
23-
const { release, pendingDirectory, hasAssets } = await releaseAssets.prepareAssets({ github, glob, repo: context.repo, tag: process.env.RELEASE_TAG, directory: process.env.ASSET_DIRECTORY });
31+
const { release, pendingDirectory, hasAssets } = await releaseAssets.prepareAssets({
32+
github, repo: context.repo, tag: process.env.RELEASE_TAG,
33+
tarballPath: process.env.PACKAGE_TARBALL_PATH, assetName: process.env.ASSET_NAME,
34+
tempDirectory: process.env.RUNNER_TEMP,
35+
});
2436
core.setOutput('release-id', release.id);
2537
core.setOutput('published', String(!release.draft));
2638
core.setOutput('pending-directory', pendingDirectory);

‎actions/release/finalize/index.js‎

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,47 @@ async function findRelease(github, repo, tag) {
1919
}
2020
}
2121

22-
async function prepareAssets({ github, glob, repo, tag, directory }) {
22+
async function prepareAssets({
23+
github,
24+
repo,
25+
tag,
26+
tarballPath,
27+
assetName,
28+
tempDirectory,
29+
}) {
30+
if (
31+
typeof assetName !== "string" ||
32+
!/^[A-Za-z0-9][A-Za-z0-9._-]*\.tgz$/.test(assetName)
33+
)
34+
throw new Error("asset-name must be a plain .tgz filename.");
35+
if (!fs.lstatSync(tarballPath).isFile())
36+
throw new Error("package-tarball-path must point to a regular file.");
2337
const release = await findRelease(github, repo, tag);
2438
if (!release) throw new Error(`No release found for ${tag}.`);
2539
const assets = await github.paginate(github.rest.repos.listReleaseAssets, {
2640
...repo,
2741
release_id: release.id,
2842
per_page: 100,
2943
});
30-
const globber = await glob.create(path.join(directory, "*.tgz"), {
31-
followSymbolicLinks: false,
32-
matchDirectories: false,
33-
implicitDescendants: false,
34-
});
35-
const files = (await globber.glob()).filter((file) =>
36-
fs.lstatSync(file).isFile(),
37-
);
38-
if (!files.length)
39-
throw new Error("No package tarballs available to attach.");
40-
const missing = [];
41-
for (const file of files) {
42-
const name = path.basename(file);
43-
const data = fs.readFileSync(file);
44-
const digest = `sha256:${createHash("sha256").update(data).digest("hex")}`;
45-
const existing = assets.find((asset) => asset.name === name);
46-
if (existing) {
47-
if (existing.digest !== digest)
48-
throw new Error(
49-
`Release asset ${name} has a different or unavailable digest.`,
50-
);
51-
continue;
52-
}
53-
if (!release.draft)
54-
throw new Error(`Published release is missing expected asset ${name}.`);
55-
missing.push({ name, data });
44+
const data = fs.readFileSync(tarballPath);
45+
const digest = `sha256:${createHash("sha256").update(data).digest("hex")}`;
46+
const existing = assets.find((asset) => asset.name === assetName);
47+
if (existing) {
48+
if (existing.digest !== digest)
49+
throw new Error(
50+
`Release asset ${assetName} has a different or unavailable digest.`,
51+
);
52+
return { release, pendingDirectory: "", hasAssets: false };
5653
}
57-
const pendingDirectory = fs.mkdtempSync(path.join(directory, "pending-"));
58-
for (const { name, data } of missing)
59-
fs.writeFileSync(path.join(pendingDirectory, name), data);
60-
return { release, pendingDirectory, hasAssets: missing.length > 0 };
54+
if (!release.draft)
55+
throw new Error(
56+
`Published release is missing expected asset ${assetName}.`,
57+
);
58+
const pendingDirectory = fs.mkdtempSync(
59+
path.join(tempDirectory, "release-pending-"),
60+
);
61+
fs.writeFileSync(path.join(pendingDirectory, assetName), data);
62+
return { release, pendingDirectory, hasAssets: true };
6163
}
6264

6365
module.exports = { prepareAssets };

‎actions/release/finalize/index.test.js‎

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const assert = require("node:assert/strict");
2-
const glob = require("@actions/glob");
32
const { test } = require("node:test");
43
const { createHash } = require("node:crypto");
54
const fs = require("node:fs");
@@ -15,7 +14,8 @@ function directory(t) {
1514
function assetsFixture(t) {
1615
const root = directory(t);
1716
const data = Buffer.from("published package");
18-
fs.writeFileSync(path.join(root, "example-core-1.2.0.tgz"), data);
17+
const tarballPath = path.join(root, "downloaded-artifact.tgz");
18+
fs.writeFileSync(tarballPath, data);
1919
const release = { id: 1, draft: true };
2020
const assets = [];
2121
const github = {
@@ -32,15 +32,16 @@ function assetsFixture(t) {
3232
data,
3333
args: {
3434
github,
35-
glob,
3635
repo: { owner: "example", repo: "project" },
3736
tag: "v1.2.0",
38-
directory: root,
37+
tarballPath,
38+
assetName: "example-core-1.2.0.tgz",
39+
tempDirectory: root,
3940
},
4041
};
4142
}
4243

43-
test("stages only missing package bytes for HoverKraft upload and skips identical assets", async (t) => {
44+
test("stages the explicit tarball under its release name and skips identical assets", async (t) => {
4445
const f = assetsFixture(t);
4546
const result = await releaseAssets.prepareAssets(f.args);
4647
assert.equal(result.hasAssets, true);
@@ -50,13 +51,15 @@ test("stages only missing package bytes for HoverKraft upload and skips identica
5051
),
5152
f.data,
5253
);
54+
assert.deepEqual(fs.readdirSync(result.pendingDirectory), [f.args.assetName]);
55+
assert.deepEqual(fs.readFileSync(f.args.tarballPath), f.data);
5356
f.assets.push({
5457
name: "example-core-1.2.0.tgz",
5558
digest: `sha256:${createHash("sha256").update(f.data).digest("hex")}`,
5659
});
5760
const retry = await releaseAssets.prepareAssets(f.args);
5861
assert.equal(retry.hasAssets, false);
59-
assert.deepEqual(fs.readdirSync(retry.pendingDirectory), []);
62+
assert.equal(retry.pendingDirectory, "");
6063
f.release.draft = false;
6164
assert.equal((await releaseAssets.prepareAssets(f.args)).hasAssets, false);
6265
});
@@ -76,42 +79,50 @@ test("does not overwrite mismatched assets or modify an incomplete published rel
7679
);
7780
});
7881

79-
test("partial asset recovery passes only missing tarballs to the uploader", async (t) => {
82+
test("attaches only the requested file even when other tarballs are present", async (t) => {
8083
const f = assetsFixture(t);
81-
const name = "example-cli-1.2.0.tgz";
82-
fs.writeFileSync(path.join(f.args.directory, name), "second package");
83-
f.assets.push({
84-
name: "example-core-1.2.0.tgz",
85-
digest: `sha256:${createHash("sha256").update(f.data).digest("hex")}`,
86-
});
87-
const result = await releaseAssets.prepareAssets(f.args);
88-
assert.equal(result.hasAssets, true);
89-
assert.deepEqual(fs.readdirSync(result.pendingDirectory), [name]);
90-
assert.equal(
91-
fs.readFileSync(path.join(result.pendingDirectory, name), "utf8"),
92-
"second package",
84+
fs.writeFileSync(
85+
path.join(f.args.tempDirectory, "unrelated.tgz"),
86+
"other package",
9387
);
88+
const result = await releaseAssets.prepareAssets(f.args);
89+
assert.deepEqual(fs.readdirSync(result.pendingDirectory), [f.args.assetName]);
9490
});
9591

96-
test("asset discovery excludes nested tarballs, directories and symbolic links", async (t) => {
92+
test("rejects asset names that could escape staging or are not tarball filenames", async (t) => {
9793
const f = assetsFixture(t);
98-
const nested = path.join(f.args.directory, "previous-attempt");
99-
fs.mkdirSync(nested);
100-
fs.writeFileSync(path.join(nested, "ignored.tgz"), "previous attempt");
101-
fs.mkdirSync(path.join(f.args.directory, "directory.tgz"));
102-
fs.symlinkSync(
103-
path.join(nested, "ignored.tgz"),
104-
path.join(f.args.directory, "link.tgz"),
105-
);
106-
const result = await releaseAssets.prepareAssets(f.args);
107-
assert.deepEqual(fs.readdirSync(result.pendingDirectory), [
108-
"example-core-1.2.0.tgz",
94+
for (const assetName of [
95+
"",
96+
"../outside.tgz",
97+
"/outside.tgz",
98+
"dir\\outside.tgz",
99+
"package.zip",
100+
"bad\nname.tgz",
101+
]) {
102+
await assert.rejects(
103+
releaseAssets.prepareAssets({ ...f.args, assetName }),
104+
/asset-name must be a plain .tgz filename/,
105+
);
106+
}
107+
assert.deepEqual(fs.readdirSync(f.args.tempDirectory), [
108+
"downloaded-artifact.tgz",
109109
]);
110-
fs.unlinkSync(path.join(f.args.directory, "example-core-1.2.0.tgz"));
111-
await assert.rejects(
112-
releaseAssets.prepareAssets(f.args),
113-
/No package tarballs/,
114-
);
110+
});
111+
112+
test("rejects missing tarballs, directories and symbolic links", async (t) => {
113+
const f = assetsFixture(t);
114+
const folder = path.join(f.args.tempDirectory, "directory.tgz");
115+
const link = path.join(f.args.tempDirectory, "link.tgz");
116+
fs.mkdirSync(folder);
117+
fs.symlinkSync(f.args.tarballPath, link);
118+
for (const tarballPath of [folder, link]) {
119+
await assert.rejects(
120+
releaseAssets.prepareAssets({ ...f.args, tarballPath }),
121+
/package-tarball-path must point to a regular file/,
122+
);
123+
}
124+
fs.unlinkSync(f.args.tarballPath);
125+
await assert.rejects(releaseAssets.prepareAssets(f.args), /ENOENT/);
115126
});
116127

117128
test("recovers draft assets through release listing and propagates lookup failures", async (t) => {

0 commit comments

Comments
 (0)