@catladder/pipeline 1.134.0 → 1.134.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/dist/bundles/catladder-gitlab/index.js +2 -2
  2. package/dist/constants.js +1 -1
  3. package/dist/deploy/cloudRun/createJobs/cloudRunJobs.d.ts +7 -0
  4. package/dist/deploy/cloudRun/createJobs/cloudRunJobs.js +178 -0
  5. package/dist/deploy/cloudRun/createJobs/cloudRunServices.d.ts +4 -0
  6. package/dist/deploy/cloudRun/createJobs/cloudRunServices.js +53 -0
  7. package/dist/deploy/cloudRun/createJobs/common.d.ts +15 -0
  8. package/dist/deploy/cloudRun/createJobs/common.js +78 -0
  9. package/dist/deploy/cloudRun/createJobs/getCloudRunDeployScripts.d.ts +2 -0
  10. package/dist/deploy/cloudRun/createJobs/getCloudRunDeployScripts.js +56 -0
  11. package/dist/deploy/cloudRun/createJobs/getCloudRunStopScripts.d.ts +2 -0
  12. package/dist/deploy/cloudRun/createJobs/getCloudRunStopScripts.js +52 -0
  13. package/dist/deploy/cloudRun/createJobs/index.d.ts +3 -0
  14. package/dist/deploy/cloudRun/createJobs/index.js +43 -0
  15. package/dist/deploy/cloudRun/createJobs/variables.d.ts +5 -0
  16. package/dist/deploy/cloudRun/createJobs/variables.js +19 -0
  17. package/dist/deploy/cloudRun/index.js +2 -2
  18. package/dist/tsconfig.tsbuildinfo +1 -1
  19. package/package.json +1 -1
  20. package/src/deploy/cloudRun/createJobs/cloudRunJobs.ts +178 -0
  21. package/src/deploy/cloudRun/createJobs/cloudRunServices.ts +70 -0
  22. package/src/deploy/cloudRun/createJobs/common.ts +46 -0
  23. package/src/deploy/cloudRun/createJobs/getCloudRunDeployScripts.ts +58 -0
  24. package/src/deploy/cloudRun/createJobs/getCloudRunStopScripts.ts +34 -0
  25. package/src/deploy/cloudRun/createJobs/index.ts +45 -0
  26. package/src/deploy/cloudRun/createJobs/variables.ts +21 -0
  27. package/src/deploy/cloudRun/index.ts +1 -1
  28. package/dist/deploy/cloudRun/deployJob.d.ts +0 -3
  29. package/dist/deploy/cloudRun/deployJob.js +0 -261
  30. package/src/deploy/cloudRun/deployJob.ts +0 -318
