@catladder/pipeline 1.82.2 → 1.83.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.
@@ -0,0 +1,54 @@
1
+ import type { Config, DeployConfigCloudRunCloudSql } from "../src";
2
+ import { createAllPipelines } from "./__utils__/helpers";
3
+
4
+ const CLOUD_SQL: DeployConfigCloudRunCloudSql = {
5
+ type: "unmanaged",
6
+ instanceConnectionName: "projectId:region:instancename",
7
+ dbUser: "my-user",
8
+ };
9
+ const config: Config = {
10
+ appName: "test-app",
11
+ customerName: "pan",
12
+ components: {
13
+ api: {
14
+ dir: "api",
15
+ build: {
16
+ type: "node",
17
+ },
18
+ deploy: {
19
+ type: "google-cloudrun",
20
+ projectId: "google-project-id",
21
+ region: "europe-west6",
22
+ cloudSql: CLOUD_SQL,
23
+ },
24
+ },
25
+ worker: {
26
+ dir: "api",
27
+ build: {
28
+ type: "node",
29
+ buildCommand: "yarn build:worker",
30
+ },
31
+ deploy: {
32
+ type: "google-cloudrun",
33
+ projectId: "google-project-id",
34
+ region: "europe-west6",
35
+ cloudSql: {
36
+ ...CLOUD_SQL,
37
+ // override the db base name to the other component's name
38
+ // if ommited, this defaults to the component's name
39
+ dbBaseName: "api",
40
+ },
41
+ },
42
+ vars: {
43
+ public: {
44
+ // need to reference the password
45
+ DB_PASSWORD: "${api:DB_PASSWORD}",
46
+ },
47
+ },
48
+ },
49
+ },
50
+ };
51
+
52
+ it("matches snapshot", async () => {
53
+ expect(await createAllPipelines(config)).toMatchSnapshot();
54
+ });
@@ -0,0 +1,94 @@
1
+ import type { Config } from "../src";
2
+ import { createAllPipelines } from "./__utils__/helpers";
3
+
4
+ // the image version should match your `.ruby-version`
5
+ const RAILS_TEST_STAGE_IMAGE = "ruby:3.2.1"
6
+
7
+ const config: Config = {
8
+ appName: "test-app",
9
+ customerName: "pan",
10
+ components: {
11
+ app: {
12
+ dir: ".",
13
+ vars: {
14
+ public: {
15
+ RAILS_ENV: "production",
16
+ },
17
+ secret: [
18
+ "SECRET_KEY_BASE",
19
+ ]
20
+ },
21
+ build: {
22
+ type: "rails",
23
+ test: {
24
+ // if operating system dependencies are required you can either
25
+ // - build a dedicated image
26
+ // - use `command` to apt-get install them before running the test command
27
+ jobImage: RAILS_TEST_STAGE_IMAGE,
28
+ },
29
+ lint: {
30
+ jobImage: RAILS_TEST_STAGE_IMAGE,
31
+ },
32
+ audit: {
33
+ jobImage: RAILS_TEST_STAGE_IMAGE,
34
+ },
35
+ cnbBuilder: {
36
+ buildVars: { SECRET_KEY_BASE: "dummy-value" },
37
+ },
38
+ },
39
+ deploy: {
40
+ type: "kubernetes",
41
+ cluster: {
42
+ name: "some-cluster-name",
43
+ region: "europe-west6",
44
+ projectId: "some-project-id",
45
+ type: "gcloud",
46
+ domainCanonical: "panter.cloud",
47
+ },
48
+ values: {
49
+ application: {
50
+ // if deployed to Google Cloud Run use a gem like cloudtasker instead of a permanently running expensive worker
51
+ worker: {
52
+ enabled: true,
53
+ command: "launcher bundle exec rake jobs:work",
54
+ },
55
+ },
56
+ jobs: {
57
+ "db-migrate": {
58
+ hook: "post-install,post-upgrade",
59
+ command:
60
+ "launcher bundle exec rake db:migrate",
61
+ },
62
+ },
63
+ },
64
+ },
65
+ env: {
66
+ review: {
67
+ deploy: {
68
+ values: {
69
+ jobs: {
70
+ "db-prepare-seed": {
71
+ hook: "post-install",
72
+ command:
73
+ "launcher bundle exec rake db:prepare db:seed",
74
+ },
75
+ "db-migrate": {
76
+ hook: "post-upgrade",
77
+ command:
78
+ "launcher bundle exec rake db:migrate",
79
+ },
80
+ },
81
+ },
82
+ },
83
+ },
84
+ prod: {
85
+ host: "my-fancy-website.com"
86
+ }
87
+ },
88
+ },
89
+ },
90
+ };
91
+
92
+ it("matches snapshot", async () => {
93
+ expect(await createAllPipelines(config)).toMatchSnapshot();
94
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@catladder/pipeline",
3
- "version": "1.82.2",
3
+ "version": "1.83.1",
4
4
  "scripts": {
5
5
  "build:tsc": "yarn tsc",
6
6
  "build": "yarn build:compile && yarn build:inline-variables && yarn build:bundle",
@@ -51,6 +51,12 @@ export const BUILD_TYPES: BuildTypes = {
51
51
  },
52
52
  rails: {
53
53
  jobs: createRailsJobs,
54
- defaults: () => ({}),
54
+ defaults: () => ({
55
+ startCommand: "/cnb/process/web",
56
+ cnbBuilder: {
57
+ image: "heroku/buildpacks:20",
58
+ packVersion: "0.28.0",
59
+ }
60
+ }),
55
61
  },
56
62
  };
@@ -1,27 +1,34 @@
1
1
  import type { Context } from "../..";
2
+ import { isOfBuildType } from "../types";
2
3
  import type { CatladderJob } from "../../types/jobs";
3
4
  import { createDockerBuildJobBase, gitlabDockerLogin } from "../docker";
4
5
 
5
6
  export const createRailsBuildJobs = (context: Context): CatladderJob[] => {
7
+ const buildConfig = context.componentConfig.build
8
+ if (!isOfBuildType(buildConfig, "rails")) {
9
+ // should not happen
10
+ throw new Error("build type is not rails");
11
+ }
12
+
13
+ const cnbConf = buildConfig.cnbBuilder;
14
+
15
+ // backwards compatabilty with CNB_ENV_VARS
16
+ // TODO: remove when all projects are migrated
17
+ const packEnvArgs = buildConfig.extraVars?.CNB_ENV_VARS?.split(" ").map(v => `--env '${v}'`)
18
+ ?? Object.entries(cnbConf?.buildVars ?? {}).map(([k, v]) => `--env '${k}${v ? `=${v}` : ""}'`)
19
+
6
20
  return [
7
21
  createDockerBuildJobBase(context, {
8
- variables: {
9
- CNB_BUILDER: "heroku/buildpacks:18",
10
- CNB_ENV_VARS: "BUNDLE_GIT__PANTER__CH",
11
- CNB_EXTRA_ARGS: "",
12
- CNB_PACK_VERSION: "0.20.0",
13
- ...context.componentConfig.build.extraVars,
14
- },
22
+ variables: context.componentConfig.build.extraVars,
15
23
  // custom script
16
24
  script: [
17
25
  gitlabDockerLogin,
18
26
  `docker pull $DOCKER_CACHE_IMAGE || true`,
19
- `wget --output-document=- https://github.com/buildpacks/pack/releases/download/v$CNB_PACK_VERSION/pack-v$CNB_PACK_VERSION-linux.tgz | tar -zx --directory /usr/local/bin pack`,
27
+ `wget --output-document=- https://github.com/buildpacks/pack/releases/download/v${cnbConf?.packVersion}/pack-v${cnbConf?.packVersion}-linux.tgz | tar -zx --directory /usr/local/bin pack`,
20
28
  `chmod +x /usr/local/bin/pack`,
21
29
  // replace private git ssh gem sources with https to make bundler with credentials via env var work
22
30
  `sed --in-place 's|git@\\([^:]*\\):|https://\\1/|g' Gemfile Gemfile.lock`,
23
- `for v in $CNB_ENV_VARS; do env_args="$env_args --env $v"; done`,
24
- `pack build $DOCKER_IMAGE:$DOCKER_IMAGE_TAG --builder $CNB_BUILDER --publish --cache-image $DOCKER_CACHE_IMAGE $env_args $CNB_EXTRA_ARGS`,
31
+ `pack build "$DOCKER_IMAGE:$DOCKER_IMAGE_TAG" --builder '${cnbConf?.image}' --publish --cache-image "$DOCKER_CACHE_IMAGE" ${packEnvArgs.join(" ")} ${cnbConf?.packExtraArgs?.join(" ") ?? ""}`
25
32
  ],
26
33
  }),
27
34
  ];
@@ -1,5 +1,5 @@
1
- import { Context } from "../../types";
2
- import { CatladderJob } from "../../types/jobs";
1
+ import type { Context } from "../../types";
2
+ import type { CatladderJob } from "../../types/jobs";
3
3
  import { createRailsBuildJobs } from "./build";
4
4
  import { createRailsTestJobs } from "./test";
5
5
 
@@ -118,6 +118,33 @@ export type BuildConfigCustom = BuildConfigBase & {
118
118
 
119
119
  export type BuildConfigRails = BuildConfigBase & {
120
120
  type: "rails";
121
+ cnbBuilder?: {
122
+ /**
123
+ * The Cloud Native Buildpacks builder image to use.
124
+ * See e.g. https://github.com/heroku/builder or others.
125
+ * Default: heroku/buildpacks:20
126
+ */
127
+ image?: string;
128
+ /**
129
+ * The version of the Cloud Native Buildpacks pack command to use.
130
+ * See https://buildpacks.io/docs/tools/pack/
131
+ * Default: 0.28.0
132
+ */
133
+ packVersion?: string;
134
+ /**
135
+ * Additional command arguments passed to the pack command.
136
+ * See https://buildpacks.io/docs/tools/pack/
137
+ */
138
+ packExtraArgs?: string[];
139
+ /**
140
+ * Variables needed for Bundler and Rails asset precompilation.
141
+ *
142
+ * Specify a key with the value `undefined` to inherit it from the EnvVars.
143
+ *
144
+ * See https://github.com/rails/rails/issues/32947
145
+ */
146
+ buildVars?: Record<string, string | undefined>
147
+ }
121
148
  };
122
149
 
123
150
  export type BuildConfigStorybook = BuildConfigNodeBase & {
@@ -31,6 +31,7 @@ export const getEnvironment = async (
31
31
  url,
32
32
  },
33
33
  fullName: envContext.fullName,
34
+ slugPrefix: envContext.environmentSlugPrefix,
34
35
  slug: envContext.environmentSlug,
35
36
  shortName: env,
36
37
  envVars,
@@ -5,17 +5,16 @@ import type { EnvironmentContext } from "../types/environmentContext";
5
5
  import { getEnvConfig } from "./getEnvConfig";
6
6
  import { getEnvType } from "./getEnvType";
7
7
 
8
- const getEnvironmentSlug = (
8
+ const getEnvironmentSlugPrefix = (
9
9
  envConfig: EnvConfigWithComponent,
10
10
  env: string,
11
- componentName: string,
12
11
  commitInfo?: CommitInfo
13
12
  ) => {
14
13
  const envType = getEnvType(env, envConfig);
15
14
 
16
15
  return envType === "review" && commitInfo
17
- ? `${env}-${commitInfo.reviewSlug}-${componentName}`
18
- : `${env}-${componentName}`;
16
+ ? `${env}-${commitInfo.reviewSlug}`
17
+ : `${env}`;
19
18
  };
20
19
 
21
20
  export const getEnvironmentContext = (
@@ -27,13 +26,13 @@ export const getEnvironmentContext = (
27
26
  const envConfigRaw = getEnvConfig(config, componentName, env);
28
27
  const envType = getEnvType(env, envConfigRaw);
29
28
 
30
- const environmentSlug = getEnvironmentSlug(
29
+ const environmentSlugPrefix = getEnvironmentSlugPrefix(
31
30
  envConfigRaw,
32
31
  env,
33
- componentName,
34
32
  commitInfo
35
33
  );
36
34
 
35
+ const environmentSlug = `${environmentSlugPrefix}-${componentName}`;
37
36
  const gitlabEnvironmentName =
38
37
  envType === "review" && commitInfo
39
38
  ? `${env}/${commitInfo.refName}/${componentName}`
@@ -45,6 +44,7 @@ export const getEnvironmentContext = (
45
44
  envConfigRaw,
46
45
  deployConfigRaw: envConfigRaw.deploy,
47
46
  buildConfigRaw: envConfigRaw.build,
47
+ environmentSlugPrefix,
48
48
  environmentSlug,
49
49
  gitlabEnvironmentName,
50
50
  fullName,
@@ -12,7 +12,7 @@ export const GCLOUD_RUN_CANONICAL_HOST_SUFFIX =
12
12
 
13
13
  const getCloudSqlVariables = ({
14
14
  deployConfigRaw,
15
- environmentSlug,
15
+ environmentSlugPrefix,
16
16
  env,
17
17
  componentName,
18
18
  fullConfig,
@@ -21,7 +21,8 @@ const getCloudSqlVariables = ({
21
21
  const DB_NAME = [
22
22
  deployConfigRaw.cloudSql.dbNamePrefix ??
23
23
  `${fullConfig.customerName}-${fullConfig.appName}`,
24
- environmentSlug,
24
+ environmentSlugPrefix,
25
+ deployConfigRaw.cloudSql.dbBaseName ?? componentName,
25
26
  ]
26
27
  .filter(Boolean)
27
28
  .join("-");
@@ -46,11 +46,16 @@ export type DeployConfigCloudRunCloudSql = {
46
46
  dbUser?: string;
47
47
 
48
48
  /**
49
- * the prefix of the database, the full db name is this plus the environment slug
49
+ * the prefix of the database, the full db name is this plus the environment slug prefix plus the componentName
50
50
  *
51
51
  * defaults to customerName-appName
52
52
  */
53
53
  dbNamePrefix?: string | false;
54
+
55
+ /**
56
+ * the base name of the db, defaults to the componentName
57
+ */
58
+ dbBaseName?: string;
54
59
  /**
55
60
  * whether to delete the database if the environment is stopped
56
61
  * defaults to true for review envs, to false for every other environment
@@ -18,6 +18,7 @@ export type Environment = {
18
18
  url: string;
19
19
  };
20
20
  shortName: string;
21
+ slugPrefix: string;
21
22
  slug: string;
22
23
  /**
23
24
  * env vars contain all build-time env vars. secrets have to be resolved (they are stored in gitlab)
@@ -16,6 +16,13 @@ export type EnvironmentContext<
16
16
  envType: EnvType;
17
17
  componentName: string;
18
18
  fullName: string;
19
+ /**
20
+ * the environment slug without component name.
21
+ */
22
+ environmentSlugPrefix: string;
23
+ /**
24
+ * the full environment slug, including the componentName
25
+ */
19
26
  environmentSlug: string;
20
27
  gitlabEnvironmentName: string;
21
28