@catladder/pipeline 1.147.0 → 1.148.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.
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+
3
+ var __read = this && this.__read || function (o, n) {
4
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
5
+ if (!m) return o;
6
+ var i = m.call(o),
7
+ r,
8
+ ar = [],
9
+ e;
10
+ try {
11
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
12
+ } catch (error) {
13
+ e = {
14
+ error: error
15
+ };
16
+ } finally {
17
+ try {
18
+ if (r && !r.done && (m = i["return"])) m.call(i);
19
+ } finally {
20
+ if (e) throw e.error;
21
+ }
22
+ }
23
+ return ar;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", {
26
+ value: true
27
+ });
28
+ exports.removeUndefined = void 0;
29
+ var removeUndefined = function (obj) {
30
+ return Object.fromEntries(Object.entries(obj).filter(function (_a) {
31
+ var _b = __read(_a, 2),
32
+ value = _b[1];
33
+ return value !== undefined;
34
+ }));
35
+ };
36
+ exports.removeUndefined = removeUndefined;
package/package.json CHANGED
@@ -52,7 +52,7 @@
52
52
  }
53
53
  ],
54
54
  "license": "MIT",
55
- "version": "1.147.0",
55
+ "version": "1.148.0",
56
56
  "scripts": {
57
57
  "build:tsc": "yarn tsc",
58
58
  "build": "yarn build:compile && yarn build:inline-variables && yarn build:bundle",
@@ -3,6 +3,7 @@ import { writeYamlfile } from "../utils/writeFiles";
3
3
  import { createChildPipeline } from "./createChildPipeline";
4
4
  import { createMainPipeline } from "./createMainPipeline";
5
5
  import { getPipelineTriggerForGitlabChildPipeline } from "./gitlab/getPipelineTriggerForGitlabChildPipeline";
6
+ import { sortGitLabJobDefProps } from "./gitlab/sortGitLabJobDefProps";
6
7
  export async function generatePipelineFiles<T extends PipelineType>(
7
8
  config: Config,
8
9
  pipelineType: T,
@@ -11,28 +12,40 @@ export async function generatePipelineFiles<T extends PipelineType>(
11
12
  if (mode === "childpipeline") {
12
13
  const trigger = getPipelineTriggerForGitlabChildPipeline();
13
14
 
14
- const { jobs, image, stages, variables, workflow, ...mainPipeline } =
15
+ const { jobs, image, stages, variables, workflow, ...pipelineRest } =
15
16
  await createChildPipeline(pipelineType, trigger, config);
16
- // need to spread out the jobs, forgot why
17
+ const jobsWithSortedProps = Object.fromEntries(
18
+ Object.entries(jobs).map(([jobName, job]) => [
19
+ jobName,
20
+ sortGitLabJobDefProps(job),
21
+ ]),
22
+ );
17
23
  await writeYamlfile(`__pipeline.yml`, {
18
24
  image,
19
25
  stages,
20
26
  variables,
21
27
  workflow,
22
- ...mainPipeline,
23
- ...jobs,
28
+ ...pipelineRest,
29
+ // jobs need to be spread into main YAML, because GitLab pipeline YAML has no jobs key - jobs are top level with their key as their name
30
+ ...jobsWithSortedProps,
24
31
  });
25
32
  } else {
26
- const { jobs, image, stages, variables, workflow, ...mainPipeline } =
33
+ const { jobs, image, stages, variables, workflow, ...pipelineRest } =
27
34
  await createMainPipeline(pipelineType, config);
28
- // need to spread out the jobs, forgot why
35
+ const jobsWithSortedProps = Object.fromEntries(
36
+ Object.entries(jobs).map(([jobName, job]) => [
37
+ jobName,
38
+ sortGitLabJobDefProps(job),
39
+ ]),
40
+ );
29
41
  await writeYamlfile(`.gitlab-ci.yml`, {
30
42
  image,
31
43
  stages,
32
44
  variables,
33
45
  workflow,
34
- ...mainPipeline,
35
- ...jobs,
46
+ ...pipelineRest,
47
+ // jobs need to be spread into main YAML, because GitLab pipeline YAML has no jobs key - jobs are top level with their key as their name
48
+ ...jobsWithSortedProps,
36
49
  });
37
50
  }
38
51
  }
@@ -11,6 +11,7 @@ import type { CatladderJob, CatladderJobNeed } from "../../types/jobs";
11
11
  import { notNil } from "../../utils";
12
12
  import { collapseableSection } from "../../utils/gitlab";
13
13
  import type { AllCatladderJobs } from "../createAllJobs";
14
+ import { removeUndefined } from "../../utils/removeUndefined";
14
15
 
15
16
  export type AllGitlabJobs = {
16
17
  name: string;
@@ -20,10 +21,6 @@ export type AllGitlabJobs = {
20
21
  }[];
21
22
 
22
23
  export const GITLAB_ENVIRONMENT_URL_VARIABLE = "CL_GITLAB_ENVIRONMENT_URL";
23
- const removeUndefined = (obj: GitlabJobDef): GitlabJobDef =>
24
- Object.fromEntries(
25
- Object.entries(obj).filter(([, value]) => value !== undefined),
26
- ) as GitlabJobDef;
27
24
  const getFullJobName = (
28
25
  name: string,
29
26
  componentName: string,
@@ -197,9 +194,7 @@ export const makeGitlabJob = (
197
194
  allJobs,
198
195
  );
199
196
 
200
- const gitlabJob: GitlabJobDef = removeUndefined(modified);
201
-
202
- return [fullJobName, gitlabJob];
197
+ return [fullJobName, removeUndefined(modified)];
203
198
  };
204
199
 
205
200
  const addGitlabEnvironment = (
@@ -0,0 +1,59 @@
1
+ import type { GitlabJobDef } from "../../types";
2
+
3
+ type GitlabJobDefKey = keyof GitlabJobDef;
4
+
5
+ /**
6
+ * The desired order of GitLab job definition keys.
7
+ */
8
+ const jobDefOrder: GitlabJobDefKey[] = [
9
+ "stage",
10
+ "tags",
11
+ "image",
12
+ "services",
13
+ "variables",
14
+ "before_script",
15
+ "script",
16
+ "after_script",
17
+ "cache",
18
+ "coverage",
19
+ "environment",
20
+ "release",
21
+ "artifacts",
22
+ "rules",
23
+ "only",
24
+ "except",
25
+ "needs",
26
+ "dependencies",
27
+ "trigger",
28
+ "retry",
29
+ "interruptible",
30
+ "allow_failure",
31
+ "parallel",
32
+ "hooks",
33
+ "resource_group",
34
+ ];
35
+
36
+ const sortGitLabJobDefKeys = (jobDef: GitlabJobDef): GitlabJobDefKey[] =>
37
+ (Object.keys(jobDef) as GitlabJobDefKey[]).sort((a, b) =>
38
+ jobDefOrder.indexOf(a) === -1 && jobDefOrder.indexOf(b) === -1
39
+ ? 0
40
+ : jobDefOrder.indexOf(a) === -1
41
+ ? 1
42
+ : jobDefOrder.indexOf(b) === -1
43
+ ? -1
44
+ : jobDefOrder.indexOf(a) - jobDefOrder.indexOf(b),
45
+ );
46
+
47
+ /**
48
+ * Sorts the properties of a GitLab job definition based on the desired order.
49
+ *
50
+ * This is only useful when generating a GitLab pipeline YAML file.
51
+ */
52
+ export const sortGitLabJobDefProps = (jobDef: GitlabJobDef): GitlabJobDef =>
53
+ sortGitLabJobDefKeys(jobDef).reduce(
54
+ (acc, key) =>
55
+ jobDef[key] !== undefined
56
+ ? Object.assign(acc, { [key]: jobDef[key] })
57
+ : acc,
58
+ {} as GitlabJobDef,
59
+ );
@@ -0,0 +1,4 @@
1
+ export const removeUndefined = <T extends object>(obj: T): T =>
2
+ Object.fromEntries(
3
+ Object.entries(obj).filter(([, value]) => value !== undefined),
4
+ ) as T;