/ opencodehub / src / lib / cloud-hooks.ts
Swadhin Biswas feat(api): add runner, analytics, packages, and security services · 3/6/2026
28f7954
131 lines · 4.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { and, eq } from "drizzle-orm";
import { getDatabase, schema } from "@/db";
import {
  awsDeployECS,
  azureDeployAppService,
  gcpDeployCloudRun,
  k8sApplyDeployment,
  type CloudConfig,
} from "@/lib/cloud-integrations";
import { logger } from "@/lib/logger";

export interface CloudHookResult {
  configId: string;
  provider: string;
  success: boolean;
  deploymentId?: string;
  message?: string;
}

export async function triggerCloudDeploy(options: {
  repositoryId: string;
  configId?: string;
  imageTag?: string;
  clusterName?: string;
  serviceName?: string;
  taskDefinition?: string;
  appName?: string;
  namespace?: string;
  deploymentName?: string;
}): Promise<CloudHookResult[]> {
  const db = getDatabase() as NodePgDatabase<typeof schema>;
  const configs =
    (await db.query.cloudConfigs?.findMany({
      where: and(
        eq(schema.cloudConfigs.repositoryId, options.repositoryId),
        eq(schema.cloudConfigs.isEnabled, true)
      ),
    })) || [];

  const targets = options.configId
    ? configs.filter((config) => config.id === options.configId)
    : configs.filter((config) => ["aws", "gcp", "azure", "kubernetes"].includes(config.provider));

  const image = options.imageTag || "latest";
  const results: CloudHookResult[] = [];

  for (const config of targets) {
    try {
      if (config.provider === "aws") {
        const deployment = await awsDeployECS({
          config: config as CloudConfig,
          clusterName: options.clusterName || "default",
          serviceName: options.serviceName || "app",
          taskDefinition: options.taskDefinition || "app",
          imageTag: image,
        });
        results.push({
          configId: config.id,
          provider: config.provider,
          success: !!deployment,
          deploymentId: deployment?.id,
          message: deployment ? "AWS deployment triggered" : "AWS deployment failed",
        });
        continue;
      }

      if (config.provider === "gcp") {
        const deployment = await gcpDeployCloudRun({
          config: config as CloudConfig,
          serviceName: options.serviceName || "app",
          region: config.region || "us-central1",
          image,
        });
        results.push({
          configId: config.id,
          provider: config.provider,
          success: !!deployment,
          deploymentId: deployment?.id,
          message: deployment ? "GCP deployment triggered" : "GCP deployment failed",
        });
        continue;
      }

      if (config.provider === "azure") {
        const deployment = await azureDeployAppService({
          config: config as CloudConfig,
          resourceGroup: options.namespace || "default",
          appName: options.appName || "app",
          image,
        });
        results.push({
          configId: config.id,
          provider: config.provider,
          success: !!deployment,
          deploymentId: deployment?.id,
          message: deployment ? "Azure deployment triggered" : "Azure deployment failed",
        });
        continue;
      }

      if (config.provider === "kubernetes") {
        const deployment = await k8sApplyDeployment({
          config: config as CloudConfig,
          namespace: options.namespace || "default",
          name: options.deploymentName || "app",
          image,
          replicas: 1,
        });
        results.push({
          configId: config.id,
          provider: config.provider,
          success: !!deployment,
          deploymentId: deployment?.id,
          message: deployment ? "Kubernetes deployment applied" : "Kubernetes deployment failed",
        });
      }
    } catch (error) {
      logger.error({ configId: config.id, provider: config.provider, error }, "Cloud deploy hook failed");
      results.push({
        configId: config.id,
        provider: config.provider,
        success: false,
        message: error instanceof Error ? error.message : "Unknown cloud deploy error",
      });
    }
  }

  return results;
}