Skip to content

Commit 3e2c17a

Browse files
committed
Attach a policy to ECR repos to expire untagged images
1 parent c0e8db8 commit 3e2c17a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

apps/webapp/app/v3/getDeploymentImageRef.server.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
type Tag,
77
RepositoryNotFoundException,
88
GetAuthorizationTokenCommand,
9+
PutLifecyclePolicyCommand,
910
} from "@aws-sdk/client-ecr";
1011
import { STSClient, AssumeRoleCommand } from "@aws-sdk/client-sts";
1112
import { tryCatch } from "@trigger.dev/core";
@@ -234,6 +235,30 @@ async function createEcrRepository({
234235
throw new Error(`Failed to create ECR repository: ${repositoryName}`);
235236
}
236237

238+
// When the `cache` tag is mutated, the old cache images are untagged.
239+
// This policy matches those images and expires them to avoid bloating the repository.
240+
await ecr.send(
241+
new PutLifecyclePolicyCommand({
242+
repositoryName: result.repository.repositoryName,
243+
registryId: result.repository.registryId,
244+
lifecyclePolicyText: JSON.stringify({
245+
rules: [
246+
{
247+
rulePriority: 1,
248+
description: "Expire untagged images older than 3 days",
249+
selection: {
250+
tagStatus: "untagged",
251+
countType: "sinceImagePushed",
252+
countUnit: "days",
253+
countNumber: 3,
254+
},
255+
action: { type: "expire" },
256+
},
257+
],
258+
}),
259+
})
260+
);
261+
237262
return result.repository;
238263
}
239264

0 commit comments

Comments
 (0)