fix(ci): avoid duplicate agent latest assets
This commit is contained in:
@@ -23,8 +23,14 @@ async function main() {
|
||||
return;
|
||||
}
|
||||
|
||||
const localAssetPaths = localAssets(args.assetsDir);
|
||||
const localAssetNames = new Set(localAssetPaths.map((assetPath) => basename(assetPath)));
|
||||
if (args.pruneAssets) {
|
||||
await pruneReleaseAssets(client, release, localAssetNames);
|
||||
}
|
||||
|
||||
const existingAssets = new Set((release.assets || []).map((asset) => asset.name));
|
||||
const assets = localAssets(args.assetsDir).filter((assetPath) => {
|
||||
const assets = localAssetPaths.filter((assetPath) => {
|
||||
const name = basename(assetPath);
|
||||
if (existingAssets.has(name) && !args.overwriteExisting) {
|
||||
console.log(`Skipping existing CNB asset: ${name}`);
|
||||
@@ -49,6 +55,7 @@ function parseArgs(argv) {
|
||||
githubReleasePath: "",
|
||||
assetsDir: "",
|
||||
metadataOnly: false,
|
||||
pruneAssets: false,
|
||||
};
|
||||
for (let index = 0; index < argv.length; index++) {
|
||||
const arg = argv[index];
|
||||
@@ -56,12 +63,13 @@ function parseArgs(argv) {
|
||||
else if (arg === "--assets-dir") args.assetsDir = argv[++index];
|
||||
else if (arg === "--metadata-only") args.metadataOnly = true;
|
||||
else if (arg === "--overwrite-existing") args.overwriteExisting = true;
|
||||
else if (arg === "--prune-assets") args.pruneAssets = true;
|
||||
else throw new Error(`Unknown argument: ${arg}`);
|
||||
}
|
||||
if (!args.token) throw new Error("CNB_TOKEN is required.");
|
||||
if (!args.githubReleasePath || (!args.metadataOnly && !args.assetsDir)) {
|
||||
throw new Error(
|
||||
"Usage: sync-cnb-release.mjs --github-release <release.json> (--assets-dir <dir> | --metadata-only)",
|
||||
"Usage: sync-cnb-release.mjs --github-release <release.json> (--assets-dir <dir> | --metadata-only) [--prune-assets]",
|
||||
);
|
||||
}
|
||||
if (!Number.isInteger(args.concurrency) || args.concurrency < 1) {
|
||||
@@ -70,6 +78,16 @@ function parseArgs(argv) {
|
||||
return args;
|
||||
}
|
||||
|
||||
export async function pruneReleaseAssets(client, release, localAssetNames) {
|
||||
for (const asset of release.assets || []) {
|
||||
if (localAssetNames.has(asset.name)) continue;
|
||||
if (!asset.id) throw new Error(`CNB asset ${asset.name} is missing its id.`);
|
||||
// Mutable release aliases must not retain files removed from the source release.
|
||||
await client.deleteAsset(release.id, asset.id);
|
||||
console.log(`Deleted stale CNB asset: ${asset.name}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function uploadWithRetry(client, releaseId, filePath, overwriteExisting) {
|
||||
const name = basename(filePath);
|
||||
for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
|
||||
@@ -139,6 +157,13 @@ export class CnbClient {
|
||||
}
|
||||
}
|
||||
|
||||
async deleteAsset(releaseId, assetId) {
|
||||
await this.request(
|
||||
"DELETE",
|
||||
`/${this.repository}/-/releases/${encodeURIComponent(releaseId)}/assets/${encodeURIComponent(assetId)}`,
|
||||
);
|
||||
}
|
||||
|
||||
async request(method, path, body = null, allow404 = false) {
|
||||
const response = await fetch(`${this.apiBase}${path}`, {
|
||||
method,
|
||||
|
||||
@@ -7,7 +7,7 @@ import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import test from "node:test";
|
||||
|
||||
import { CnbClient } from "./sync-cnb-release.mjs";
|
||||
import { CnbClient, pruneReleaseAssets } from "./sync-cnb-release.mjs";
|
||||
|
||||
const scriptPath = fileURLToPath(new URL("./sync-cnb-release.mjs", import.meta.url));
|
||||
|
||||
@@ -125,3 +125,59 @@ test("metadata-only sync updates release without reading an assets directory", a
|
||||
{ method: "PATCH", url: "/dbxio.com/dbx/-/releases/release-2" },
|
||||
]);
|
||||
});
|
||||
|
||||
test("pruneReleaseAssets deletes files missing from the source release", async () => {
|
||||
const deleted = [];
|
||||
const client = {
|
||||
async deleteAsset(releaseId, assetId) {
|
||||
deleted.push({ releaseId, assetId });
|
||||
},
|
||||
};
|
||||
|
||||
await pruneReleaseAssets(
|
||||
client,
|
||||
{
|
||||
id: "release-latest",
|
||||
assets: [
|
||||
{ id: "registry", name: "agent-registry.json" },
|
||||
{ id: "old-jre", name: "dbx-jre-8-linux-x64.tar.gz" },
|
||||
{ id: "old-driver", name: "dbx-agent-h2-0.1.0.jar" },
|
||||
],
|
||||
},
|
||||
new Set(["agent-registry.json"]),
|
||||
);
|
||||
|
||||
assert.deepEqual(deleted, [
|
||||
{ releaseId: "release-latest", assetId: "old-jre" },
|
||||
{ releaseId: "release-latest", assetId: "old-driver" },
|
||||
]);
|
||||
});
|
||||
|
||||
test("deleteAsset accepts CNB's empty successful response", async (t) => {
|
||||
const requests = [];
|
||||
const server = createServer((request, response) => {
|
||||
requests.push({ method: request.method, url: request.url });
|
||||
response.statusCode = 204;
|
||||
response.end();
|
||||
});
|
||||
|
||||
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
||||
t.after(() => new Promise((resolve, reject) => server.close((error) => (error ? reject(error) : resolve()))));
|
||||
|
||||
const address = server.address();
|
||||
assert.ok(address && typeof address !== "string");
|
||||
const client = new CnbClient({
|
||||
apiBase: `http://127.0.0.1:${address.port}`,
|
||||
repository: "dbxio.com/dbx",
|
||||
token: "test-token",
|
||||
});
|
||||
|
||||
await client.deleteAsset("release/latest", "asset old");
|
||||
|
||||
assert.deepEqual(requests, [
|
||||
{
|
||||
method: "DELETE",
|
||||
url: "/dbxio.com/dbx/-/releases/release%2Flatest/assets/asset%20old",
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user