Files
probably-rendering-one-more…/apps/waterfall-wiki/scripts/cleanup-ci-bucket.mjs
T
codex-bot 6654a1456b
Unified application CI and deployment / verify-and-release (pull_request) Waiting to run
python CI and deployment / verify-and-deploy (pull_request) Successful in 1m12s
Unify Wiki and Studio application and simplify deployment
2026-11-04 03:41:16 +08:00

19 lines
1.1 KiB
JavaScript

// Only invoked inside the dedicated test network with this run's bucket name.
import {S3Client,ListObjectsV2Command,DeleteObjectsCommand,DeleteBucketCommand} from '@aws-sdk/client-s3';
const bucket=process.env.S3_BUCKET;
if (!/^ci-[1-9][0-9]*$/.test(bucket || '')) throw Error('Refusing non-CI bucket');
const client=new S3Client({endpoint:'http://objects:9000',region:'us-east-1',forcePathStyle:true,credentials:{accessKeyId:process.env.S3_ACCESS_KEY,secretAccessKey:process.env.S3_SECRET_KEY}});
try {
// Re-list the first page after each deletion so no pagination token can skip keys.
for (;;) {
const result=await client.send(new ListObjectsV2Command({Bucket:bucket,MaxKeys:1000}));
const keys=(result.Contents || []).map(({Key})=>({Key}));
if (!keys.length) break;
const deleted=await client.send(new DeleteObjectsCommand({Bucket:bucket,Delete:{Objects:keys,Quiet:true}}));
if (deleted.Errors?.length) throw Error('CI object cleanup failed');
}
await client.send(new DeleteBucketCommand({Bucket:bucket}));
} catch (error) {
if (error.name !== 'NoSuchBucket') throw error;
}