The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as clouddeploy from "@distilled.cloud/gcp/clouddeploy_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import { tagRecord } from "alchemy/Tags"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | fieldMask, |
| 14 | fingerprint, |
| 15 | listAtLocation, |
| 16 | listLabeledPages, |
| 17 | normalizeLocation, |
| 18 | parentOf, |
| 19 | parseName, |
| 20 | replaceOnIdentity, |
| 21 | ResourceNotResolved, |
| 22 | sameBool, |
| 23 | sameText, |
| 24 | stringMap, |
| 25 | toPhysicalId, |
| 26 | userLabels, |
| 27 | waitForOperation, |
| 28 | waitUntilExists, |
| 29 | waitUntilGone, |
| 30 | } from "./internal.ts"; |
| 31 | |
| 32 | export type Stage = { |
| 33 | /** |
| 34 | * Target id this stage points at (the last segment of a Target name). |
| 35 | */ |
| 36 | targetId?: string; |
| 37 | /** |
| 38 | * Skaffold profiles used when rendering this stage's Target. |
| 39 | */ |
| 40 | profiles?: string[]; |
| 41 | /** |
| 42 | * Rollout strategy for this stage (`standard` or `canary`). |
| 43 | */ |
| 44 | strategy?: clouddeploy.Strategy; |
| 45 | /** |
| 46 | * Deploy parameters applied to the target in this stage. |
| 47 | */ |
| 48 | deployParameters?: clouddeploy.DeployParameters[]; |
| 49 | }; |
| 50 | |
| 51 | export type SerialPipeline = { |
| 52 | /** |
| 53 | * Ordered stages. The list order is the promotion flow. |
| 54 | */ |
| 55 | stages?: Stage[]; |
| 56 | }; |
| 57 | |
| 58 | export type PipelineCondition = { |
| 59 | /** Whether referenced targets exist. */ |
| 60 | targetsPresentCondition?: clouddeploy.TargetsPresentCondition; |
| 61 | /** Whether the pipeline's target types are compatible. */ |
| 62 | targetsTypeCondition?: clouddeploy.TargetsTypeCondition; |
| 63 | /** Whether the pipeline is ready. */ |
| 64 | pipelineReadyCondition?: clouddeploy.PipelineReadyCondition; |
| 65 | }; |
| 66 | |
| 67 | export type DeliveryPipelineProps = { |
| 68 | /** |
| 69 | * Delivery pipeline id (the `{deliveryPipeline}` segment of |
| 70 | * `projects/{project}/locations/{location}/deliveryPipelines/{deliveryPipeline}`). |
| 71 | * If omitted, a unique RFC1035 name is generated from the stack, stage, |
| 72 | * and logical id. Immutable — changing it replaces the pipeline. |
| 73 | */ |
| 74 | deliveryPipelineId?: string; |
| 75 | /** |
| 76 | * Region (`us-central1`, `us-east1`, …). Immutable — changing it |
| 77 | * replaces the pipeline. `US-CENTRAL1` is accepted and normalized to |
| 78 | * `us-central1`. |
| 79 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 80 | */ |
| 81 | location?: string; |
| 82 | /** |
| 83 | * Sequential stages this pipeline promotes through. |
| 84 | */ |
| 85 | serialPipeline?: SerialPipeline; |
| 86 | /** |
| 87 | * Human-readable description. Max length 255 characters. |
| 88 | */ |
| 89 | description?: string; |
| 90 | /** |
| 91 | * When true, no new releases or rollouts can be created. In-progress |
| 92 | * ones still complete. |
| 93 | * @default false |
| 94 | */ |
| 95 | suspended?: boolean; |
| 96 | /** |
| 97 | * User annotations (not used by Cloud Deploy). |
| 98 | */ |
| 99 | annotations?: Record<string, string>; |
| 100 | /** |
| 101 | * User labels. Alchemy ownership labels are merged in automatically. |
| 102 | */ |
| 103 | labels?: Record<string, string>; |
| 104 | }; |
| 105 | |
| 106 | export type DeliveryPipeline = Resource< |
| 107 | "GCP.CloudDeploy.DeliveryPipeline", |
| 108 | DeliveryPipelineProps, |
| 109 | { |
| 110 | /** Full resource name. */ |
| 111 | name: string; |
| 112 | /** Delivery pipeline id (last path segment). */ |
| 113 | deliveryPipelineId: string; |
| 114 | /** Project id. */ |
| 115 | project: string; |
| 116 | /** Location id. */ |
| 117 | location: string; |
| 118 | /** Sequential pipeline configuration. */ |
| 119 | serialPipeline: SerialPipeline | undefined; |
| 120 | /** Human-readable description. */ |
| 121 | description: string | undefined; |
| 122 | /** Whether the pipeline is suspended. */ |
| 123 | suspended: boolean; |
| 124 | /** Server-reported pipeline condition. */ |
| 125 | condition: PipelineCondition | undefined; |
| 126 | /** User annotations. */ |
| 127 | annotations: Record<string, string>; |
| 128 | /** User labels (Alchemy ownership labels stripped). */ |
| 129 | labels: Record<string, string>; |
| 130 | /** Server-generated resource uid. */ |
| 131 | uid: string | undefined; |
| 132 | /** RFC3339 creation timestamp. */ |
| 133 | createTime: string | undefined; |
| 134 | /** RFC3339 last-update timestamp. */ |
| 135 | updateTime: string | undefined; |
| 136 | /** Server-computed etag. */ |
| 137 | etag: string | undefined; |
| 138 | }, |
| 139 | never, |
| 140 | Providers |
| 141 | >; |
| 142 | |
| 143 | /** |
| 144 | * A Cloud Deploy delivery pipeline — the ordered stages a Skaffold |
| 145 | * configuration progresses through. |
| 146 | * |
| 147 | * Changing `deliveryPipelineId` or `location` replaces the pipeline. |
| 148 | * `serialPipeline`, description, labels, annotations, and `suspended` |
| 149 | * update in place. |
| 150 | * |
| 151 | * ### Creating a Delivery Pipeline |
| 152 | * **Example:** Generated name |
| 153 | * ```typescript |
| 154 | * const pipeline = yield* GCP.CloudDeploy.DeliveryPipeline("App", { |
| 155 | * serialPipeline: { stages: [{ targetId: "prod" }] }, |
| 156 | * }); |
| 157 | * ``` |
| 158 | * |
| 159 | * **Example:** Explicit id and labels |
| 160 | * ```typescript |
| 161 | * const pipeline = yield* GCP.CloudDeploy.DeliveryPipeline("App", { |
| 162 | * deliveryPipelineId: "app-pipeline", |
| 163 | * serialPipeline: { |
| 164 | * stages: [ |
| 165 | * { targetId: "staging" }, |
| 166 | * { targetId: "prod" }, |
| 167 | * ], |
| 168 | * }, |
| 169 | * labels: { env: "prod" }, |
| 170 | * }); |
| 171 | * ``` |
| 172 | * |
| 173 | * ### Updating a Delivery Pipeline |
| 174 | * Re-declare the same logical id with changed props; the engine keeps the |
| 175 | * physical resource and updates it in place. |
| 176 | * |
| 177 | * **Example:** Description, suspend, and labels |
| 178 | * ```typescript |
| 179 | * const pipeline = yield* GCP.CloudDeploy.DeliveryPipeline("App", { |
| 180 | * serialPipeline: { stages: [{ targetId: "prod" }] }, |
| 181 | * description: "app pipeline v2", |
| 182 | * suspended: true, |
| 183 | * labels: { env: "prod", team: "platform" }, |
| 184 | * }); |
| 185 | * ``` |
| 186 | * |
| 187 | * @resource |
| 188 | * @category CloudDeploy |
| 189 | */ |
| 190 | export const DeliveryPipeline = Resource<DeliveryPipeline>("GCP.CloudDeploy.DeliveryPipeline"); |
| 191 | |
| 192 | const resourceName = (project: string, location: string, deliveryPipelineId: string) => |
| 193 | `projects/${project}/locations/${location}/deliveryPipelines/${deliveryPipelineId}`; |
| 194 | |
| 195 | const toSerialPipeline = ( |
| 196 | value: clouddeploy.SerialPipeline | undefined, |
| 197 | ): SerialPipeline | undefined => |
| 198 | value === undefined |
| 199 | ? undefined |
| 200 | : { |
| 201 | stages: value.stages?.map((stage) => ({ |
| 202 | targetId: stage.targetId, |
| 203 | profiles: stage.profiles, |
| 204 | strategy: stage.strategy, |
| 205 | deployParameters: stage.deployParameters, |
| 206 | })), |
| 207 | }; |
| 208 | |
| 209 | const toCondition = ( |
| 210 | value: clouddeploy.PipelineCondition | undefined, |
| 211 | ): PipelineCondition | undefined => |
| 212 | value === undefined |
| 213 | ? undefined |
| 214 | : { |
| 215 | targetsPresentCondition: value.targetsPresentCondition, |
| 216 | targetsTypeCondition: value.targetsTypeCondition, |
| 217 | pipelineReadyCondition: value.pipelineReadyCondition, |
| 218 | }; |
| 219 | |
| 220 | const toAttrs = (item: clouddeploy.DeliveryPipeline, project: string) => { |
| 221 | const name = item.name ?? ""; |
| 222 | const parsed = parseName(name, "deliveryPipelines"); |
| 223 | return { |
| 224 | name, |
| 225 | deliveryPipelineId: parsed.id, |
| 226 | project: parsed.project || project, |
| 227 | location: parsed.location, |
| 228 | serialPipeline: toSerialPipeline(item.serialPipeline), |
| 229 | description: item.description, |
| 230 | suspended: item.suspended === true, |
| 231 | condition: toCondition(item.condition), |
| 232 | annotations: stringMap(item.annotations), |
| 233 | labels: userLabels(item.labels), |
| 234 | uid: item.uid, |
| 235 | createTime: item.createTime, |
| 236 | updateTime: item.updateTime, |
| 237 | etag: item.etag, |
| 238 | }; |
| 239 | }; |
| 240 | |
| 241 | const getByName = (name: string) => |
| 242 | clouddeploy |
| 243 | .getProjectsLocationsDeliveryPipelines({ name }) |
| 244 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 245 | |
| 246 | const listOwned = (project: string, region: string) => |
| 247 | listAtLocation(project, region, (parent) => |
| 248 | listLabeledPages( |
| 249 | clouddeploy.listProjectsLocationsDeliveryPipelines.pages({ |
| 250 | parent, |
| 251 | pageSize: 1000, |
| 252 | }), |
| 253 | (page) => page.deliveryPipelines, |
| 254 | (item) => item.labels, |
| 255 | ), |
| 256 | ); |
| 257 | |
| 258 | export const DeliveryPipelineProvider = () => |
| 259 | Provider.succeed(DeliveryPipeline, { |
| 260 | stables: ["name", "deliveryPipelineId", "project", "location", "uid", "createTime"], |
| 261 | |
| 262 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 263 | if (!isResolved(news)) return undefined; |
| 264 | const env = yield* GcpEnvironment.current; |
| 265 | return replaceOnIdentity({ |
| 266 | previousId: olds?.deliveryPipelineId ?? output?.deliveryPipelineId, |
| 267 | nextId: news.deliveryPipelineId ?? olds?.deliveryPipelineId ?? output?.deliveryPipelineId, |
| 268 | previousLocation: normalizeLocation(olds?.location ?? output?.location ?? env.region), |
| 269 | nextLocation: normalizeLocation( |
| 270 | news.location ?? olds?.location ?? output?.location ?? env.region, |
| 271 | ), |
| 272 | }); |
| 273 | }), |
| 274 | |
| 275 | read: Effect.fn(function* ({ id, olds, output }) { |
| 276 | const env = yield* GcpEnvironment.current; |
| 277 | const deliveryPipelineId = yield* toPhysicalId( |
| 278 | id, |
| 279 | olds?.deliveryPipelineId, |
| 280 | output?.deliveryPipelineId, |
| 281 | "deliverypipeline", |
| 282 | ); |
| 283 | const location = normalizeLocation(olds?.location ?? output?.location ?? env.region); |
| 284 | const name = output?.name ?? resourceName(env.project, location, deliveryPipelineId); |
| 285 | const existing = yield* getByName(name); |
| 286 | if (existing === undefined) return undefined; |
| 287 | const attrs = toAttrs(existing, env.project); |
| 288 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 289 | }), |
| 290 | |
| 291 | list: () => |
| 292 | Effect.gen(function* () { |
| 293 | const env = yield* GcpEnvironment.current; |
| 294 | const items = yield* listOwned(env.project, env.region); |
| 295 | return items.map((item) => toAttrs(item, env.project)); |
| 296 | }), |
| 297 | |
| 298 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 299 | const env = yield* GcpEnvironment.current; |
| 300 | const deliveryPipelineId = yield* toPhysicalId( |
| 301 | id, |
| 302 | news.deliveryPipelineId, |
| 303 | output?.deliveryPipelineId, |
| 304 | "deliverypipeline", |
| 305 | ); |
| 306 | const location = normalizeLocation(news.location ?? output?.location ?? env.region); |
| 307 | const name = resourceName(env.project, location, deliveryPipelineId); |
| 308 | const desiredLabels = { |
| 309 | ...toLabels(news.labels), |
| 310 | ...(yield* createInternalLabels(id)), |
| 311 | }; |
| 312 | const desiredAnnotations = news.annotations ?? {}; |
| 313 | const desiredSuspended = news.suspended === true; |
| 314 | |
| 315 | let current = yield* getByName(output?.name ?? name); |
| 316 | |
| 317 | if (current === undefined) { |
| 318 | const created = yield* clouddeploy |
| 319 | .createProjectsLocationsDeliveryPipelines({ |
| 320 | parent: parentOf(env.project, location), |
| 321 | deliveryPipelineId, |
| 322 | body: { |
| 323 | description: news.description, |
| 324 | serialPipeline: news.serialPipeline, |
| 325 | suspended: desiredSuspended, |
| 326 | annotations: desiredAnnotations, |
| 327 | labels: desiredLabels, |
| 328 | }, |
| 329 | }) |
| 330 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 331 | if (created !== undefined) { |
| 332 | yield* waitForOperation(created); |
| 333 | } |
| 334 | current = yield* waitUntilExists(getByName(name), name); |
| 335 | } |
| 336 | |
| 337 | if (current === undefined) { |
| 338 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 339 | } |
| 340 | |
| 341 | const observedLabels = tagRecord(current.labels); |
| 342 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 343 | const mask = fieldMask([ |
| 344 | (upsert.length > 0 || removed.length > 0) && "labels", |
| 345 | fingerprint(stringMap(current.annotations)) !== fingerprint(desiredAnnotations) && |
| 346 | "annotations", |
| 347 | !sameText(current.description, news.description) && "description", |
| 348 | !sameBool(current.suspended, desiredSuspended) && "suspended", |
| 349 | fingerprint(toSerialPipeline(current.serialPipeline)) !== |
| 350 | fingerprint(news.serialPipeline) && "serialPipeline", |
| 351 | ]); |
| 352 | |
| 353 | if (mask.length > 0) { |
| 354 | const operation = yield* clouddeploy.patchProjectsLocationsDeliveryPipelines({ |
| 355 | name: current.name ?? name, |
| 356 | updateMask: mask, |
| 357 | body: { |
| 358 | etag: current.etag, |
| 359 | description: news.description, |
| 360 | serialPipeline: news.serialPipeline, |
| 361 | suspended: desiredSuspended, |
| 362 | annotations: desiredAnnotations, |
| 363 | labels: desiredLabels, |
| 364 | }, |
| 365 | }); |
| 366 | yield* waitForOperation(operation); |
| 367 | current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name); |
| 368 | } |
| 369 | |
| 370 | return toAttrs(current, env.project); |
| 371 | }), |
| 372 | |
| 373 | delete: Effect.fn(function* ({ output }) { |
| 374 | const operation = yield* clouddeploy |
| 375 | .deleteProjectsLocationsDeliveryPipelines({ |
| 376 | name: output.name, |
| 377 | force: true, |
| 378 | allowMissing: true, |
| 379 | }) |
| 380 | .pipe( |
| 381 | Effect.retry({ |
| 382 | while: (error) => error._tag === "Conflict", |
| 383 | times: 8, |
| 384 | schedule: Schedule.spaced("2 seconds"), |
| 385 | }), |
| 386 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 387 | ); |
| 388 | if (operation !== undefined) { |
| 389 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 390 | } |
| 391 | yield* waitUntilGone(getByName(output.name), output.name); |
| 392 | }), |
| 393 | }); |