@catladder/pipeline 0.0.6 → 1.0.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 (208) hide show
  1. package/babel-inline.json +11 -0
  2. package/bin/catladder-gitlab.js +3 -0
  3. package/dist/build/base/constants.d.ts +7 -0
  4. package/dist/build/base/constants.js +11 -0
  5. package/dist/build/base/createBuildJob.d.ts +5 -0
  6. package/dist/build/base/createBuildJob.js +99 -0
  7. package/dist/build/docker.d.ts +5 -0
  8. package/dist/build/docker.js +95 -0
  9. package/dist/build/index.d.ts +12 -0
  10. package/dist/build/index.js +63 -0
  11. package/dist/build/node/buildJob.d.ts +3 -0
  12. package/dist/build/node/buildJob.js +97 -0
  13. package/dist/build/node/cache.d.ts +4 -0
  14. package/dist/build/node/cache.js +67 -0
  15. package/dist/build/node/constants.d.ts +6 -0
  16. package/dist/build/node/constants.js +10 -0
  17. package/dist/build/node/index.d.ts +5 -0
  18. package/dist/build/node/index.js +63 -0
  19. package/dist/build/node/meteor.d.ts +3 -0
  20. package/dist/build/node/meteor.js +103 -0
  21. package/dist/build/node/testJob.d.ts +3 -0
  22. package/dist/build/node/testJob.js +112 -0
  23. package/dist/build/node/yarn.d.ts +2 -0
  24. package/dist/build/node/yarn.js +12 -0
  25. package/dist/build/tests/storybook.test.d.ts +1 -0
  26. package/dist/build/tests/storybook.test.js +221 -0
  27. package/dist/build/types.d.ts +63 -0
  28. package/dist/build/types.js +16 -0
  29. package/dist/catladder-gitlab.d.ts +1 -0
  30. package/dist/catladder-gitlab.js +44 -0
  31. package/dist/config/configruedEnvs.d.ts +4 -0
  32. package/dist/config/configruedEnvs.js +86 -0
  33. package/dist/config/configruedEnvs.test.d.ts +1 -0
  34. package/dist/config/configruedEnvs.test.js +103 -0
  35. package/dist/config/index.d.ts +2 -0
  36. package/dist/config/index.js +24 -0
  37. package/dist/config/readConfig.d.ts +2 -0
  38. package/dist/config/readConfig.js +47 -0
  39. package/dist/constants.d.ts +2 -0
  40. package/dist/constants.js +6 -0
  41. package/dist/context/index.d.ts +5 -0
  42. package/dist/context/index.js +211 -0
  43. package/dist/defaults/index.d.ts +2 -0
  44. package/dist/defaults/index.js +8 -0
  45. package/dist/deploy/base.d.ts +9 -0
  46. package/dist/deploy/base.js +94 -0
  47. package/dist/deploy/index.d.ts +11 -0
  48. package/dist/deploy/index.js +34 -0
  49. package/dist/deploy/kubernetes/cloudsql.d.ts +6 -0
  50. package/dist/deploy/kubernetes/cloudsql.js +16 -0
  51. package/dist/deploy/kubernetes/deployJob.d.ts +3 -0
  52. package/dist/deploy/kubernetes/deployJob.js +148 -0
  53. package/dist/deploy/kubernetes/index.d.ts +1 -0
  54. package/dist/deploy/kubernetes/index.js +22 -0
  55. package/dist/deploy/kubernetes/mongodb.d.ts +15 -0
  56. package/dist/deploy/kubernetes/mongodb.js +23 -0
  57. package/dist/deploy/types.d.ts +92 -0
  58. package/dist/deploy/types.js +16 -0
  59. package/dist/index.d.ts +8 -0
  60. package/dist/index.js +36 -0
  61. package/dist/pipeline/createChildPipeline.d.ts +9 -0
  62. package/dist/pipeline/createChildPipeline.js +266 -0
  63. package/dist/pipeline/createChildPipeline.test.d.ts +1 -0
  64. package/dist/pipeline/createChildPipeline.test.js +199 -0
  65. package/dist/pipeline/createJobs.d.ts +3 -0
  66. package/dist/pipeline/createJobs.js +331 -0
  67. package/dist/pipeline/index.d.ts +2 -0
  68. package/dist/pipeline/index.js +24 -0
  69. package/dist/pipeline/yarnInfo.d.ts +2 -0
  70. package/dist/pipeline/yarnInfo.js +296 -0
  71. package/dist/rules/index.d.ts +3 -0
  72. package/dist/rules/index.js +27 -0
  73. package/dist/runner/index.d.ts +3 -0
  74. package/dist/runner/index.js +12 -0
  75. package/dist/tsconfig.tsbuildinfo +1 -0
  76. package/dist/types/config.d.ts +73 -0
  77. package/dist/types/config.js +80 -0
  78. package/dist/types/context.d.ts +36 -0
  79. package/dist/types/context.js +3 -0
  80. package/dist/types/gitlab-types.d.ts +86 -0
  81. package/dist/types/gitlab-types.js +5 -0
  82. package/dist/{scripts/magic/types → types}/index.d.ts +1 -1
  83. package/dist/types/index.js +26 -0
  84. package/dist/utils/index.d.ts +2 -0
  85. package/dist/utils/index.js +18 -0
  86. package/package.json +30 -8
  87. package/src/build/base/constants.ts +7 -0
  88. package/src/build/base/createBuildJob.ts +46 -0
  89. package/{scripts/magic → src}/build/docker.ts +17 -4
  90. package/src/build/index.ts +42 -0
  91. package/src/build/node/buildJob.ts +54 -0
  92. package/src/build/node/cache.ts +36 -0
  93. package/src/build/node/constants.ts +6 -0
  94. package/src/build/node/index.ts +17 -0
  95. package/src/build/node/meteor.ts +75 -0
  96. package/src/build/node/testJob.ts +80 -0
  97. package/src/build/node/yarn.ts +8 -0
  98. package/src/build/tests/storybook.test.ts +46 -0
  99. package/src/build/types.ts +75 -0
  100. package/src/catladder-gitlab.ts +52 -0
  101. package/src/config/configruedEnvs.test.ts +97 -0
  102. package/src/config/configruedEnvs.ts +56 -0
  103. package/src/config/index.ts +2 -0
  104. package/src/config/readConfig.ts +36 -0
  105. package/src/constants.ts +2 -0
  106. package/src/context/index.ts +194 -0
  107. package/src/defaults/index.ts +6 -0
  108. package/src/deploy/base.ts +101 -0
  109. package/src/deploy/index.ts +19 -0
  110. package/src/deploy/kubernetes/cloudsql.ts +13 -0
  111. package/src/deploy/kubernetes/deployJob.ts +116 -0
  112. package/src/deploy/kubernetes/index.ts +1 -0
  113. package/src/deploy/kubernetes/mongodb.ts +20 -0
  114. package/src/deploy/types.ts +60 -0
  115. package/src/index.ts +8 -0
  116. package/{scripts/magic/tests/createPipeline.test.ts → src/pipeline/createChildPipeline.test.ts} +12 -11
  117. package/src/pipeline/createChildPipeline.ts +51 -0
  118. package/src/pipeline/createJobs.ts +158 -0
  119. package/src/pipeline/index.ts +2 -0
  120. package/src/pipeline/yarnInfo.ts +54 -0
  121. package/src/rules/index.ts +37 -0
  122. package/src/runner/index.ts +10 -0
  123. package/src/types/config.ts +88 -0
  124. package/src/types/context.ts +36 -0
  125. package/src/types/gitlab-types.ts +94 -0
  126. package/{scripts/magic → src}/types/index.ts +0 -0
  127. package/src/utils/index.ts +10 -0
  128. package/tsconfig.json +4 -2
  129. package/Dockerfile +0 -84
  130. package/dist/scripts/createSamplePipeline.d.ts +0 -2
  131. package/dist/scripts/createSamplePipeline.js +0 -14
  132. package/dist/scripts/magic/build/docker.d.ts +0 -2
  133. package/dist/scripts/magic/build/docker.js +0 -68
  134. package/dist/scripts/magic/build/index.d.ts +0 -9
  135. package/dist/scripts/magic/build/index.js +0 -10
  136. package/dist/scripts/magic/build/node.d.ts +0 -5
  137. package/dist/scripts/magic/build/node.js +0 -137
  138. package/dist/scripts/magic/build/types.d.ts +0 -12
  139. package/dist/scripts/magic/build/types.js +0 -7
  140. package/dist/scripts/magic/createChildPipeline/index.d.ts +0 -5
  141. package/dist/scripts/magic/createChildPipeline/index.js +0 -153
  142. package/dist/scripts/magic/createPipeline.d.ts +0 -14
  143. package/dist/scripts/magic/createPipeline.js +0 -80
  144. package/dist/scripts/magic/deploy/index.d.ts +0 -9
  145. package/dist/scripts/magic/deploy/index.js +0 -12
  146. package/dist/scripts/magic/deploy/kubernetes.d.ts +0 -5
  147. package/dist/scripts/magic/deploy/kubernetes.js +0 -62
  148. package/dist/scripts/magic/deploy/types.d.ts +0 -41
  149. package/dist/scripts/magic/deploy/types.js +0 -7
  150. package/dist/scripts/magic/index.d.ts +0 -2
  151. package/dist/scripts/magic/index.js +0 -16
  152. package/dist/scripts/magic/sample-config.d.ts +0 -3
  153. package/dist/scripts/magic/sample-config.js +0 -77
  154. package/dist/scripts/magic/tests/createPipeline.test.d.ts +0 -1
  155. package/dist/scripts/magic/tests/createPipeline.test.js +0 -89
  156. package/dist/scripts/magic/types/config.d.ts +0 -49
  157. package/dist/scripts/magic/types/config.js +0 -17
  158. package/dist/scripts/magic/types/context.d.ts +0 -19
  159. package/dist/scripts/magic/types/context.js +0 -2
  160. package/dist/scripts/magic/types/gitlab-types.d.ts +0 -57
  161. package/dist/scripts/magic/types/gitlab-types.js +0 -2
  162. package/dist/scripts/magic/types/index.js +0 -15
  163. package/dist/scripts/magic.d.ts +0 -2
  164. package/dist/scripts/magic.js +0 -43
  165. package/dist/scripts/monorepoGenerate.d.ts +0 -2
  166. package/dist/scripts/monorepoGenerate.js +0 -164
  167. package/dist/scripts/printAllValues.d.ts +0 -2
  168. package/dist/scripts/printAllValues.js +0 -58
  169. package/dist/scripts/printBuildEnv.d.ts +0 -2
  170. package/dist/scripts/printBuildEnv.js +0 -77
  171. package/dist/scripts/utils/extractAllValues.d.ts +0 -1
  172. package/dist/scripts/utils/extractAllValues.js +0 -78
  173. package/dist/scripts/utils/extractBuildEnv.d.ts +0 -5
  174. package/dist/scripts/utils/extractBuildEnv.js +0 -152
  175. package/dist/scripts/utils/extractValuesFromEnvVars.d.ts +0 -2
  176. package/dist/scripts/utils/extractValuesFromEnvVars.js +0 -63
  177. package/dist/scripts/utils/extractValuesFromMr.d.ts +0 -2
  178. package/dist/scripts/utils/extractValuesFromMr.js +0 -86
  179. package/dist/tests/extractValuesFromMr.test.d.ts +0 -1
  180. package/dist/tests/extractValuesFromMr.test.js +0 -68
  181. package/scripts/createSamplePipeline.ts +0 -10
  182. package/scripts/getCommitInfo +0 -11
  183. package/scripts/kubernetesCreateSecret +0 -9
  184. package/scripts/kubernetesDelete +0 -4
  185. package/scripts/kubernetesDeploy +0 -55
  186. package/scripts/kubernetesEnsureNamespace +0 -3
  187. package/scripts/magic/build/index.ts +0 -16
  188. package/scripts/magic/build/node.ts +0 -130
  189. package/scripts/magic/build/types.ts +0 -17
  190. package/scripts/magic/createChildPipeline/index.ts +0 -176
  191. package/scripts/magic/createPipeline.ts +0 -52
  192. package/scripts/magic/deploy/index.ts +0 -19
  193. package/scripts/magic/deploy/kubernetes.ts +0 -65
  194. package/scripts/magic/deploy/types.ts +0 -29
  195. package/scripts/magic/index.ts +0 -2
  196. package/scripts/magic/sample-config.ts +0 -78
  197. package/scripts/magic/types/config.ts +0 -54
  198. package/scripts/magic/types/context.ts +0 -21
  199. package/scripts/magic/types/gitlab-types.ts +0 -69
  200. package/scripts/magic.ts +0 -51
  201. package/scripts/monorepoGenerate.ts +0 -104
  202. package/scripts/printAllValues.ts +0 -17
  203. package/scripts/printBuildEnv.ts +0 -22
  204. package/scripts/utils/extractAllValues.ts +0 -32
  205. package/scripts/utils/extractBuildEnv.ts +0 -94
  206. package/scripts/utils/extractValuesFromEnvVars.ts +0 -21
  207. package/scripts/utils/extractValuesFromMr.ts +0 -39
  208. package/tests/extractValuesFromMr.test.ts +0 -156
