@catladder/pipeline 1.167.0 → 1.168.0

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.
package/package.json CHANGED
@@ -53,7 +53,7 @@
53
53
  }
54
54
  ],
55
55
  "license": "MIT",
56
- "version": "1.167.0",
56
+ "version": "1.168.0",
57
57
  "scripts": {
58
58
  "build:tsc": "yarn tsc",
59
59
  "build": "yarn build:compile && yarn build:inline-variables && yarn build:bundle",
@@ -1,5 +1,4 @@
1
1
  import type { ComponentContext } from "../../../types/context";
2
- import { allowFailureInScripts } from "../../../utils/gitlab";
3
2
 
4
3
  import type {
5
4
  DeployConfigCloudRunJob,
@@ -22,6 +21,7 @@ import type {
22
21
  StringOrBashExpression,
23
22
  } from "../../../bash/BashExpression";
24
23
  import { ENV_VARS_FILENAME } from "./constants";
24
+ import { notNil } from "../../../utils";
25
25
 
26
26
  const getJobRunScriptForJob = (
27
27
  context: ComponentContext,
@@ -81,83 +81,115 @@ export const getJobRunScripts = (
81
81
  );
82
82
  };
83
83
 
84
- export const getJobCreateScripts = (context: ComponentContext) => {
85
- const jobsWithNames = getCloudRunJobsWithNames(context);
84
+ export const getJobCreateScripts = (context: ComponentContext): string[] =>
85
+ getCloudRunJobsWithNames(context).map(
86
+ (
87
+ {
88
+ job: {
89
+ command,
90
+ image,
91
+ cpu,
92
+ memory = "512Mi",
93
+ timeout = "10m",
94
+ parallelism = 1,
95
+ volumes,
96
+ },
97
+ jobName,
98
+ },
99
+ jobIndex,
100
+ ): string => {
101
+ const commandArray = Array.isArray(command)
102
+ ? command
103
+ : command.split(" ");
104
+
105
+ const {
106
+ image: commonImage,
107
+ project,
108
+ region,
109
+ ...deployArgs
110
+ } = getCommonDeployArgs(context);
111
+ const commonDeployArgsString = createArgsString({
112
+ command: `"${commandArray.join(",")}"`,
113
+ labels: `"${makeLabelString(getLabels(context))},cloud-run-job-name=$current_job_name"`,
114
+ image: `"${image ?? commonImage}"`,
115
+ project,
116
+ region,
117
+ cpu,
118
+ memory,
119
+ parallelism,
120
+ "task-timeout": timeout,
121
+ "env-vars-file": ENV_VARS_FILENAME,
122
+ "max-retries": 0,
123
+ ...deployArgs,
124
+ });
86
125
 
87
- return jobsWithNames
88
- .map(({ job, jobName }) => getJobCreateScriptsForJob(context, jobName, job))
89
- .flat();
90
- };
126
+ // due to some oversight from google, jobs create does not yet accept `--add-volume` 🤦
127
+ // lucky, update on the other hand accepts it... so let's just imediatly update it
128
+ // we cannot upsert a job, so we have to create it and catch the error and then update
129
+ const hasVolumes = Object.keys(volumes || {}).length > 0;
130
+ const needsBeta = hasVolumes ? "beta" : undefined;
131
+ const volumeArgs = hasVolumes
132
+ ? createArgsString(...createVolumeConfig(volumes, "job"))
133
+ : "";
91
134
 
92
- const getJobCreateScriptsForJob = (
135
+ return [
136
+ jobIndex === 0
137
+ ? `exist_job_names="$(\n ${gcloudRunCmd()} jobs list --filter='metadata.name ~ ${context.env}.*${context.name}' --format='value(name)' --limit=999 --project='${project}' --region='${region}'\n)"`
138
+ : null,
139
+ `current_job_name="${jobName}"`,
140
+ 'if grep "$current_job_name" <<<"$exist_job_names" >/dev/null; then',
141
+ ` ${gcloudRunCmd(needsBeta)} jobs update "$current_job_name" ${commonDeployArgsString} ${volumeArgs}`,
142
+ "else",
143
+ ` ${gcloudRunCmd()} jobs create "$current_job_name" ${commonDeployArgsString}`,
144
+ "fi",
145
+ ]
146
+ .filter(notNil)
147
+ .join("\n");
148
+ },
149
+ );
150
+
151
+ export const getCreateScheduleScripts = (
93
152
  context: ComponentContext,
94
- jobName: StringOrBashExpression,
95
- job: DeployConfigCloudRunJob,
96
- ) => {
97
- const commonDeployArgs = getCommonDeployArgs(context);
98
-
99
- // due to some oversight from google, jobs create does not yet accept `--add-volume` 🤦
100
- // lucky, update on the other hand accepts it... so let's just imediatly update it
101
- // we cannot upsert a job, so we have to create it and catch the error and then update
102
- const commandArray = Array.isArray(job.command)
103
- ? job.command
104
- : job.command.split(" ");
105
-
106
- const commonDeployArgsString = createArgsString({
107
- command: '"' + commandArray.join(",") + '"',
108
- ...commonDeployArgs,
109
- labels: makeLabelString({
110
- ...getLabels(context),
111
- "cloud-run-job-name": jobName.toString(),
112
- }),
113
- image: job.image || commonDeployArgs.image,
114
- cpu: job?.cpu,
115
- memory: job.memory || "512Mi",
116
- "task-timeout": job.timeout || "10m",
117
- parallelism: job.parallelism || 1,
118
- "env-vars-file": ENV_VARS_FILENAME,
119
- "max-retries": 0,
120
- });
121
-
122
- const requiresBeta = job?.volumes && Object.keys(job?.volumes).length > 0;
123
-
124
- const argsString = `${jobName} ${commonDeployArgsString}`;
125
- return [
126
- ...allowFailureInScripts([`${gcloudRunCmd()} jobs create ${argsString}`]),
127
- `${gcloudRunCmd(
128
- requiresBeta ? "beta" : undefined,
129
- )} jobs update ${argsString} ${createArgsString(
130
- ...createVolumeConfig(job?.volumes, "job"),
131
- )}`,
132
- ];
133
- };
134
-
135
- export const getCreateScheduleScripts = (context: ComponentContext) => {
153
+ ): string[] => {
136
154
  const jobsWithSchedule = getCloudRunJobsWithSchedule(context);
137
- const deployConfig = getCloudRunDeployConfig(context);
138
-
139
- return jobsWithSchedule
140
- .map(({ job, jobName, schedulerName }) => {
155
+ const { region: location, projectId: project } =
156
+ getCloudRunDeployConfig(context);
157
+
158
+ const uriBase = `https://${location}-run.googleapis.com/apis/run.googleapis.com/v1/namespaces/${project}/jobs`;
159
+ const gcloudArgs = {
160
+ project,
161
+ location,
162
+ uri: `"$current_job_uri"`,
163
+ "http-method": "POST",
164
+ "oauth-service-account-email": `"$GCLOUD_PROJECT_NUMBER-compute@developer.gserviceaccount.com"`,
165
+ };
166
+
167
+ return jobsWithSchedule.map(
168
+ (
169
+ { job: { maxRetryAttempts, schedule }, jobName, schedulerName },
170
+ jobIndex,
171
+ ): string => {
141
172
  const argsString = createArgsString({
142
- project: deployConfig.projectId,
143
- location: deployConfig.region,
144
- schedule: `"${job.schedule}"`,
145
- "max-retry-attempts": job.maxRetryAttempts ?? 0,
146
-
147
- uri: `"https://${deployConfig.region}-run.googleapis.com/apis/run.googleapis.com/v1/namespaces/${deployConfig.projectId}/jobs/${jobName}:run"`,
148
- "http-method": "POST",
149
- "oauth-service-account-email":
150
- "$GCLOUD_PROJECT_NUMBER-compute@developer.gserviceaccount.com",
173
+ ...gcloudArgs,
174
+ schedule: `"${schedule}"`,
175
+ "max-retry-attempts": maxRetryAttempts ?? 0,
151
176
  });
152
- const commonArgs = `http ${schedulerName} ${argsString}`;
153
177
  return [
154
- ...allowFailureInScripts([
155
- `${gcloudSchedulerCmd()} jobs create ${commonArgs}`,
156
- ]),
157
- `${gcloudSchedulerCmd()} jobs update ${commonArgs}`,
158
- ];
159
- })
160
- .flat();
178
+ jobIndex === 0
179
+ ? `exist_scheduler_names="$(\n ${gcloudSchedulerCmd()} jobs list --filter='httpTarget.uri ~ ${context.env}.*${context.name}' --format='value(name)' --limit=999 --location='${location}' --project='${project}'\n)"`
180
+ : null,
181
+ `current_job_uri="${uriBase}/${jobName}:run"`,
182
+ `current_scheduler_name="${schedulerName}"`,
183
+ `if grep "$current_scheduler_name" <<<"$exist_scheduler_names" >/dev/null; then`,
184
+ ` ${gcloudSchedulerCmd()} jobs update http "$current_scheduler_name" ${argsString}`,
185
+ `else`,
186
+ ` ${gcloudSchedulerCmd()} jobs create http "$current_scheduler_name" ${argsString}`,
187
+ `fi`,
188
+ ]
189
+ .filter(notNil)
190
+ .join("\n");
191
+ },
192
+ );
161
193
  };
162
194
 
163
195
  const getCloudRunJobsWithSchedule = (context: ComponentContext) => {
@@ -170,11 +202,13 @@ const getCloudRunJobsWithSchedule = (context: ComponentContext) => {
170
202
  ): entry is {
171
203
  jobName: BashExpression;
172
204
  job: DeployConfigCloudRunJobWithSchedule;
205
+ jobKey: string;
173
206
  } => entry.job.when === "schedule",
174
207
  )
175
- .map(({ job, jobName }) => ({
208
+ .map(({ job, jobName, jobKey }) => ({
176
209
  job,
177
210
  jobName,
211
+ jobKey,
178
212
  schedulerName: jobName.concat("-scheduler"),
179
213
  }));
180
214
  };
@@ -190,9 +224,10 @@ const getCloudRunJobsWithNames = (context: ComponentContext) => {
190
224
  .filter((entry): entry is [string, DeployConfigCloudRunJob] =>
191
225
  Boolean(entry[1]),
192
226
  )
193
- .map(([name, job]) => ({
194
- jobName: getFullJobName(name),
227
+ .map(([jobKey, job]) => ({
228
+ jobName: getFullJobName(jobKey),
195
229
  job,
230
+ jobKey,
196
231
  }));
197
232
  return jobsWithNames;
198
233
  };