@mayurajs/deploy-cloudrun 0.0.0-stage → 1.2.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/LICENSE +201 -0
- package/NOTICE +8 -0
- package/README.md +37 -2
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +90 -0
- package/dist/index.js.map +1 -0
- package/package.json +43 -4
- package/src/index.ts +101 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Mayura
|
|
2
|
+
Copyright 2026 The Mayura Authors
|
|
3
|
+
|
|
4
|
+
This product includes software developed by the Mayura contributors.
|
|
5
|
+
|
|
6
|
+
Third-party dependencies retain their own copyright and license terms. Exact
|
|
7
|
+
resolved versions are recorded in pnpm-lock.yaml and reviewed by the release
|
|
8
|
+
artifact and dependency qualification procedures.
|
package/README.md
CHANGED
|
@@ -1,3 +1,38 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mayurajs/deploy-cloudrun
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[Google Cloud Run](https://cloud.google.com/run) as a `mayura deploy` target. A migration job runs to completion,
|
|
4
|
+
then the server and an always-on worker deploy as services, all from one image, with gcloud.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
npm install --save-dev @mayurajs/deploy-cloudrun
|
|
8
|
+
mayura deploy init --target cloudrun --apply
|
|
9
|
+
# In mayura.deploy.json: "image" is an Artifact Registry repository and targets.cloudrun.project is the project id.
|
|
10
|
+
mayura deploy --target cloudrun # prints the plan and its digest
|
|
11
|
+
mayura deploy --target cloudrun --apply --confirm <digest>
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
A release runs these steps, stopping at the first failure:
|
|
15
|
+
|
|
16
|
+
1. `gcloud projects describe`, so a missing login or project stops it.
|
|
17
|
+
2. The image is built with Cloud Build, or with your Docker and pushed (`build: "local"`).
|
|
18
|
+
3. The `<name>-migrate` job is updated to the release's image, then run and waited for (`--execute-now --wait`, no
|
|
19
|
+
retries). A failed migration stops the release.
|
|
20
|
+
4. `<name>-server` deploys with CPU always allocated and at least one instance, since agent runs continue after a
|
|
21
|
+
response. It gets a `/readyz` startup probe and a one-hour request timeout for streams.
|
|
22
|
+
5. `<name>-worker` deploys as an internal, non-public service that serves only its probes, also always running.
|
|
23
|
+
|
|
24
|
+
Notes:
|
|
25
|
+
|
|
26
|
+
- **Secrets** come from Secret Manager with `--set-secrets`. By default, `DATABASE_URL` and every name in `env` read
|
|
27
|
+
a secret of the same name at `latest`; map others with `secrets`, such as `{ "OPENAI_API_KEY": "openai-key:3" }`.
|
|
28
|
+
Values never appear in a plan.
|
|
29
|
+
- **Settings** in `mayura.deploy.json` under `targets.cloudrun`:
|
|
30
|
+
- `project`, and `region` (`us-central1`);
|
|
31
|
+
- `build` (`cloud-build` or `local`) and `secrets`;
|
|
32
|
+
- `cloudSqlInstance` (`project:region:instance`, for the Cloud SQL connector) and `serviceAccount`;
|
|
33
|
+
- `public` (`true`: the server authenticates requests itself);
|
|
34
|
+
- `serverMinInstances` and `workerInstances` (1 each).
|
|
35
|
+
- **Credentials:** gcloud uses its own login (`gcloud auth login`, or a service account in CI); Mayura reads neither.
|
|
36
|
+
On Windows gcloud is a `.cmd` script, which `mayura deploy` runs through cmd.exe with checked, quoted arguments.
|
|
37
|
+
|
|
38
|
+
See [Deploy with mayura deploy](https://mayurajs.com/docs/cli/deploy/). Apache-2.0.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** Where a release's image is built: by Cloud Build, or by your local Docker and pushed. */
|
|
2
|
+
export type CloudRunBuild = 'cloud-build' | 'local';
|
|
3
|
+
export interface CloudRunSettings {
|
|
4
|
+
/** The Google Cloud project id; a release needs it. */
|
|
5
|
+
readonly project?: string;
|
|
6
|
+
/** The Cloud Run region, such as `us-central1`. */
|
|
7
|
+
readonly region: string;
|
|
8
|
+
readonly build: CloudRunBuild;
|
|
9
|
+
/**
|
|
10
|
+
* Environment variables taken from Secret Manager, as `NAME: 'secret:version'`. By default `DATABASE_URL` and every
|
|
11
|
+
* name in `env` come from a secret of the same name, at `latest`.
|
|
12
|
+
*/
|
|
13
|
+
readonly secrets: Readonly<Record<string, string>>;
|
|
14
|
+
/** A Cloud SQL instance (`project:region:instance`) to connect over the Cloud SQL connector. */
|
|
15
|
+
readonly cloudSqlInstance?: string;
|
|
16
|
+
/** The service account the services and the migration job run as. */
|
|
17
|
+
readonly serviceAccount?: string;
|
|
18
|
+
/** Whether anyone may call the server (it authenticates requests itself); `true` by default. */
|
|
19
|
+
readonly public: boolean;
|
|
20
|
+
/** Instances kept warm: the server's minimum (1) and the worker's count (1). */
|
|
21
|
+
readonly serverMinInstances: number;
|
|
22
|
+
readonly workerInstances: number;
|
|
23
|
+
}
|
|
24
|
+
/** Deploys to Google Cloud Run with gcloud: a migration job, then the server and an always-on worker as services. */
|
|
25
|
+
export declare const cloudRunTarget: import("mayura/cli/deploy").DeployTarget<CloudRunSettings>;
|
|
26
|
+
export default cloudRunTarget;
|
|
27
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,4FAA4F;AAC5F,MAAM,MAAM,aAAa,GAAG,aAAa,GAAG,OAAO,CAAC;AAEpD,MAAM,WAAW,gBAAgB;IAC/B,uDAAuD;IACvD,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,mDAAmD;IACnD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC;IAC9B;;;OAGG;IACH,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IACnD,gGAAgG;IAChG,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IACnC,qEAAqE;IACrE,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;IACjC,gGAAgG;IAChG,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,gFAAgF;IAChF,QAAQ,CAAC,kBAAkB,EAAE,MAAM,CAAC;IACpC,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;CAClC;AAqCD,qHAAqH;AACrH,eAAO,MAAM,cAAc,4DAkCzB,CAAC;eAEY,cAAc"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { MayuraError } from 'mayura';
|
|
2
|
+
import { defineDeployTarget, dockerfile, dockerignore, imageSteps } from 'mayura/cli/deploy';
|
|
3
|
+
const allowed = ['project', 'region', 'build', 'secrets', 'cloudSqlInstance', 'serviceAccount', 'public', 'serverMinInstances', 'workerInstances'];
|
|
4
|
+
const projectId = /^[a-z][a-z0-9-]{4,28}[a-z0-9]$/u;
|
|
5
|
+
const region = /^[a-z]+-[a-z]+\d$/u;
|
|
6
|
+
const envName = /^[A-Z_][A-Z0-9_]{0,127}$/u;
|
|
7
|
+
const secretRef = /^[A-Za-z0-9_-]{1,255}:(?:latest|\d{1,10})$/u;
|
|
8
|
+
function settings(value, project) {
|
|
9
|
+
const raw = (value ?? {});
|
|
10
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw) || Object.keys(raw).some(key => !allowed.includes(key)))
|
|
11
|
+
throw new MayuraError('INVALID_CONFIG', `targets.cloudrun allows only ${allowed.join(', ')}.`);
|
|
12
|
+
const id = raw['project'];
|
|
13
|
+
const where = raw['region'] ?? 'us-central1';
|
|
14
|
+
const build = raw['build'] ?? 'cloud-build';
|
|
15
|
+
if (id !== undefined && (typeof id !== 'string' || !projectId.test(id)))
|
|
16
|
+
throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.project must be a Google Cloud project id.');
|
|
17
|
+
if (typeof where !== 'string' || !region.test(where))
|
|
18
|
+
throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.region must be a Cloud Run region, such as us-central1.');
|
|
19
|
+
if (build !== 'cloud-build' && build !== 'local')
|
|
20
|
+
throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.build must be cloud-build or local.');
|
|
21
|
+
const secrets = Object.fromEntries(['DATABASE_URL', ...project.env].map(name => [name, `${name}:latest`]));
|
|
22
|
+
const configured = raw['secrets'];
|
|
23
|
+
if (configured !== undefined) {
|
|
24
|
+
if (!configured || typeof configured !== 'object' || Array.isArray(configured))
|
|
25
|
+
throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.secrets maps variable names to secret:version.');
|
|
26
|
+
for (const [name, reference] of Object.entries(configured)) {
|
|
27
|
+
if (!envName.test(name) || typeof reference !== 'string' || !secretRef.test(reference))
|
|
28
|
+
throw new MayuraError('INVALID_CONFIG', `targets.cloudrun.secrets.${name} must be a Secret Manager reference such as openai-key:latest.`);
|
|
29
|
+
secrets[name] = reference;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const instance = raw['cloudSqlInstance'];
|
|
33
|
+
const account = raw['serviceAccount'];
|
|
34
|
+
const isPublic = raw['public'] ?? true;
|
|
35
|
+
if (instance !== undefined && (typeof instance !== 'string' || !/^[a-z][a-z0-9-]{4,28}[a-z0-9]:[a-z]+-[a-z]+\d:[a-z][a-z0-9-]{0,95}$/u.test(instance)))
|
|
36
|
+
throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.cloudSqlInstance must be project:region:instance.');
|
|
37
|
+
if (account !== undefined && (typeof account !== 'string' || !/^[a-z][a-z0-9-]{4,29}@[a-z0-9.-]+\.iam\.gserviceaccount\.com$/u.test(account)))
|
|
38
|
+
throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.serviceAccount must be a service account email.');
|
|
39
|
+
if (typeof isPublic !== 'boolean')
|
|
40
|
+
throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.public must be true or false.');
|
|
41
|
+
const count = (name, fallback) => {
|
|
42
|
+
const item = raw[name] ?? fallback;
|
|
43
|
+
if (typeof item !== 'number' || !Number.isSafeInteger(item) || item < 1 || item > 100)
|
|
44
|
+
throw new MayuraError('INVALID_CONFIG', `targets.cloudrun.${name} must be from 1 to 100.`);
|
|
45
|
+
return item;
|
|
46
|
+
};
|
|
47
|
+
return { ...(id === undefined ? {} : { project: id }), region: where, build, secrets, ...(instance === undefined ? {} : { cloudSqlInstance: instance }),
|
|
48
|
+
...(account === undefined ? {} : { serviceAccount: account }), public: isPublic, serverMinInstances: count('serverMinInstances', 1), workerInstances: count('workerInstances', 1) };
|
|
49
|
+
}
|
|
50
|
+
/** Deploys to Google Cloud Run with gcloud: a migration job, then the server and an always-on worker as services. */
|
|
51
|
+
export const cloudRunTarget = defineDeployTarget({
|
|
52
|
+
id: 'cloudrun', description: 'Deploy to Google Cloud Run with gcloud: a migration job, then the server and an always-on worker as services.', tools: ['gcloud', 'docker'],
|
|
53
|
+
settings,
|
|
54
|
+
files: ({ project }) => ({ Dockerfile: dockerfile(project), '.dockerignore': dockerignore }),
|
|
55
|
+
plan: async ({ project, settings: run, release, readFile }) => {
|
|
56
|
+
await readFile('Dockerfile');
|
|
57
|
+
if (run.project === undefined)
|
|
58
|
+
throw new MayuraError('INVALID_CONFIG', 'Set targets.cloudrun.project in mayura.deploy.json to the Google Cloud project id.');
|
|
59
|
+
const gcp = run.project;
|
|
60
|
+
if (release.image === undefined)
|
|
61
|
+
throw new MayuraError('INVALID_CONFIG', 'Cloud Run deploys an image: set "image" in mayura.deploy.json to an Artifact Registry repository, such as us-central1-docker.pkg.dev/acme/apps/refunds.');
|
|
62
|
+
const image = release.image;
|
|
63
|
+
const where = ['--project', gcp, '--region', run.region];
|
|
64
|
+
const shared = [
|
|
65
|
+
`--image=${image}`, ...(Object.keys(run.secrets).length ? [`--set-secrets=${Object.entries(run.secrets).map(([name, reference]) => `${name}=${reference}`).join(',')}`] : []),
|
|
66
|
+
...(run.cloudSqlInstance === undefined ? [] : [`--set-cloudsql-instances=${run.cloudSqlInstance}`]), ...(run.serviceAccount === undefined ? [] : [`--service-account=${run.serviceAccount}`]),
|
|
67
|
+
];
|
|
68
|
+
const probe = (port) => `--startup-probe=httpGet.path=/readyz,httpGet.port=${port},periodSeconds=5,failureThreshold=24`;
|
|
69
|
+
const build = run.build === 'local' ? imageSteps(release, 'cloudrun')
|
|
70
|
+
: [{ id: 'build-image', description: `Build ${image} with Cloud Build`, tool: 'gcloud', args: ['builds', 'submit', '--project', gcp, `--tag=${image}`, '--quiet', '.'] }];
|
|
71
|
+
return [
|
|
72
|
+
{ id: 'check-project', description: `Check that gcloud can reach ${gcp}`, tool: 'gcloud', args: ['projects', 'describe', gcp, '--format=none'] },
|
|
73
|
+
...build,
|
|
74
|
+
// One job, updated to the release's image and run to completion; a failure stops the release.
|
|
75
|
+
{ id: 'migrate', description: 'Run the migration job and wait for it', tool: 'gcloud',
|
|
76
|
+
args: ['run', 'jobs', 'deploy', `${project.name}-migrate`, ...where, ...shared, `--args=migrate,--app,${project.app}`, '--max-retries=0', '--task-timeout=1800s', '--execute-now', '--wait', '--quiet'] },
|
|
77
|
+
// CPU stays allocated and instances stay warm: agent runs continue after a response.
|
|
78
|
+
{ id: 'deploy-server', description: 'Deploy the server and wait until it is ready', tool: 'gcloud',
|
|
79
|
+
args: ['run', 'deploy', `${project.name}-server`, ...where, ...shared, `--args=serve,--app,${project.app}`, `--port=${project.port}`, '--no-cpu-throttling',
|
|
80
|
+
`--min-instances=${run.serverMinInstances}`, '--timeout=3600s', probe(project.port), run.public ? '--allow-unauthenticated' : '--no-allow-unauthenticated', '--quiet'] },
|
|
81
|
+
// The worker serves only its probes: internal, never public, always running.
|
|
82
|
+
{ id: 'deploy-worker', description: 'Deploy the always-on worker and wait until it is ready', tool: 'gcloud',
|
|
83
|
+
args: ['run', 'deploy', `${project.name}-worker`, ...where, ...shared, `--args=worker,--app,${project.app},--probe-host,0.0.0.0,--probe-port,${project.probePort}`,
|
|
84
|
+
`--port=${project.probePort}`, '--no-cpu-throttling', `--min-instances=${run.workerInstances}`, `--max-instances=${run.workerInstances}`, '--ingress=internal',
|
|
85
|
+
'--no-allow-unauthenticated', probe(project.probePort), '--quiet'] },
|
|
86
|
+
];
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
export default cloudRunTarget;
|
|
90
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAmC,MAAM,QAAQ,CAAC;AACtE,OAAO,EAAE,kBAAkB,EAAE,UAAU,EAAE,YAAY,EAAE,UAAU,EAAuC,MAAM,mBAAmB,CAAC;AA2BlI,MAAM,OAAO,GAAG,CAAC,SAAS,EAAE,QAAQ,EAAE,OAAO,EAAE,SAAS,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,QAAQ,EAAE,oBAAoB,EAAE,iBAAiB,CAAC,CAAC;AACnJ,MAAM,SAAS,GAAG,iCAAiC,CAAC;AACpD,MAAM,MAAM,GAAG,oBAAoB,CAAC;AACpC,MAAM,OAAO,GAAG,2BAA2B,CAAC;AAC5C,MAAM,SAAS,GAAG,6CAA6C,CAAC;AAEhE,SAAS,QAAQ,CAAC,KAA4B,EAAE,OAAsB;IACpE,MAAM,GAAG,GAAG,CAAC,KAAK,IAAI,EAAE,CAAe,CAAC;IACxC,IAAI,CAAC,GAAG,IAAI,OAA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I,SAAS,EAAE,GAAG,KAAK,EAAE,GAAG,MAAM,EAAE,sBAAsB,OAAO,CAAC,GAAG,EAAE,EAAE,UAAU,OAAO,CAAC,IAAI,EAAE,EAAE,qBAAqB;oBACzJ,mBAAmB,GAAG,CAAC,kBAAkB,EAAE,EAAE,iBAAiB,EAAE,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,yBAAyB,CAAC,CAAC,CAAC,4BAA4B,EAAE,SAAS,CAAC,EAAE;YAC5K,6EAA6E;YAC7E,EAAE,EAAE,EAAE,eAAe,EAAE,WAAW,EAAE,wDAAwD,EAAE,IAAI,EAAE,QAAQ;gBAC1G,IAAI,EAAE,CAAC,KAAK,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,IAAI,SAAS,EAAE,GAAG,KAAK,EAAE,GAAG,MAAM,EAAE,uBAAuB,OAAO,CAAC,GAAG,sCAAsC,OAAO,CAAC,SAAS,EAAE;oBAChK,UAAU,OAAO,CAAC,SAAS,EAAE,EAAE,qBAAqB,EAAE,mBAAmB,GAAG,CAAC,eAAe,EAAE,EAAE,mBAAmB,GAAG,CAAC,eAAe,EAAE,EAAE,oBAAoB;oBAC9J,4BAA4B,EAAE,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,SAAS,CAAC,EAAE;SACzE,CAAC;IACJ,CAAC;CACF,CAAC,CAAC;AAEH,eAAe,cAAc,CAAC","sourcesContent":["import { MayuraError, type JsonObject, type JsonValue } from 'mayura';\nimport { defineDeployTarget, dockerfile, dockerignore, imageSteps, type DeployProject, type DeployStep } from 'mayura/cli/deploy';\n\n/** Where a release's image is built: by Cloud Build, or by your local Docker and pushed. */\nexport type CloudRunBuild = 'cloud-build' | 'local';\n\nexport interface CloudRunSettings {\n /** The Google Cloud project id; a release needs it. */\n readonly project?: string;\n /** The Cloud Run region, such as `us-central1`. */\n readonly region: string;\n readonly build: CloudRunBuild;\n /**\n * Environment variables taken from Secret Manager, as `NAME: 'secret:version'`. By default `DATABASE_URL` and every\n * name in `env` come from a secret of the same name, at `latest`.\n */\n readonly secrets: Readonly<Record<string, string>>;\n /** A Cloud SQL instance (`project:region:instance`) to connect over the Cloud SQL connector. */\n readonly cloudSqlInstance?: string;\n /** The service account the services and the migration job run as. */\n readonly serviceAccount?: string;\n /** Whether anyone may call the server (it authenticates requests itself); `true` by default. */\n readonly public: boolean;\n /** Instances kept warm: the server's minimum (1) and the worker's count (1). */\n readonly serverMinInstances: number;\n readonly workerInstances: number;\n}\n\nconst allowed = ['project', 'region', 'build', 'secrets', 'cloudSqlInstance', 'serviceAccount', 'public', 'serverMinInstances', 'workerInstances'];\nconst projectId = /^[a-z][a-z0-9-]{4,28}[a-z0-9]$/u;\nconst region = /^[a-z]+-[a-z]+\\d$/u;\nconst envName = /^[A-Z_][A-Z0-9_]{0,127}$/u;\nconst secretRef = /^[A-Za-z0-9_-]{1,255}:(?:latest|\\d{1,10})$/u;\n\nfunction settings(value: JsonValue | undefined, project: DeployProject): CloudRunSettings {\n const raw = (value ?? {}) as JsonObject;\n if (!raw || typeof raw !== 'object' || Array.isArray(raw) || Object.keys(raw).some(key => !allowed.includes(key))) throw new MayuraError('INVALID_CONFIG', `targets.cloudrun allows only ${allowed.join(', ')}.`);\n const id = raw['project']; const where = raw['region'] ?? 'us-central1'; const build = raw['build'] ?? 'cloud-build';\n if (id !== undefined && (typeof id !== 'string' || !projectId.test(id))) throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.project must be a Google Cloud project id.');\n if (typeof where !== 'string' || !region.test(where)) throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.region must be a Cloud Run region, such as us-central1.');\n if (build !== 'cloud-build' && build !== 'local') throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.build must be cloud-build or local.');\n const secrets: Record<string, string> = Object.fromEntries(['DATABASE_URL', ...project.env].map(name => [name, `${name}:latest`]));\n const configured = raw['secrets'];\n if (configured !== undefined) {\n if (!configured || typeof configured !== 'object' || Array.isArray(configured)) throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.secrets maps variable names to secret:version.');\n for (const [name, reference] of Object.entries(configured)) {\n if (!envName.test(name) || typeof reference !== 'string' || !secretRef.test(reference)) throw new MayuraError('INVALID_CONFIG', `targets.cloudrun.secrets.${name} must be a Secret Manager reference such as openai-key:latest.`);\n secrets[name] = reference;\n }\n }\n const instance = raw['cloudSqlInstance']; const account = raw['serviceAccount']; const isPublic = raw['public'] ?? true;\n if (instance !== undefined && (typeof instance !== 'string' || !/^[a-z][a-z0-9-]{4,28}[a-z0-9]:[a-z]+-[a-z]+\\d:[a-z][a-z0-9-]{0,95}$/u.test(instance))) throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.cloudSqlInstance must be project:region:instance.');\n if (account !== undefined && (typeof account !== 'string' || !/^[a-z][a-z0-9-]{4,29}@[a-z0-9.-]+\\.iam\\.gserviceaccount\\.com$/u.test(account))) throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.serviceAccount must be a service account email.');\n if (typeof isPublic !== 'boolean') throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.public must be true or false.');\n const count = (name: string, fallback: number): number => {\n const item = raw[name] ?? fallback;\n if (typeof item !== 'number' || !Number.isSafeInteger(item) || item < 1 || item > 100) throw new MayuraError('INVALID_CONFIG', `targets.cloudrun.${name} must be from 1 to 100.`);\n return item;\n };\n return { ...(id === undefined ? {} : { project: id as string }), region: where, build, secrets, ...(instance === undefined ? {} : { cloudSqlInstance: instance as string }),\n ...(account === undefined ? {} : { serviceAccount: account as string }), public: isPublic, serverMinInstances: count('serverMinInstances', 1), workerInstances: count('workerInstances', 1) };\n}\n\n/** Deploys to Google Cloud Run with gcloud: a migration job, then the server and an always-on worker as services. */\nexport const cloudRunTarget = defineDeployTarget<CloudRunSettings>({\n id: 'cloudrun', description: 'Deploy to Google Cloud Run with gcloud: a migration job, then the server and an always-on worker as services.', tools: ['gcloud', 'docker'],\n settings,\n files: ({ project }) => ({ Dockerfile: dockerfile(project), '.dockerignore': dockerignore }),\n plan: async ({ project, settings: run, release, readFile }) => {\n await readFile('Dockerfile');\n if (run.project === undefined) throw new MayuraError('INVALID_CONFIG', 'Set targets.cloudrun.project in mayura.deploy.json to the Google Cloud project id.');\n const gcp = run.project;\n if (release.image === undefined) throw new MayuraError('INVALID_CONFIG', 'Cloud Run deploys an image: set \"image\" in mayura.deploy.json to an Artifact Registry repository, such as us-central1-docker.pkg.dev/acme/apps/refunds.');\n const image = release.image; const where = ['--project', gcp, '--region', run.region];\n const shared = [\n `--image=${image}`, ...(Object.keys(run.secrets).length ? [`--set-secrets=${Object.entries(run.secrets).map(([name, reference]) => `${name}=${reference}`).join(',')}`] : []),\n ...(run.cloudSqlInstance === undefined ? [] : [`--set-cloudsql-instances=${run.cloudSqlInstance}`]), ...(run.serviceAccount === undefined ? [] : [`--service-account=${run.serviceAccount}`]),\n ];\n const probe = (port: number) => `--startup-probe=httpGet.path=/readyz,httpGet.port=${port},periodSeconds=5,failureThreshold=24`;\n const build: DeployStep[] = run.build === 'local' ? imageSteps(release, 'cloudrun')\n : [{ id: 'build-image', description: `Build ${image} with Cloud Build`, tool: 'gcloud', args: ['builds', 'submit', '--project', gcp, `--tag=${image}`, '--quiet', '.'] }];\n return [\n { id: 'check-project', description: `Check that gcloud can reach ${gcp}`, tool: 'gcloud', args: ['projects', 'describe', gcp, '--format=none'] },\n ...build,\n // One job, updated to the release's image and run to completion; a failure stops the release.\n { id: 'migrate', description: 'Run the migration job and wait for it', tool: 'gcloud',\n args: ['run', 'jobs', 'deploy', `${project.name}-migrate`, ...where, ...shared, `--args=migrate,--app,${project.app}`, '--max-retries=0', '--task-timeout=1800s', '--execute-now', '--wait', '--quiet'] },\n // CPU stays allocated and instances stay warm: agent runs continue after a response.\n { id: 'deploy-server', description: 'Deploy the server and wait until it is ready', tool: 'gcloud',\n args: ['run', 'deploy', `${project.name}-server`, ...where, ...shared, `--args=serve,--app,${project.app}`, `--port=${project.port}`, '--no-cpu-throttling',\n `--min-instances=${run.serverMinInstances}`, '--timeout=3600s', probe(project.port), run.public ? '--allow-unauthenticated' : '--no-allow-unauthenticated', '--quiet'] },\n // The worker serves only its probes: internal, never public, always running.\n { id: 'deploy-worker', description: 'Deploy the always-on worker and wait until it is ready', tool: 'gcloud',\n args: ['run', 'deploy', `${project.name}-worker`, ...where, ...shared, `--args=worker,--app,${project.app},--probe-host,0.0.0.0,--probe-port,${project.probePort}`,\n `--port=${project.probePort}`, '--no-cpu-throttling', `--min-instances=${run.workerInstances}`, `--max-instances=${run.workerInstances}`, '--ingress=internal',\n '--no-allow-unauthenticated', probe(project.probePort), '--quiet'] },\n ];\n },\n});\n\nexport default cloudRunTarget;\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mayurajs/deploy-cloudrun",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "Google Cloud Run as a mayura deploy target: a migration job, then the server and an always-on worker as services.",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Aryabh",
|
|
7
|
+
"email": "dev@rokad.co"
|
|
8
|
+
},
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/KartikeyAI/mayura.git",
|
|
13
|
+
"directory": "extensions/deploy-cloudrun"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/KartikeyAI/mayura#readme",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/KartikeyAI/mayura/issues"
|
|
18
|
+
},
|
|
19
|
+
"type": "module",
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=22.12.0 <23 || >=24.14.1 <25"
|
|
23
|
+
},
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"src/**/*.ts",
|
|
33
|
+
"README.md",
|
|
34
|
+
"LICENSE",
|
|
35
|
+
"NOTICE"
|
|
36
|
+
],
|
|
37
|
+
"dependencies": {},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"mayura": "^1.2.0"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public",
|
|
43
|
+
"provenance": true
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { MayuraError, type JsonObject, type JsonValue } from 'mayura';
|
|
2
|
+
import { defineDeployTarget, dockerfile, dockerignore, imageSteps, type DeployProject, type DeployStep } from 'mayura/cli/deploy';
|
|
3
|
+
|
|
4
|
+
/** Where a release's image is built: by Cloud Build, or by your local Docker and pushed. */
|
|
5
|
+
export type CloudRunBuild = 'cloud-build' | 'local';
|
|
6
|
+
|
|
7
|
+
export interface CloudRunSettings {
|
|
8
|
+
/** The Google Cloud project id; a release needs it. */
|
|
9
|
+
readonly project?: string;
|
|
10
|
+
/** The Cloud Run region, such as `us-central1`. */
|
|
11
|
+
readonly region: string;
|
|
12
|
+
readonly build: CloudRunBuild;
|
|
13
|
+
/**
|
|
14
|
+
* Environment variables taken from Secret Manager, as `NAME: 'secret:version'`. By default `DATABASE_URL` and every
|
|
15
|
+
* name in `env` come from a secret of the same name, at `latest`.
|
|
16
|
+
*/
|
|
17
|
+
readonly secrets: Readonly<Record<string, string>>;
|
|
18
|
+
/** A Cloud SQL instance (`project:region:instance`) to connect over the Cloud SQL connector. */
|
|
19
|
+
readonly cloudSqlInstance?: string;
|
|
20
|
+
/** The service account the services and the migration job run as. */
|
|
21
|
+
readonly serviceAccount?: string;
|
|
22
|
+
/** Whether anyone may call the server (it authenticates requests itself); `true` by default. */
|
|
23
|
+
readonly public: boolean;
|
|
24
|
+
/** Instances kept warm: the server's minimum (1) and the worker's count (1). */
|
|
25
|
+
readonly serverMinInstances: number;
|
|
26
|
+
readonly workerInstances: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const allowed = ['project', 'region', 'build', 'secrets', 'cloudSqlInstance', 'serviceAccount', 'public', 'serverMinInstances', 'workerInstances'];
|
|
30
|
+
const projectId = /^[a-z][a-z0-9-]{4,28}[a-z0-9]$/u;
|
|
31
|
+
const region = /^[a-z]+-[a-z]+\d$/u;
|
|
32
|
+
const envName = /^[A-Z_][A-Z0-9_]{0,127}$/u;
|
|
33
|
+
const secretRef = /^[A-Za-z0-9_-]{1,255}:(?:latest|\d{1,10})$/u;
|
|
34
|
+
|
|
35
|
+
function settings(value: JsonValue | undefined, project: DeployProject): CloudRunSettings {
|
|
36
|
+
const raw = (value ?? {}) as JsonObject;
|
|
37
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw) || Object.keys(raw).some(key => !allowed.includes(key))) throw new MayuraError('INVALID_CONFIG', `targets.cloudrun allows only ${allowed.join(', ')}.`);
|
|
38
|
+
const id = raw['project']; const where = raw['region'] ?? 'us-central1'; const build = raw['build'] ?? 'cloud-build';
|
|
39
|
+
if (id !== undefined && (typeof id !== 'string' || !projectId.test(id))) throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.project must be a Google Cloud project id.');
|
|
40
|
+
if (typeof where !== 'string' || !region.test(where)) throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.region must be a Cloud Run region, such as us-central1.');
|
|
41
|
+
if (build !== 'cloud-build' && build !== 'local') throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.build must be cloud-build or local.');
|
|
42
|
+
const secrets: Record<string, string> = Object.fromEntries(['DATABASE_URL', ...project.env].map(name => [name, `${name}:latest`]));
|
|
43
|
+
const configured = raw['secrets'];
|
|
44
|
+
if (configured !== undefined) {
|
|
45
|
+
if (!configured || typeof configured !== 'object' || Array.isArray(configured)) throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.secrets maps variable names to secret:version.');
|
|
46
|
+
for (const [name, reference] of Object.entries(configured)) {
|
|
47
|
+
if (!envName.test(name) || typeof reference !== 'string' || !secretRef.test(reference)) throw new MayuraError('INVALID_CONFIG', `targets.cloudrun.secrets.${name} must be a Secret Manager reference such as openai-key:latest.`);
|
|
48
|
+
secrets[name] = reference;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const instance = raw['cloudSqlInstance']; const account = raw['serviceAccount']; const isPublic = raw['public'] ?? true;
|
|
52
|
+
if (instance !== undefined && (typeof instance !== 'string' || !/^[a-z][a-z0-9-]{4,28}[a-z0-9]:[a-z]+-[a-z]+\d:[a-z][a-z0-9-]{0,95}$/u.test(instance))) throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.cloudSqlInstance must be project:region:instance.');
|
|
53
|
+
if (account !== undefined && (typeof account !== 'string' || !/^[a-z][a-z0-9-]{4,29}@[a-z0-9.-]+\.iam\.gserviceaccount\.com$/u.test(account))) throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.serviceAccount must be a service account email.');
|
|
54
|
+
if (typeof isPublic !== 'boolean') throw new MayuraError('INVALID_CONFIG', 'targets.cloudrun.public must be true or false.');
|
|
55
|
+
const count = (name: string, fallback: number): number => {
|
|
56
|
+
const item = raw[name] ?? fallback;
|
|
57
|
+
if (typeof item !== 'number' || !Number.isSafeInteger(item) || item < 1 || item > 100) throw new MayuraError('INVALID_CONFIG', `targets.cloudrun.${name} must be from 1 to 100.`);
|
|
58
|
+
return item;
|
|
59
|
+
};
|
|
60
|
+
return { ...(id === undefined ? {} : { project: id as string }), region: where, build, secrets, ...(instance === undefined ? {} : { cloudSqlInstance: instance as string }),
|
|
61
|
+
...(account === undefined ? {} : { serviceAccount: account as string }), public: isPublic, serverMinInstances: count('serverMinInstances', 1), workerInstances: count('workerInstances', 1) };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Deploys to Google Cloud Run with gcloud: a migration job, then the server and an always-on worker as services. */
|
|
65
|
+
export const cloudRunTarget = defineDeployTarget<CloudRunSettings>({
|
|
66
|
+
id: 'cloudrun', description: 'Deploy to Google Cloud Run with gcloud: a migration job, then the server and an always-on worker as services.', tools: ['gcloud', 'docker'],
|
|
67
|
+
settings,
|
|
68
|
+
files: ({ project }) => ({ Dockerfile: dockerfile(project), '.dockerignore': dockerignore }),
|
|
69
|
+
plan: async ({ project, settings: run, release, readFile }) => {
|
|
70
|
+
await readFile('Dockerfile');
|
|
71
|
+
if (run.project === undefined) throw new MayuraError('INVALID_CONFIG', 'Set targets.cloudrun.project in mayura.deploy.json to the Google Cloud project id.');
|
|
72
|
+
const gcp = run.project;
|
|
73
|
+
if (release.image === undefined) throw new MayuraError('INVALID_CONFIG', 'Cloud Run deploys an image: set "image" in mayura.deploy.json to an Artifact Registry repository, such as us-central1-docker.pkg.dev/acme/apps/refunds.');
|
|
74
|
+
const image = release.image; const where = ['--project', gcp, '--region', run.region];
|
|
75
|
+
const shared = [
|
|
76
|
+
`--image=${image}`, ...(Object.keys(run.secrets).length ? [`--set-secrets=${Object.entries(run.secrets).map(([name, reference]) => `${name}=${reference}`).join(',')}`] : []),
|
|
77
|
+
...(run.cloudSqlInstance === undefined ? [] : [`--set-cloudsql-instances=${run.cloudSqlInstance}`]), ...(run.serviceAccount === undefined ? [] : [`--service-account=${run.serviceAccount}`]),
|
|
78
|
+
];
|
|
79
|
+
const probe = (port: number) => `--startup-probe=httpGet.path=/readyz,httpGet.port=${port},periodSeconds=5,failureThreshold=24`;
|
|
80
|
+
const build: DeployStep[] = run.build === 'local' ? imageSteps(release, 'cloudrun')
|
|
81
|
+
: [{ id: 'build-image', description: `Build ${image} with Cloud Build`, tool: 'gcloud', args: ['builds', 'submit', '--project', gcp, `--tag=${image}`, '--quiet', '.'] }];
|
|
82
|
+
return [
|
|
83
|
+
{ id: 'check-project', description: `Check that gcloud can reach ${gcp}`, tool: 'gcloud', args: ['projects', 'describe', gcp, '--format=none'] },
|
|
84
|
+
...build,
|
|
85
|
+
// One job, updated to the release's image and run to completion; a failure stops the release.
|
|
86
|
+
{ id: 'migrate', description: 'Run the migration job and wait for it', tool: 'gcloud',
|
|
87
|
+
args: ['run', 'jobs', 'deploy', `${project.name}-migrate`, ...where, ...shared, `--args=migrate,--app,${project.app}`, '--max-retries=0', '--task-timeout=1800s', '--execute-now', '--wait', '--quiet'] },
|
|
88
|
+
// CPU stays allocated and instances stay warm: agent runs continue after a response.
|
|
89
|
+
{ id: 'deploy-server', description: 'Deploy the server and wait until it is ready', tool: 'gcloud',
|
|
90
|
+
args: ['run', 'deploy', `${project.name}-server`, ...where, ...shared, `--args=serve,--app,${project.app}`, `--port=${project.port}`, '--no-cpu-throttling',
|
|
91
|
+
`--min-instances=${run.serverMinInstances}`, '--timeout=3600s', probe(project.port), run.public ? '--allow-unauthenticated' : '--no-allow-unauthenticated', '--quiet'] },
|
|
92
|
+
// The worker serves only its probes: internal, never public, always running.
|
|
93
|
+
{ id: 'deploy-worker', description: 'Deploy the always-on worker and wait until it is ready', tool: 'gcloud',
|
|
94
|
+
args: ['run', 'deploy', `${project.name}-worker`, ...where, ...shared, `--args=worker,--app,${project.app},--probe-host,0.0.0.0,--probe-port,${project.probePort}`,
|
|
95
|
+
`--port=${project.probePort}`, '--no-cpu-throttling', `--min-instances=${run.workerInstances}`, `--max-instances=${run.workerInstances}`, '--ingress=internal',
|
|
96
|
+
'--no-allow-unauthenticated', probe(project.probePort), '--quiet'] },
|
|
97
|
+
];
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
export default cloudRunTarget;
|