@@ -1,318 +0,0 @@
1
- import { dump } from "js-yaml";
2
- import { merge, omit } from "lodash";
3
- import { GCLOUD_DEPLOY_CREDENTIALS_KEY } from ".";
4
- import { getDockerJobBaseProps } from "../../build/docker";
5
- import { getLabels } from "../../context/getLabels";
6
- import { getRunnerImage } from "../../runner";
7
- import type { Context } from "../../types/context";
8
- import type { CatladderJob } from "../../types/jobs";
9
- import { allowFailureInScripts, collapseableSection } from "../../utils/gitlab";
10
- import { createDeployementJobs } from "../base";
11
- import {
12
- getDependencyTrackDeleteScript,
13
- getDependencyTrackUploadScript,
14
- } from "../sbom";
15
- import type {
16
- DeployConfigCloudRun,
17
- DeployConfigCloudRunJob,
18
- DeployConfigCloudRunJobWithSchedule,
19
- DeployConfigCloudRunService,
20
- } from "../types";
21
- import { isOfDeployType } from "../types";
22
- import { createArgsString } from "./utils/createArgsString";
23
- import {
24
- getDatabaseCreateScript,
25
- getDatabaseDeleteScript,
26
- } from "./utils/database";
27
- import { gcloudServiceAccountLoginCommands } from "./utils/gcloudServiceAccountLoginCommands";
28
- import { getCloudRunJobName } from "./utils/jobName";
29
- import { getArtifactsRegistryImage } from "./artifactsRegistry";
30
- import { getServiceName } from "./utils/getServiceName";
31
- import { getRemoveOldRevisionsAndImagesCommand } from "./cleanup";
32
-
33
- const setGoogleProjectNumberScript = (deployConfig: DeployConfigCloudRun) => [
34
- `export GCLOUD_PROJECT_NUMBER=$(gcloud projects describe ${deployConfig.projectId} --format="value(projectNumber)")`,
35
- `echo "GCLOUD_PROJECT_NUMBER: $GCLOUD_PROJECT_NUMBER"`,
36
- ];
37
-
38
- const makeLabelString = (obj: Record<string, unknown>) =>
39
- Object.entries(obj)
40
- .map(([key, value]) => `${key}=${value}`)
41
- .join(",");
42
-
43
- export const createGoogleCloudRunDeployJobs = (
44
- context: Context
45
- ): CatladderJob[] => {
46
- const deployConfig = context.componentConfig.deploy;
47
- if (deployConfig === false) {
48
- return [];
49
- }
50
- if (!isOfDeployType(deployConfig, "google-cloudrun")) {
51
- // should not happen
52
- throw new Error("deploy config is wrong");
53
- }
54
-
55
- const allEnvVars = omit(
56
- context.environment.envVars,
57
- GCLOUD_DEPLOY_CREDENTIALS_KEY
58
- );
59
-
60
- const commonArgs = {
61
- project: deployConfig.projectId,
62
- region: deployConfig.region,
63
- };
64
-
65
- const commonDeployArgs = {
66
- image: getArtifactsRegistryImage(context),
67
- ...commonArgs,
68
- "set-cloudsql-instances": deployConfig.cloudSql
69
- ? deployConfig.cloudSql.instanceConnectionName
70
- : undefined,
71
- };
72
-
73
- const serviceName = getServiceName(context);
74
-
75
- const getFullJobName = (name: string) =>
76
- getCloudRunJobName(context.environment.fullName, name);
77
-
78
- const jobsWithNames = Object.entries(deployConfig.jobs ?? {})
79
- // filter out disabled jobs
80
- .filter((entry): entry is [string, DeployConfigCloudRunJob] =>
81
- Boolean(entry[1])
82
- )
83
- .map(([name, job]) => ({
84
- jobName: getFullJobName(name),
85
- job,
86
- }));
87
- const jobsWithSchedule = jobsWithNames
88
- .filter(
89
- (
90
- entry
91
- ): entry is {
92
- jobName: string;
93
- job: DeployConfigCloudRunJobWithSchedule;
94
- } => entry.job.when === "schedule"
95
- )
96
- .map(({ job, jobName }) => ({
97
- job,
98
- jobName,
99
- schedulerName: jobName + "-scheduler",
100
- }));
101
-
102
- const getServiceDeployScript = (
103
- service: DeployConfigCloudRunService | true | undefined,
104
- nameSuffix?: string
105
- ) => {
106
- const customConfig = service !== true ? service : undefined;
107
- const command =
108
- service !== true
109
- ? service?.command ?? context.componentConfig.build.startCommand
110
- : undefined;
111
-
112
- const commandArray = command
113
- ? Array.isArray(command)
114
- ? command
115
- : command.split(" ")
116
- : undefined;
117
- const fullServiceName = `${serviceName}${nameSuffix ?? ""}`;
118
- const argsString = createArgsString({
119
- // command as empty string resets it to default (uses the image's entrypoint)
120
- command: commandArray ? '"' + commandArray.join(",") + '"' : '""',
121
- ...commonDeployArgs,
122
- labels: makeLabelString({
123
- ...getLabels(context),
124
- "cloud-run-service-name": fullServiceName,
125
- }),
126
- "env-vars-file": "____envvars.yaml",
127
- "min-instances": customConfig?.minInstances ?? 0,
128
- "max-instances": customConfig?.maxInstances ?? 100,
129
- "cpu-throttling": customConfig?.noCpuThrottling !== true,
130
- memory: customConfig?.memory,
131
- "allow-unauthenticated": customConfig?.allowUnauthenticated ?? true,
132
- ingress: customConfig?.ingress ?? "all",
133
- "cpu-boost": true,
134
- });
135
-
136
- return `gcloud run deploy ${fullServiceName} ${argsString}`;
137
- };
138
-
139
- const getJobCreateScriptsForJob = (
140
- jobName: string,
141
- job: DeployConfigCloudRunJob
142
- ) => {
143
- // due to some oversight from google, jobs create does not accept `--env-vars-file` 🤦
144
- // lucky, update on the other hand accepts it... so let's just imediatly update it
145
-
146
- // also we cannot upsert a job, so we have to create it and catch the error and then update
147
-
148
- const commandArray = Array.isArray(job.command)
149
- ? job.command
150
- : job.command.split(" ");
151
-
152
- const commonDeployArgsString = createArgsString({
153
- command: '"' + commandArray.join(",") + '"',
154
- ...commonDeployArgs,
155
- labels: makeLabelString({
156
- ...getLabels(context),
157
- "cloud-run-job-name": jobName,
158
- }),
159
- image: job.image || commonDeployArgs.image,
160
- memory: job.memory || "512Mi",
161
- "task-timeout": job.timeout || "10m",
162
- });
163
-
164
- const argsString = `${jobName} ${commonDeployArgsString}`;
165
- return [
166
- ...allowFailureInScripts([`gcloud beta run jobs create ${argsString}`]),
167
- `gcloud beta run jobs update ${argsString} --env-vars-file=____envvars.yaml`,
168
- ];
169
- };
170
-
171
- const commonArgsString = createArgsString(commonArgs);
172
-
173
- const getJobCreateScripts = () =>
174
- jobsWithNames
175
- .map(({ job, jobName }) => getJobCreateScriptsForJob(jobName, job))
176
- .flat();
177
-
178
- const getJobRunScriptForJob = (jobName: string, wait: boolean) => {
179
- return `gcloud beta run jobs execute ${jobName} ${commonArgsString}${
180
- wait ? " --wait" : ""
181
- }`;
182
- };
183
-
184
- const getJobRunScripts = (when: DeployConfigCloudRunJob["when"]) =>
185
- jobsWithNames
186
- .filter(({ job }) => job.when === when)
187
- .map(({ jobName }) =>
188
- getJobRunScriptForJob(
189
- jobName,
190
- // wait for completin on stop jobs, since stop will delete the jobs afterwards, so they will fail
191
- ["preStop", "postStop"].includes(when)
192
- )
193
- );
194
-
195
- const getCreateScheduleScripts = () => {
196
- return jobsWithSchedule
197
- .map(({ job, jobName, schedulerName }) => {
198
- const argsString = createArgsString({
199
- project: deployConfig.projectId,
200
- location: deployConfig.region,
201
- schedule: `"${job.schedule}"`,
202
- "max-retry-attempts": job.maxRetryAttempts ?? 0,
203
-
204
- uri: `"https://${deployConfig.region}-run.googleapis.com/apis/run.googleapis.com/v1/namespaces/${deployConfig.projectId}/jobs/${jobName}:run"`,
205
- "http-method": "POST",
206
- "oauth-service-account-email":
207
- "$GCLOUD_PROJECT_NUMBER-compute@developer.gserviceaccount.com",
208
- });
209
- const commonArgs = `http ${schedulerName} ${argsString}`;
210
- return [
211
- ...allowFailureInScripts([
212
- `gcloud scheduler jobs create ${commonArgs}`,
213
- ]),
214
- `gcloud scheduler jobs update ${commonArgs}`,
215
- ];
216
- })
217
- .flat();
218
- };
219
-
220
- const getDeleteSchedulesScripts = () => {
221
- const argsString = createArgsString({
222
- project: deployConfig.projectId,
223
- location: deployConfig.region,
224
- });
225
- return jobsWithSchedule
226
- .map(({ schedulerName }) => {
227
- return [`gcloud scheduler jobs delete ${schedulerName} ${argsString}`];
228
- })
229
- .flat();
230
- };
231
-
232
- const getDeleteJobsScripts = () =>
233
- jobsWithNames.flatMap(({ jobName }) => [
234
- // first delete all job executions. Otherwise delete might fail if one of those is still running
235
- `gcloud beta run jobs executions list ${commonArgsString} --job ${jobName} --format="value(name)" | xargs -I {} gcloud beta run jobs executions delete {} --quiet ${commonArgsString}`,
236
- `gcloud beta run jobs delete ${jobName} ${commonArgsString}`,
237
- ]);
238
-
239
- const deployScripts = [
240
- ...collapseableSection(
241
- "prepare",
242
- "Prepare..."
243
- )([
244
- ...gcloudServiceAccountLoginCommands(context),
245
- ...setGoogleProjectNumberScript(deployConfig),
246
- ]),
247
- ...collapseableSection(
248
- "deploy",
249
- "Deploy to cloud run"
250
- )([
251
- `echo "$ENV_VARS" > ____envvars.yaml`, // TODO: split secrets out
252
- ...(deployConfig.cloudSql
253
- ? getDatabaseCreateScript(context, deployConfig) // we create the db, so that we can also delete it afterwards
254
- : []),
255
- ...getCreateScheduleScripts(),
256
- ...getJobCreateScripts(),
257
- ...getJobRunScripts("preDeploy"),
258
-
259
- ...(deployConfig.service !== false
260
- ? [getServiceDeployScript(deployConfig.service)]
261
- : []),
262
- ...Object.entries(deployConfig.additionalServices ?? {}).map(
263
- ([name, service]) => getServiceDeployScript(service, "-" + name)
264
- ),
265
- ...getJobRunScripts("postDeploy"),
266
- ]),
267
- ...collapseableSection(
268
- "cleanup",
269
- "Cleanup"
270
- )(
271
- getRemoveOldRevisionsAndImagesCommand(context, "postDeploy") // we cleanup inactive images both on deploy and stop
272
- ),
273
- ...getDependencyTrackUploadScript(context),
274
- ];
275
-
276
- const stopScripts = [
277
- ...gcloudServiceAccountLoginCommands(context),
278
- ...getJobRunScripts("preStop"),
279
- ...(deployConfig.service !== false
280
- ? [`gcloud run services delete ${serviceName} ${commonArgsString}`]
281
- : []),
282
- ...Object.entries(deployConfig.additionalServices ?? {}).map(
283
- ([name]) =>
284
- `gcloud run services delete ${serviceName}-${name} ${commonArgsString}`
285
- ),
286
- ...getJobRunScripts("postStop"),
287
- ...getDeleteSchedulesScripts(),
288
- ...getDeleteJobsScripts(),
289
- ...(deployConfig.cloudSql && deployConfig.cloudSql.deleteDatabaseOnStop
290
- ? getDatabaseDeleteScript(context, deployConfig)
291
- : []),
292
-
293
- ...getRemoveOldRevisionsAndImagesCommand(context, "onStop"), // we cleanup inactive images both on deploy and stop
294
- ...getDependencyTrackDeleteScript(context),
295
- ];
296
- return createDeployementJobs(context, {
297
- deploy: merge(getDockerJobBaseProps(context), {
298
- artifacts: { paths: ["____envvars.yaml"] },
299
- variables: {
300
- CLOUDSDK_CORE_DISABLE_PROMPTS: "1",
301
- ENV_VARS: dump(allEnvVars, {
302
- lineWidth: -1,
303
- quotingType: "'",
304
- forceQuotes: true,
305
- }),
306
- },
307
- image: getRunnerImage("gcloud"),
308
- script: deployScripts,
309
- }),
310
- stop: {
311
- image: getRunnerImage("gcloud"),
312
- variables: {
313
- CLOUDSDK_CORE_DISABLE_PROMPTS: "1",
314
- },
315
- script: allowFailureInScripts(stopScripts),
316
- },
317
- });
318
- };