@@ -1,65 +0,0 @@
1
- import { GitlabJobs } from "../types/gitlab-types";
2
- import { Context } from "../types/context";
3
- import { DeployConfigKubernetes, isOfType } from "./types";
4
-
5
- export const createKubernetesDeployJobs = (context: Context): GitlabJobs => {
6
- const deployConfig = context.componentConfig.deploy;
7
- if (!isOfType(deployConfig, "kubernetes")) {
8
- // should not happen
9
- throw new Error("deploy config is not kubernetes");
10
- }
11
-
12
- const defaltKubeValues = {
13
- application: {
14
- hostname: context.environment.hostname,
15
- command: context.componentConfig.build.startCommand,
16
- },
17
- };
18
- const kubeValues = {
19
- ...defaltKubeValues,
20
- ...deployConfig.values,
21
- }; // TODO: merge with some defaults
22
- return [
23
- {
24
- name: "deploy-to-kubernetes",
25
-
26
- job: {
27
- variables: {
28
- ...context.environment.variables,
29
-
30
- // TODO: refactor and unify with other stages
31
- HELM_EXPERIMENTAL_OCI: "1",
32
- IMAGE_PULL_SECRET: `gitlab-registry-${context.componentName}`,
33
- KUBE_VALUES: JSON.stringify(kubeValues),
34
- HELM_GITLAB_CHART_PATH: "catladder/helm-charts",
35
- HELM_GITLAB_CHART_NAME: "the-panter-chart",
36
- COMPONENT_NAME: context.componentName,
37
- // TODO: unify with docker build stage
38
- IMAGE_TAG: "$CI_COMMIT_SHA",
39
- HELM_GITLAB_CHART_VERSION: "3.2.0", // TODO, we could actually just ship the chart directly here
40
- RELEASE_NAME: `${context.fullConfig.customerName}-${context.fullConfig.appName}-$CI_ENVIRONMENT_SLUG`,
41
- },
42
- stage: "deploy",
43
- dependencies: [],
44
- script: [
45
- "kubernetesEnsureNamespace",
46
- "kubernetesCreateSecret",
47
- "kubernetesDeploy",
48
- ],
49
- environment: {
50
- name: context.environment.fullName,
51
- url: context.environment.url,
52
- kubernetes: {
53
- namespace: context.environment.variables.KUBE_NAMESPACE,
54
- },
55
- },
56
- },
57
- },
58
- ];
59
- };
60
-
61
- export const getKubernetesDefault = (): Partial<DeployConfigKubernetes> => {
62
- return {
63
- values: {},
64
- };
65
- };
@@ -1,29 +0,0 @@
1
- export type DeployConfigBase = {};
2
- export type DeployConfigKubernetes = {
3
- type: "kubernetes";
4
- additionalHelmArgs?: string[];
5
- values?: {
6
- application?: {
7
- resources?: {
8
- limits?: {
9
- cpu?: any;
10
- };
11
- requests?: {
12
- cpu?: any;
13
- };
14
- };
15
- } & Record<string, any>;
16
- } & Record<string, any>;
17
- } & DeployConfigBase;
18
-
19
- export type DeployConfigServerless = {
20
- type: "serverless";
21
- };
22
- export type DeployConfig = DeployConfigKubernetes | DeployConfigServerless;
23
-
24
- export const isOfType = <T extends DeployConfig["type"]>(
25
- t: DeployConfig,
26
- type: T
27
- ): t is Extract<DeployConfig, { type: T }> => {
28
- return t.type === type;
29
- };
@@ -1,2 +0,0 @@
1
- export { createPipeline } from "./createPipeline";
2
- export * from "./types";
@@ -1,78 +0,0 @@
1
- import { Config } from "./types/config";
2
-
3
- const sampleConfig: Config = {
4
- customerName: "pan",
5
- appName: "fonsi",
6
- components: {
7
- web: {
8
- dir: "web",
9
- build: {
10
- type: "node",
11
- },
12
- deploy: {
13
- type: "kubernetes",
14
- values: {
15
- application: {
16
- resources: {
17
- limits: {
18
- cpu: "100m",
19
- },
20
- },
21
- },
22
- },
23
- },
24
- vars: {
25
- public: {
26
- GOOGLE_TAG_MANAGER_ID: "1234",
27
- },
28
- fromComponents: {
29
- api: {
30
- API_URL: "ROOT_URL",
31
- },
32
- },
33
- },
34
- env: {
35
- prod: {
36
- hostname: "fudilo.ch",
37
- vars: {},
38
- deploy: {
39
- type: "kubernetes",
40
- values: {
41
- application: {
42
- resources: {
43
- limits: {
44
- cpu: "1000m",
45
- },
46
- },
47
- },
48
- },
49
- },
50
- },
51
- myCustomProd: {
52
- type: "prod",
53
- hostname: "fudilo2.ch",
54
- },
55
- },
56
- },
57
- api: {
58
- env: {
59
- prod: {
60
- hostname: "api.fudilo.ch",
61
- },
62
- myCustomProd: {
63
- type: "prod",
64
- hostname: "api.fudilo2.ch",
65
- },
66
- },
67
- build: {
68
- type: "node",
69
- },
70
- deploy: {
71
- type: "kubernetes",
72
- },
73
- dir: "api",
74
- },
75
- },
76
- };
77
-
78
- export default sampleConfig;
@@ -1,54 +0,0 @@
1
- import { BuildConfig } from "../build/types";
2
- import { DeployConfig } from "../deploy/types";
3
-
4
- export type PipelineTrigger = "mainBranch" | "mr" | "taggedRelease";
5
-
6
- export const ENV_TYPES = {
7
- dev: {
8
- trigger: "mainBranch",
9
- },
10
- review: {
11
- trigger: "mr",
12
- },
13
- prod: {
14
- trigger: "taggedRelease",
15
- },
16
- stage: {
17
- trigger: "taggedRelease",
18
- },
19
- } as const;
20
- type EnvType = keyof typeof ENV_TYPES;
21
-
22
- export type DefaultEnvConfig = {
23
- deploy: DeployConfig;
24
- build: BuildConfig;
25
- vars?: {
26
- public?: Record<string, any>;
27
- secret?: Record<string, string>;
28
- fromComponents?: {
29
- [otherApp: string]: Record<string, string>;
30
- };
31
- };
32
- };
33
- type EnvConfig<E extends EnvType = EnvType> = {
34
- type?: E;
35
- hostname?: string;
36
- } & Partial<DefaultEnvConfig>;
37
-
38
- export type Env = {
39
- dev?: EnvConfig<"dev">;
40
- stage?: EnvConfig<"stage">;
41
- review?: EnvConfig<"review">;
42
- prod?: EnvConfig<"prod">;
43
- } & Record<string, EnvConfig>;
44
-
45
- export type ComponentConfig = {
46
- env?: Env;
47
- dir: string;
48
- } & DefaultEnvConfig;
49
-
50
- export type Config = {
51
- customerName: string;
52
- appName: string;
53
- components: Record<string, ComponentConfig>;
54
- };
@@ -1,21 +0,0 @@
1
- import { ComponentConfig, Config } from "./config";
2
-
3
- export type Environment = {
4
- hostname: string;
5
- fullName: string;
6
- shortName: string;
7
- url: string;
8
- variables: Record<string, string>;
9
- };
10
-
11
- export type CommitInfo = {
12
- refName: string;
13
- refSlug: string;
14
- };
15
- export type Context = {
16
- componentConfig: ComponentConfig;
17
- componentName: string;
18
- fullConfig: Config;
19
- environment: Environment;
20
- commit: CommitInfo;
21
- };
@@ -1,69 +0,0 @@
1
- export type GitlabStage =
2
- | "setup"
3
- | "test"
4
- | "build"
5
- | "deploy"
6
- | "verify"
7
- | "actions";
8
-
9
- export type Artifacts = {
10
- paths: string[];
11
- };
12
- export type GitlabJobCache = {
13
- key: string;
14
- policy?: string;
15
- paths: string[];
16
- };
17
- export type GitlabRule = {
18
- if: string;
19
- when?: "always" | "on_success" | "manual" | "never"; // todo
20
- };
21
- export type Retry = {
22
- max: number;
23
- when: string[];
24
- };
25
-
26
- export type Service = {
27
- name: string;
28
- command: string[];
29
- };
30
-
31
- export type GitlabEnvironment = {
32
- url: string;
33
- name: string;
34
- kubernetes?: {
35
- namespace?: string;
36
- };
37
- };
38
- export type GitlabJobDef = {
39
- stage: GitlabStage;
40
- before_script?: string[];
41
- script: string[];
42
- interruptible?: boolean;
43
- needs?: string[];
44
- rules?: GitlabRule[];
45
- cache?: GitlabJobCache | GitlabJobCache[];
46
- artifacts?: Artifacts;
47
- retry?: Retry;
48
- services?: Service[];
49
- image?: string;
50
- variables?: GitlabVariables;
51
- dependencies?: string[];
52
- environment?: GitlabEnvironment;
53
- allow_failure?: boolean;
54
- };
55
-
56
- export type GitlabVariables = Record<string, string | undefined>;
57
-
58
- export type GitlabJob = {
59
- name: string;
60
- perEnv?: boolean;
61
- job: GitlabJobDef;
62
- };
63
- export type GitlabJobs = GitlabJob[];
64
-
65
- export type GitlabPipeline = {
66
- variables: GitlabVariables;
67
- stages: GitlabStage[];
68
- jobs: GitlabJobs;
69
- };
package/scripts/magic.ts DELETED
@@ -1,51 +0,0 @@
1
- #!/usr/bin/env ts-node
2
-
3
- import { existsSync, writeFileSync } from "fs";
4
- import { parse } from "yaml";
5
- import { PipelineTrigger } from "./magic/types/config";
6
- import { createPipeline } from "./magic/createPipeline";
7
-
8
- const {
9
- CI_MERGE_REQUEST_ID,
10
- CI_COMMIT_TAG,
11
- CI_COMMIT_BRANCH,
12
- CI_DEFAULT_BRANCH,
13
- } = process.env;
14
-
15
- const isDefaultBranch = CI_COMMIT_BRANCH == CI_DEFAULT_BRANCH;
16
- const isHotfixBranch = false; // TODO: $CI_COMMIT_BRANCH =~ /^[0-9]+\.([0-9]+|x)\.x$/
17
- const isMergeRequest = Boolean(CI_MERGE_REQUEST_ID);
18
- const isTaggedRelease = Boolean(CI_COMMIT_TAG);
19
-
20
- const fullPath = (ext: string) => process.cwd() + "/catladder." + ext;
21
- const readConfig = () => {
22
- const found = ["ts", "js", "yml", "yaml"].find((extension) =>
23
- existsSync(fullPath(extension))
24
- );
25
- if (found) {
26
- if (found === "ts" || found === "js") {
27
- return require(fullPath(found)).default;
28
- } else {
29
- return parse(fullPath(found));
30
- }
31
- }
32
- };
33
-
34
- const trigger: PipelineTrigger | null =
35
- isMergeRequest || isHotfixBranch
36
- ? "mr"
37
- : isDefaultBranch
38
- ? "mainBranch"
39
- : isTaggedRelease
40
- ? "taggedRelease"
41
- : null;
42
- if (trigger) {
43
- const config = readConfig();
44
- createPipeline(trigger, config).then((mainPipeline) => {
45
- writeFileSync(`__pipeline.yml`, JSON.stringify(mainPipeline, null, 2), {
46
- encoding: "utf-8",
47
- });
48
- });
49
- } else {
50
- throw new Error("no matching trigger");
51
- }
@@ -1,104 +0,0 @@
1
- #!/usr/bin/env ts-node
2
-
3
- import { readdirSync, readFileSync, lstatSync, existsSync } from "fs";
4
- import { join } from "path";
5
- import { parse } from "yaml";
6
- import yargs from "yargs";
7
-
8
- import { hideBin } from "yargs/helpers";
9
- import { extractBuildEnv } from "./utils/extractBuildEnv";
10
- type Type = "test" | "deploy";
11
- const args = yargs(hideBin(process.argv)).array("env").argv as Record<
12
- string,
13
- unknown
14
- >;
15
-
16
- const thisGitlabCi = parse(
17
- readFileSync(".gitlab-ci.yml", { encoding: "utf-8" })
18
- );
19
-
20
- const subcis = readdirSync(".")
21
- .filter((file) => lstatSync(file).isDirectory())
22
- .map((dir) => ({
23
- dirname: dir,
24
- ci: join(dir, ".gitlab-ci.yml"),
25
- }))
26
- .filter(({ ci }) => existsSync(ci));
27
-
28
- const makeConfig = async ({
29
- subapp,
30
- type,
31
- ci,
32
- env,
33
- }: {
34
- subapp: string;
35
- type: Type;
36
- env: string;
37
- ci: string;
38
- }) => {
39
- const changes = [subapp + "/**/*"];
40
- const rules = [
41
- // same as rules "always", but with `changes` to only trigger changed branches
42
- { if: "$CI_COMMIT_TAG" },
43
- {
44
- if: "$CI_COMMIT_MESSAGE =~ /^chore(release).*/",
45
- when: "never",
46
- },
47
- { if: "$CI_COMMIT_BRANCH =~ /^[0-9]+.([0-9]+|x).x$/" },
48
- {
49
- if: "$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH",
50
- changes,
51
- },
52
- // add it manually when no changes
53
- {
54
- if: "$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH",
55
- when: "manual",
56
- },
57
- { if: "$CI_MERGE_REQUEST_ID", changes },
58
- // add it manually when no changes
59
- { if: "$CI_MERGE_REQUEST_ID", when: "manual" },
60
- ];
61
-
62
- return {
63
- variables: {
64
- // merge
65
- ...thisGitlabCi.variables,
66
- APP_DIR: subapp,
67
- VALUES_DIR: subapp,
68
- COMPONENT_NAME: subapp,
69
- PARENT_PIPELINE_ID: "$CI_PIPELINE_ID",
70
- [type.toUpperCase() + "_ONLY"]: "1",
71
- ...(await extractBuildEnv(subapp, env)),
72
- },
73
- stage: type,
74
- rules: rules,
75
- trigger: {
76
- include: ci,
77
- strategy: "depend",
78
- },
79
- };
80
- };
81
- const generateSubPipeline = async (type: Type, envs: string[]) => {
82
- return await envs.reduce(async (acc, env) => {
83
- return {
84
- ...(await acc),
85
- ...(await subcis.reduce(
86
- async (inneracc, { dirname, ci }) => ({
87
- ...(await inneracc),
88
-
89
- [`${env}-${dirname} ${type}`]: await makeConfig({
90
- subapp: dirname,
91
- ci,
92
- type,
93
- env,
94
- }),
95
- }),
96
- {} as Promise<Record<string, unknown>>
97
- )),
98
- };
99
- }, {} as Promise<Record<string, unknown>>);
100
- };
101
-
102
- generateSubPipeline(args.t as Type, args.env as string[]).then((p) =>
103
- console.log(JSON.stringify(p, null, 2))
104
- );
@@ -1,17 +0,0 @@
1
- #!/usr/bin/env ts-node
2
-
3
- import yargs from "yargs";
4
- import { hideBin } from "yargs/helpers";
5
- import { extractAllValues } from "./utils/extractAllValues";
6
-
7
- const args = yargs(hideBin(process.argv)).argv as Record<string, unknown>;
8
-
9
- extractAllValues(args.d as string, args.e as string)
10
- .then(async (values) => {
11
- console.log(JSON.stringify(values, null, 2) ?? "");
12
- })
13
- .catch((e) => {
14
- console.error("could not extract values", {
15
- error: e,
16
- });
17
- });
@@ -1,22 +0,0 @@
1
- #!/usr/bin/env ts-node
2
-
3
- import { isObject } from "lodash";
4
- import yargs from "yargs";
5
- import { hideBin } from "yargs/helpers";
6
- import { extractBuildEnv } from "./utils/extractBuildEnv";
7
-
8
- const args = yargs(hideBin(process.argv)).argv as Record<string, unknown>;
9
-
10
- extractBuildEnv(args.d as string, args.e as string)
11
- .then(async (values) => {
12
- Object.entries(values).forEach(([key, value]) =>
13
- console.log(
14
- `${key}: "${isObject(value) ? JSON.stringify(value) : value}"`
15
- )
16
- );
17
- })
18
- .catch((e) => {
19
- console.error("could not extract values", {
20
- error: e,
21
- });
22
- });
@@ -1,32 +0,0 @@
1
- import { readFileSync } from "fs";
2
- import { merge } from "lodash";
3
- import { parse } from "yaml";
4
- import { extractValuesFromEnvVars } from "./extractValuesFromEnvVars";
5
- import { extractValuesFromMr } from "./extractValuesFromMr";
6
-
7
- const { CI_PROJECT_ID, CI_MERGE_REQUEST_IID } = process.env;
8
-
9
- const readYaml = (path: string) => {
10
- try {
11
- return parse(readFileSync(path, { encoding: "utf-8" }));
12
- } catch (e) {
13
- return {};
14
- }
15
- };
16
-
17
- export const extractAllValues = async (
18
- appDirectory: string,
19
- env: string | null
20
- ) => {
21
- const valuesFromEnvVars = extractValuesFromEnvVars();
22
- const valuesDefault = readYaml(appDirectory + "/values.yml");
23
- const valuesEnv = env
24
- ? readYaml(appDirectory + "/values-" + env + ".yml")
25
- : {};
26
- const valuesFromMR =
27
- CI_PROJECT_ID && CI_MERGE_REQUEST_IID
28
- ? await extractValuesFromMr(CI_PROJECT_ID, Number(CI_MERGE_REQUEST_IID))
29
- : {};
30
-
31
- return merge({}, valuesFromEnvVars, valuesDefault, valuesEnv, valuesFromMR);
32
- };
@@ -1,94 +0,0 @@
1
- import { extractAllValues } from "./extractAllValues";
2
- import slugify from "slugify";
3
-
4
- // deprecated
5
-
6
- type EnvVars = Record<string, string>;
7
- export const extractBuildEnv = async (
8
- componentName: string,
9
- env: string | null,
10
- visitedReferencedApps: { [app: string]: EnvVars } = {} // this is just to avoid infinit recursion
11
- ): Promise<EnvVars> => {
12
- const values = await extractAllValues(componentName, env);
13
- const {
14
- APP_NAME,
15
- CUSTOMER_NAME,
16
- CI_COMMIT_REF_SLUG,
17
- CI_MERGE_REQUEST_IID,
18
- CI_ENVIRONMENT_SLUG,
19
- } = process.env;
20
-
21
- const isNotMonorepo = componentName === ".";
22
-
23
- const KUBE_APP_NAME = CI_MERGE_REQUEST_IID
24
- ? `${componentName}-${CI_COMMIT_REF_SLUG}`
25
- : componentName;
26
- const KUBE_APP_SLUG = isNotMonorepo
27
- ? CI_ENVIRONMENT_SLUG
28
- : slugify(KUBE_APP_NAME);
29
- const CANONICAL_URL = `https://${APP_NAME}-${KUBE_APP_SLUG}.${CUSTOMER_NAME}.panter.cloud`;
30
- const defaultBuildEnv = {
31
- KUBE_APP_NAME,
32
- CANONICAL_URL,
33
- IMAGE_PULL_SECRET: `gitlab-registry-${componentName}`,
34
- ROOT_URL: values?.application?.host || CANONICAL_URL,
35
- };
36
- const publicBuildEnv = values?.buildEnv?.public ?? {};
37
- const merged = {
38
- ...defaultBuildEnv,
39
- ...publicBuildEnv,
40
- };
41
- if (process.env.DEBUG) {
42
- console.log({ values, defaultBuildEnv, publicBuildEnv });
43
- }
44
- // extract referenced build env (only works in monorepo
45
- // like this
46
- /*
47
- fromComponents:
48
- vendure:
49
- API_URL: ROOT_URL
50
- */
51
- const fromComponents: {
52
- [otherApp: string]: {
53
- [ourKey: string]: string;
54
- };
55
- } = values?.buildEnv?.fromComponents ?? {};
56
- const referencedBuildEnv = await Object.entries(fromComponents).reduce(
57
- async (acc, [otherApp, mapping]) => {
58
- if (process.env.DEBUG) {
59
- console.log("extract reference build env", otherApp, mapping);
60
- }
61
- if (!mapping) {
62
- return await acc;
63
- }
64
-
65
- // prevent infinit loop
66
- const otherBuildEnv =
67
- visitedReferencedApps[otherApp] ??
68
- (await extractBuildEnv(otherApp, env, {
69
- ...visitedReferencedApps,
70
- [componentName]: merged,
71
- }));
72
- if (process.env.DEBUG) {
73
- console.log({ otherBuildEnv });
74
- }
75
- // resolve mapping
76
- return {
77
- ...(await acc),
78
- ...Object.entries(mapping).reduce(
79
- (inneracc, [ourKey, otherKey]) => ({
80
- ...inneracc,
81
- [ourKey]: otherBuildEnv[otherKey],
82
- }),
83
- {} as EnvVars
84
- ),
85
- };
86
- },
87
- {} as Promise<EnvVars>
88
- );
89
-
90
- return {
91
- ...merged,
92
- ...referencedBuildEnv,
93
- };
94
- };
@@ -1,21 +0,0 @@
1
- #!/usr/bin/env ts-node
2
- import { set } from "lodash";
3
-
4
- export const extractValuesFromEnvVars = () => {
5
- const values = {};
6
-
7
- const PREFIX = "DEFAULT_VALUE_";
8
- for (const [key, value] of Object.entries(process.env).filter(([key]) =>
9
- key.startsWith(PREFIX)
10
- )) {
11
- const path = key.replace(PREFIX, "").split("_");
12
- let theValue;
13
- try {
14
- theValue = JSON.parse(value ?? "");
15
- } catch (e) {
16
- theValue = value;
17
- }
18
- set(values, path, theValue);
19
- }
20
- return values;
21
- };