|
| 1 | +import { |
| 2 | + ECRClient, |
| 3 | + CreateRepositoryCommand, |
| 4 | + DescribeRepositoriesCommand, |
| 5 | + type Repository, |
| 6 | + type Tag, |
| 7 | + RepositoryNotFoundException, |
| 8 | +} from "@aws-sdk/client-ecr"; |
| 9 | +import { tryCatch } from "@trigger.dev/core"; |
| 10 | +import { logger } from "~/services/logger.server"; |
| 11 | + |
| 12 | +export async function getDeploymentImageRef({ |
| 13 | + host, |
| 14 | + namespace, |
| 15 | + projectRef, |
| 16 | + nextVersion, |
| 17 | + environmentSlug, |
| 18 | + registryId, |
| 19 | + registryTags, |
| 20 | +}: { |
| 21 | + host: string; |
| 22 | + namespace: string; |
| 23 | + projectRef: string; |
| 24 | + nextVersion: string; |
| 25 | + environmentSlug: string; |
| 26 | + registryId?: string; |
| 27 | + registryTags?: string; |
| 28 | +}): Promise<{ |
| 29 | + imageRef: string; |
| 30 | + isEcr: boolean; |
| 31 | +}> { |
| 32 | + const repositoryName = `${namespace}/${projectRef}`; |
| 33 | + const imageRef = `${host}/${repositoryName}:${nextVersion}.${environmentSlug}`; |
| 34 | + |
| 35 | + if (!isEcrRegistry(host)) { |
| 36 | + return { |
| 37 | + imageRef, |
| 38 | + isEcr: false, |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + const [ecrRepoError] = await tryCatch( |
| 43 | + ensureEcrRepositoryExists({ repositoryName, registryHost: host, registryId, registryTags }) |
| 44 | + ); |
| 45 | + |
| 46 | + if (ecrRepoError) { |
| 47 | + logger.error("Failed to ensure ECR repository exists", { |
| 48 | + repositoryName, |
| 49 | + host, |
| 50 | + ecrRepoError: ecrRepoError.message, |
| 51 | + }); |
| 52 | + throw ecrRepoError; |
| 53 | + } |
| 54 | + |
| 55 | + return { |
| 56 | + imageRef, |
| 57 | + isEcr: true, |
| 58 | + }; |
| 59 | +} |
| 60 | + |
| 61 | +function isEcrRegistry(registryHost: string) { |
| 62 | + return registryHost.includes("amazonaws.com"); |
| 63 | +} |
| 64 | + |
| 65 | +function parseRegistryTags(tags: string): Tag[] { |
| 66 | + return tags.split(",").map((tag) => { |
| 67 | + const [key, value] = tag.split("="); |
| 68 | + return { Key: key, Value: value }; |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +async function createEcrRepository({ |
| 73 | + repositoryName, |
| 74 | + region, |
| 75 | + registryId, |
| 76 | + registryTags, |
| 77 | +}: { |
| 78 | + repositoryName: string; |
| 79 | + region: string; |
| 80 | + registryId?: string; |
| 81 | + registryTags?: string; |
| 82 | +}): Promise<Repository> { |
| 83 | + const ecr = new ECRClient({ region }); |
| 84 | + |
| 85 | + const result = await ecr.send( |
| 86 | + new CreateRepositoryCommand({ |
| 87 | + repositoryName, |
| 88 | + imageTagMutability: "IMMUTABLE", |
| 89 | + encryptionConfiguration: { |
| 90 | + encryptionType: "AES256", |
| 91 | + }, |
| 92 | + registryId, |
| 93 | + tags: registryTags ? parseRegistryTags(registryTags) : undefined, |
| 94 | + }) |
| 95 | + ); |
| 96 | + |
| 97 | + if (!result.repository) { |
| 98 | + logger.error("Failed to create ECR repository", { repositoryName, result }); |
| 99 | + throw new Error(`Failed to create ECR repository: ${repositoryName}`); |
| 100 | + } |
| 101 | + |
| 102 | + return result.repository; |
| 103 | +} |
| 104 | + |
| 105 | +async function getEcrRepository({ |
| 106 | + repositoryName, |
| 107 | + region, |
| 108 | + registryId, |
| 109 | +}: { |
| 110 | + repositoryName: string; |
| 111 | + region: string; |
| 112 | + registryId?: string; |
| 113 | +}): Promise<Repository | undefined> { |
| 114 | + const ecr = new ECRClient({ region }); |
| 115 | + |
| 116 | + try { |
| 117 | + const result = await ecr.send( |
| 118 | + new DescribeRepositoriesCommand({ |
| 119 | + repositoryNames: [repositoryName], |
| 120 | + registryId, |
| 121 | + }) |
| 122 | + ); |
| 123 | + |
| 124 | + if (!result.repositories || result.repositories.length === 0) { |
| 125 | + logger.debug("ECR repository not found", { repositoryName, region, result }); |
| 126 | + return undefined; |
| 127 | + } |
| 128 | + |
| 129 | + return result.repositories[0]; |
| 130 | + } catch (error) { |
| 131 | + if (error instanceof RepositoryNotFoundException) { |
| 132 | + logger.debug("ECR repository not found: RepositoryNotFoundException", { |
| 133 | + repositoryName, |
| 134 | + region, |
| 135 | + }); |
| 136 | + return undefined; |
| 137 | + } |
| 138 | + throw error; |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +export function getEcrRegion(registryHost: string): string | undefined { |
| 143 | + const parts = registryHost.split("."); |
| 144 | + if (parts.length !== 6 || parts[1] !== "dkr" || parts[2] !== "ecr") { |
| 145 | + return undefined; |
| 146 | + } |
| 147 | + return parts[3]; |
| 148 | +} |
| 149 | + |
| 150 | +async function ensureEcrRepositoryExists({ |
| 151 | + repositoryName, |
| 152 | + registryHost, |
| 153 | + registryId, |
| 154 | + registryTags, |
| 155 | +}: { |
| 156 | + repositoryName: string; |
| 157 | + registryHost: string; |
| 158 | + registryId?: string; |
| 159 | + registryTags?: string; |
| 160 | +}): Promise<Repository> { |
| 161 | + const region = getEcrRegion(registryHost); |
| 162 | + |
| 163 | + if (!region) { |
| 164 | + throw new Error(`Invalid ECR registry host: ${registryHost}`); |
| 165 | + } |
| 166 | + |
| 167 | + const [getRepoError, existingRepo] = await tryCatch( |
| 168 | + getEcrRepository({ repositoryName, region, registryId }) |
| 169 | + ); |
| 170 | + |
| 171 | + if (getRepoError) { |
| 172 | + logger.error("Failed to get ECR repository", { repositoryName, region, getRepoError }); |
| 173 | + throw getRepoError; |
| 174 | + } |
| 175 | + |
| 176 | + if (existingRepo) { |
| 177 | + logger.debug("ECR repository already exists", { repositoryName, region, existingRepo }); |
| 178 | + return existingRepo; |
| 179 | + } |
| 180 | + |
| 181 | + const [createRepoError, newRepo] = await tryCatch( |
| 182 | + createEcrRepository({ repositoryName, region, registryId, registryTags }) |
| 183 | + ); |
| 184 | + |
| 185 | + if (createRepoError) { |
| 186 | + logger.error("Failed to create ECR repository", { repositoryName, region, createRepoError }); |
| 187 | + throw createRepoError; |
| 188 | + } |
| 189 | + |
| 190 | + if (newRepo.repositoryName !== repositoryName) { |
| 191 | + logger.error("ECR repository name mismatch", { repositoryName, region, newRepo }); |
| 192 | + throw new Error( |
| 193 | + `ECR repository name mismatch: ${repositoryName} !== ${newRepo.repositoryName}` |
| 194 | + ); |
| 195 | + } |
| 196 | + |
| 197 | + return newRepo; |
| 198 | +} |
0 commit comments