Skip to content

Commit 01797a1

Browse files
authored
feat(deployments): ECR repo adaptations to enable external build cache (#2684)
* Update aws sdk ecr client to the latest version * Exlude the cache tag from the immutability enforcement * Attach a policy to ECR repos to expire untagged images * Fix filterType
1 parent 19fa669 commit 01797a1

File tree

3 files changed

+1786
-441
lines changed

3 files changed

+1786
-441
lines changed

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

Lines changed: 33 additions & 1 deletion
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";
@@ -213,7 +214,14 @@ async function createEcrRepository({
213214
const result = await ecr.send(
214215
new CreateRepositoryCommand({
215216
repositoryName,
216-
imageTagMutability: "IMMUTABLE",
217+
imageTagMutability: "IMMUTABLE_WITH_EXCLUSION",
218+
imageTagMutabilityExclusionFilters: [
219+
{
220+
// only the `cache` tag will be mutable, all other tags will be immutable
221+
filter: "cache",
222+
filterType: "WILDCARD",
223+
},
224+
],
217225
encryptionConfiguration: {
218226
encryptionType: "AES256",
219227
},
@@ -227,6 +235,30 @@ async function createEcrRepository({
227235
throw new Error(`Failed to create ECR repository: ${repositoryName}`);
228236
}
229237

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+
230262
return result.repository;
231263
}
232264

apps/webapp/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"@ai-sdk/openai": "^1.3.23",
3030
"@ariakit/react": "^0.4.6",
3131
"@ariakit/react-core": "^0.4.6",
32-
"@aws-sdk/client-ecr": "^3.839.0",
32+
"@aws-sdk/client-ecr": "^3.931.0",
3333
"@aws-sdk/client-sqs": "^3.445.0",
3434
"@aws-sdk/client-sts": "^3.840.0",
3535
"@better-auth/utils": "^0.2.6",

0 commit comments

Comments
 (0)