firebase-tools 15.34.2 → 15.35.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.
Files changed (53) hide show
  1. package/lib/api.js +2 -4
  2. package/lib/apphosting/constants.js +1 -4
  3. package/lib/apphosting/utils.js +0 -26
  4. package/lib/commands/{crashlytics-onboard-web.js → crashlytics-setup.js} +5 -5
  5. package/lib/commands/crashlytics-sourcemap-upload.js +4 -0
  6. package/lib/commands/emulators-start.js +2 -2
  7. package/lib/commands/index.js +3 -2
  8. package/lib/commands/run-services-get.js +19 -0
  9. package/lib/commands/run-services-list.js +11 -0
  10. package/lib/crashlytics/alerts.js +1 -0
  11. package/lib/crashlytics/firebasetelemetry.js +5 -3
  12. package/lib/crashlytics/onboarding.js +8 -7
  13. package/lib/crashlytics/reports.js +46 -17
  14. package/lib/crashlytics/sourcemap.js +19 -5
  15. package/lib/dataconnect/dataplaneClient.js +0 -10
  16. package/lib/deploy/apphosting/deploy.js +20 -41
  17. package/lib/deploy/apphosting/localBuild.js +49 -0
  18. package/lib/deploy/apphosting/prepare.js +12 -25
  19. package/lib/deploy/apphosting/release.js +1 -1
  20. package/lib/deploy/apphosting/util.js +27 -4
  21. package/lib/deploy/functions/containerCleaner.js +1 -142
  22. package/lib/deploy/functions/functionsDeployHelper.js +0 -4
  23. package/lib/deploy/functions/validate.js +1 -1
  24. package/lib/deploy/run/deploy.js +26 -43
  25. package/lib/deploy/run/get.js +30 -0
  26. package/lib/deploy/run/list.js +38 -0
  27. package/lib/deploy/run/prereqs.js +0 -3
  28. package/lib/deploy/run/util.js +9 -0
  29. package/lib/emulator/commandUtils.js +105 -23
  30. package/lib/emulator/dataconnect/pgliteServer.js +1 -0
  31. package/lib/emulator/downloadableEmulators.js +43 -5
  32. package/lib/emulator/storage/rules/runtime.js +1 -0
  33. package/lib/extensions/extensionsApi.js +0 -8
  34. package/lib/extensions/extensionsHelper.js +1 -22
  35. package/lib/extensions/secretsUtils.js +0 -24
  36. package/lib/frameworks/next/utils.js +0 -11
  37. package/lib/fsAsync.js +34 -20
  38. package/lib/functions/kits/install.js +37 -337
  39. package/lib/functions/kits/sources/directory.js +59 -0
  40. package/lib/functions/kits/sources/index.js +19 -0
  41. package/lib/functions/kits/sources/npm.js +274 -0
  42. package/lib/functions/kits/sources/types.js +12 -0
  43. package/lib/gcp/artifactregistry.js +0 -5
  44. package/lib/gcp/iam.js +0 -4
  45. package/lib/gcp/pubsub.js +0 -14
  46. package/lib/gcp/storage.js +2 -1
  47. package/lib/init/features/run.js +3 -7
  48. package/lib/mcp/resources/guides/crashlytics_reports.js +33 -71
  49. package/lib/mcp/tools/crashlytics/reports.js +25 -30
  50. package/lib/tsconfig.compile.tsbuildinfo +1 -1
  51. package/lib/tsconfig.publish.tsbuildinfo +1 -1
  52. package/package.json +1 -2
  53. package/lib/deploy/hosting/args.js +0 -2
@@ -4,14 +4,12 @@ exports.default = default_1;
4
4
  exports.injectEnvVarsFromApphostingConfig = injectEnvVarsFromApphostingConfig;
5
5
  exports.injectAutoInitEnvVars = injectAutoInitEnvVars;
6
6
  exports.getBackendConfigs = getBackendConfigs;
7
- exports.prepareLocalBuildScratchDirectory = prepareLocalBuildScratchDirectory;
8
7
  exports.injectAngularEnvVars = injectAngularEnvVars;
9
8
  const crypto = require("crypto");
10
9
  const fs = require("fs");
11
10
  const os = require("os");
12
11
  const path = require("path");
13
- const fsAsync = require("../../fsAsync");
14
- const util_1 = require("./util");
12
+ const localBuild_1 = require("./localBuild");
15
13
  const backend_1 = require("../../apphosting/backend");
16
14
  const apphosting_1 = require("../../gcp/apphosting");
17
15
  const yaml_1 = require("../../apphosting/yaml");
@@ -163,15 +161,18 @@ async function default_1(context, options) {
163
161
  const pathHash = crypto.createHash("md5").update(rootDir).digest("hex").substring(0, 8);
164
162
  const localBuildScratchDir = path.join(os.tmpdir(), `apphosting-local-build-${cfg.backendId}-${pathHash}`);
165
163
  try {
166
- await prepareLocalBuildScratchDirectory(rootDir, localBuildScratchDir, cfg);
167
- const { outputFiles, buildConfig } = await (0, localbuilds_1.localBuild)(projectId, localBuildScratchDir, buildEnv[cfg.backendId] || {}, {
164
+ const { archive, rootDirectory, buildConfig } = await (0, localBuild_1.buildLocally)({
165
+ projectId,
166
+ projectDir: rootDir,
167
+ config: cfg,
168
+ env: buildEnv[cfg.backendId] || {},
169
+ scratchDir: localBuildScratchDir,
168
170
  nonInteractive: options.nonInteractive,
169
171
  allowLocalBuildSecrets: !!options.allowLocalBuildSecrets,
170
- rootDir: cfg.rootDir,
171
172
  });
172
173
  context.backendLocalBuilds[cfg.backendId] = {
173
- outputFiles,
174
- localBuildScratchDir,
174
+ archive,
175
+ rootDirectory,
175
176
  buildConfig: {
176
177
  ...buildConfig,
177
178
  env: mergeEnvVars(buildConfig.env || [], runtimeEnv[cfg.backendId] || {}),
@@ -216,7 +217,9 @@ async function injectAutoInitEnvVars(cfg, backends, buildEnv, runtimeEnv) {
216
217
  const autoinitVars = (0, utils_2.getAutoinitEnvVars)(webappConfig);
217
218
  for (const [envVarName, envVarValue] of Object.entries(autoinitVars)) {
218
219
  (_a = buildEnv[cfg.backendId])[envVarName] ?? (_a[envVarName] = { value: envVarValue });
219
- (_b = runtimeEnv[cfg.backendId])[envVarName] ?? (_b[envVarName] = { value: envVarValue });
220
+ if (envVarName === "FIREBASE_CONFIG") {
221
+ (_b = runtimeEnv[cfg.backendId])[envVarName] ?? (_b[envVarName] = { value: envVarValue });
222
+ }
220
223
  }
221
224
  }
222
225
  catch (e) {
@@ -280,22 +283,6 @@ async function ensureAppHostingServiceAgentRoles(projectId, projectNumber) {
280
283
  (0, utils_1.logLabeledWarning)("apphosting", `Unable to verify App Hosting service agent permissions for ${p4saEmail}. If you encounter a PERMISSION_DENIED error during rollout, please ensure the service agent has the "Storage Object Viewer" role.`);
281
284
  }
282
285
  }
283
- async function prepareLocalBuildScratchDirectory(rootDir, localBuildScratchDir, cfg) {
284
- const ignore = (0, util_1.resolveIgnorePatterns)(cfg);
285
- fs.rmSync(localBuildScratchDir, { recursive: true, force: true });
286
- fs.mkdirSync(localBuildScratchDir, { recursive: true });
287
- const filesToCopy = await fsAsync.readdirRecursive({
288
- path: rootDir,
289
- ignoreStrings: ignore,
290
- supportGitIgnore: true,
291
- });
292
- for (const file of filesToCopy) {
293
- const relativePath = path.relative(rootDir, file.name);
294
- const destPath = path.join(localBuildScratchDir, relativePath);
295
- fs.mkdirSync(path.dirname(destPath), { recursive: true });
296
- fs.copyFileSync(file.name, destPath);
297
- }
298
- }
299
286
  function isAngularApplication(appDir) {
300
287
  const packageJsonPath = path.join(appDir, "package.json");
301
288
  if (fs.existsSync(packageJsonPath)) {
@@ -27,7 +27,7 @@ async function default_1(context, options) {
27
27
  ? {
28
28
  locallyBuilt: {
29
29
  userStorageUri,
30
- rootDirectory,
30
+ rootDirectory: localBuild.rootDirectory,
31
31
  runCommand: localBuild.buildConfig?.runCommand,
32
32
  env: localBuild.buildConfig?.env,
33
33
  ...(localBuild.buildConfig?.runConfig
@@ -2,7 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createLocalBuildTarArchive = createLocalBuildTarArchive;
4
4
  exports.createSourceDeployArchive = createSourceDeployArchive;
5
+ exports.uploadArchive = uploadArchive;
5
6
  exports.resolveIgnorePatterns = resolveIgnorePatterns;
7
+ exports.buildDirectory = buildDirectory;
6
8
  const archiver = require("archiver");
7
9
  const fs = require("fs");
8
10
  const path = require("path");
@@ -11,6 +13,7 @@ const tmp = require("tmp");
11
13
  const error_1 = require("../../error");
12
14
  const logger_1 = require("../../logger");
13
15
  const fsAsync = require("../../fsAsync");
16
+ const gcs = require("../../gcp/storage");
14
17
  const utils_1 = require("../../utils");
15
18
  const constants_1 = require("../../apphosting/constants");
16
19
  async function createLocalBuildTarArchive(config, rootDir, outputFiles, sizeLimitBytes = constants_1.CLOUD_RUN_SIZE_LIMIT_BYTES) {
@@ -64,23 +67,23 @@ async function createLocalBuildTarArchive(config, rootDir, outputFiles, sizeLimi
64
67
  }
65
68
  return tmpFile;
66
69
  }
67
- async function createSourceDeployArchive(config, rootDir, targetSubDir) {
70
+ async function createSourceDeployArchive(config, folderToZip, projectFolder = folderToZip) {
68
71
  const tmpFile = tmp.fileSync({ prefix: `${config.backendId}-`, postfix: ".zip" }).name;
69
72
  const fileStream = fs.createWriteStream(tmpFile, {
70
73
  flags: "w",
71
74
  encoding: "binary",
72
75
  });
73
76
  const archive = archiver("zip");
74
- const targetDir = targetSubDir ? path.join(rootDir, targetSubDir) : rootDir;
75
77
  const ignore = resolveIgnorePatterns(config);
76
78
  try {
77
79
  const files = await fsAsync.readdirRecursive({
78
- path: targetDir,
80
+ path: folderToZip,
79
81
  ignoreStrings: ignore,
80
82
  supportGitIgnore: true,
83
+ gitIgnoreRoot: projectFolder,
81
84
  });
82
85
  for (const file of files) {
83
- const name = path.relative(rootDir, file.name);
86
+ const name = path.relative(folderToZip, file.name);
84
87
  archive.file(file.name, {
85
88
  name,
86
89
  mode: file.mode,
@@ -93,6 +96,10 @@ async function createSourceDeployArchive(config, rootDir, targetSubDir) {
93
96
  }
94
97
  return tmpFile;
95
98
  }
99
+ async function uploadArchive(archive, bucketName, localBuild) {
100
+ const { bucket, object } = await gcs.uploadObject({ file: archive, stream: fs.createReadStream(archive) }, bucketName, localBuild ? gcs.ContentType.TAR : gcs.ContentType.ZIP, localBuild ? constants_1.CLOUD_RUN_SIZE_LIMIT_BYTES : undefined);
101
+ return { bucket, object };
102
+ }
96
103
  function resolveIgnorePatterns(config, skipDefaultNodeModules = false) {
97
104
  const ignore = config.ignore
98
105
  ? [...config.ignore]
@@ -103,6 +110,22 @@ function resolveIgnorePatterns(config, skipDefaultNodeModules = false) {
103
110
  ignore.push(".local_build_*");
104
111
  return ignore;
105
112
  }
113
+ const MONOREPO_CONFIG_FILES = ["nx.json", "turbo.json"];
114
+ function buildDirectory(projectDir, rootDir = "") {
115
+ const appFolderPath = path.join(projectDir, rootDir);
116
+ const appFolderInProject = path.relative(projectDir, appFolderPath);
117
+ if (appFolderInProject.startsWith("..")) {
118
+ throw new error_1.FirebaseError(`rootDir "${rootDir}" must be inside the project directory.`);
119
+ }
120
+ const segments = appFolderInProject ? appFolderInProject.split(path.sep) : [];
121
+ for (let depth = segments.length; depth >= 0; depth--) {
122
+ const folder = path.join(projectDir, ...segments.slice(0, depth));
123
+ if (MONOREPO_CONFIG_FILES.some((file) => fs.existsSync(path.join(folder, file)))) {
124
+ return { buildFolder: folder, appFolder: segments.slice(depth).join("/") };
125
+ }
126
+ }
127
+ return { buildFolder: appFolderPath, appFolder: "" };
128
+ }
106
129
  async function pipeAsync(from, to) {
107
130
  from.pipe(to);
108
131
  await from.finalize();
@@ -1,18 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DockerHelper = exports.ContainerRegistryCleaner = exports.ArtifactRegistryCleaner = void 0;
4
- exports.cleanupBuildImages = cleanupBuildImages;
3
+ exports.DockerHelper = void 0;
5
4
  exports.listGcfPaths = listGcfPaths;
6
5
  exports.deleteGcfArtifacts = deleteGcfArtifacts;
7
- const clc = require("colorette");
8
6
  const error_1 = require("../../error");
9
7
  const api_1 = require("../../api");
10
8
  const logger_1 = require("../../logger");
11
- const artifactregistry = require("../../gcp/artifactregistry");
12
- const backend = require("./backend");
13
9
  const docker = require("../../gcp/docker");
14
- const utils = require("../../utils");
15
- const poller = require("../../operation-poller");
16
10
  async function retry(func) {
17
11
  const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
18
12
  const MAX_RETRIES = 3;
@@ -36,141 +30,6 @@ async function retry(func) {
36
30
  }
37
31
  }
38
32
  }
39
- async function cleanupBuildImages(haveFunctions, deletedFunctions, cleaners = {}) {
40
- utils.logBullet(clc.bold(clc.cyan("functions: ")) + "cleaning up build files...");
41
- const failedDomains = new Set();
42
- const cleanup = [];
43
- const arCleaner = cleaners.ar || new ArtifactRegistryCleaner();
44
- cleanup.push(...haveFunctions.map(async (func) => {
45
- try {
46
- await arCleaner.cleanupFunction(func);
47
- }
48
- catch (err) {
49
- const path = `${func.project}/${func.region}/gcf-artifacts`;
50
- failedDomains.add(`https://console.cloud.google.com/artifacts/docker/${path}`);
51
- }
52
- }));
53
- cleanup.push(...deletedFunctions.map(async (func) => {
54
- try {
55
- await arCleaner.cleanupFunction(func);
56
- }
57
- catch (err) {
58
- const path = `${func.project}/${func.region}/gcf-artifacts`;
59
- failedDomains.add(`https://console.cloud.google.com/artifacts/docker/${path}`);
60
- }
61
- }));
62
- const gcrCleaner = cleaners.gcr || new ContainerRegistryCleaner();
63
- cleanup.push(...[...haveFunctions, ...deletedFunctions].map(async (func) => {
64
- try {
65
- await gcrCleaner.cleanupFunction(func);
66
- }
67
- catch (err) {
68
- const path = `${func.project}/${docker.GCR_SUBDOMAIN_MAPPING[func.region]}/gcf`;
69
- failedDomains.add(`https://console.cloud.google.com/gcr/images/${path}`);
70
- }
71
- }));
72
- await Promise.all(cleanup);
73
- if (failedDomains.size) {
74
- let message = "Unhandled error cleaning up build images. This could result in a small monthly bill if not corrected. ";
75
- message +=
76
- "You can attempt to delete these images by redeploying or you can delete them manually at";
77
- if (failedDomains.size === 1) {
78
- message += " " + failedDomains.values().next().value;
79
- }
80
- else {
81
- message += [...failedDomains].map((domain) => "\n\t" + domain).join("");
82
- }
83
- utils.logLabeledWarning("functions", message);
84
- }
85
- }
86
- class ArtifactRegistryCleaner {
87
- static packagePath(func) {
88
- const encodedId = func.platform === "gcfv2"
89
- ? ArtifactRegistryCleaner.encodePackageNameV2(func)
90
- : ArtifactRegistryCleaner.encodePackageNameV1(func);
91
- return `projects/${func.project}/locations/${func.region}/repositories/gcf-artifacts/packages/${encodedId}`;
92
- }
93
- static encodePart(part) {
94
- return part
95
- .replace(/_/g, "__")
96
- .replace(/-/g, "--")
97
- .replace(/^[A-Z]/, (first) => `${first.toLowerCase()}-${first.toLowerCase()}`)
98
- .replace(/[A-Z]/g, (upper) => `_${upper.toLowerCase()}`);
99
- }
100
- static encodePackageNameV1(func) {
101
- return ArtifactRegistryCleaner.encodePart(func.id);
102
- }
103
- static encodePackageNameV2(func) {
104
- return [
105
- ArtifactRegistryCleaner.encodePart(func.project),
106
- ArtifactRegistryCleaner.encodePart(func.region),
107
- ArtifactRegistryCleaner.encodePart(func.id),
108
- ].join("__");
109
- }
110
- async cleanupFunction(func) {
111
- let op;
112
- try {
113
- op = await artifactregistry.deletePackage(ArtifactRegistryCleaner.packagePath(func));
114
- }
115
- catch (err) {
116
- if (err.status === 404) {
117
- return;
118
- }
119
- throw err;
120
- }
121
- if (op.done) {
122
- return;
123
- }
124
- await poller.pollOperation({
125
- ...ArtifactRegistryCleaner.POLLER_OPTIONS,
126
- pollerName: `cleanup-${func.region}-${func.id}`,
127
- operationResourceName: op.name,
128
- });
129
- }
130
- }
131
- exports.ArtifactRegistryCleaner = ArtifactRegistryCleaner;
132
- ArtifactRegistryCleaner.POLLER_OPTIONS = {
133
- apiOrigin: (0, api_1.artifactRegistryDomain)(),
134
- apiVersion: artifactregistry.API_VERSION,
135
- masterTimeout: 5 * 60 * 1000,
136
- };
137
- class ContainerRegistryCleaner {
138
- constructor() {
139
- this.helpers = {};
140
- }
141
- helper(location) {
142
- const subdomain = docker.GCR_SUBDOMAIN_MAPPING[location] || "us";
143
- if (!this.helpers[subdomain]) {
144
- const origin = `https://${subdomain}.${(0, api_1.containerRegistryDomain)()}`;
145
- this.helpers[subdomain] = new DockerHelper(origin);
146
- }
147
- return this.helpers[subdomain];
148
- }
149
- async cleanupFunction(func) {
150
- const helper = this.helper(func.region);
151
- const uuids = (await helper.ls(`${func.project}/gcf/${func.region}`)).children;
152
- const uuidTags = {};
153
- const loadUuidTags = [];
154
- for (const uuid of uuids) {
155
- loadUuidTags.push((async () => {
156
- const path = `${func.project}/gcf/${func.region}/${uuid}`;
157
- const tags = (await helper.ls(path)).tags;
158
- uuidTags[path] = tags;
159
- })());
160
- }
161
- await Promise.all(loadUuidTags);
162
- const extractFunction = /^(.*)_version-\d+$/;
163
- const entry = Object.entries(uuidTags).find(([, tags]) => {
164
- return tags.find((tag) => extractFunction.exec(tag)?.[1] === func.id);
165
- });
166
- if (!entry) {
167
- logger_1.logger.debug("Could not find image for function", backend.functionName(func));
168
- return;
169
- }
170
- await helper.rm(entry[0]);
171
- }
172
- }
173
- exports.ContainerRegistryCleaner = ContainerRegistryCleaner;
174
33
  function getHelper(cache, subdomain) {
175
34
  if (!cache[subdomain]) {
176
35
  cache[subdomain] = new DockerHelper(`https://${subdomain}.${(0, api_1.containerRegistryDomain)()}`);
@@ -11,7 +11,6 @@ exports.generationDowngradeMessage = generationDowngradeMessage;
11
11
  exports.targetCodebases = targetCodebases;
12
12
  exports.groupEndpointsByCodebase = groupEndpointsByCodebase;
13
13
  exports.isCodebaseFiltered = isCodebaseFiltered;
14
- exports.isEndpointFiltered = isEndpointFiltered;
15
14
  exports.isCodebasePartiallyFiltered = isCodebasePartiallyFiltered;
16
15
  exports.parseDeleteFilters = parseDeleteFilters;
17
16
  exports.detectCodebaseAndIdCollisions = detectCodebaseAndIdCollisions;
@@ -148,9 +147,6 @@ function isCodebaseFiltered(codebase, filters) {
148
147
  return noIdChunks && filter.codebase === codebase;
149
148
  });
150
149
  }
151
- function isEndpointFiltered(endpoint, filters) {
152
- return filters.some((filter) => endpointMatchesFilter(endpoint, filter));
153
- }
154
150
  function isCodebasePartiallyFiltered(codebase, filters) {
155
151
  if (!filters) {
156
152
  return false;
@@ -229,7 +229,7 @@ function functionIdsAreValid(functions) {
229
229
  const invalidIds = functions.filter((fn) => !functionName.test(fn.id));
230
230
  if (invalidIds.length !== 0) {
231
231
  const msg = `${invalidIds.map((f) => f.id).join(", ")} function name(s) can only contain letters, ` +
232
- `numbers, hyphens, and not exceed 62 characters in length`;
232
+ `numbers, underscores, hyphens, and not exceed 63 characters in length`;
233
233
  throw new error_1.FirebaseError(msg);
234
234
  }
235
235
  }
@@ -6,16 +6,14 @@ exports.localBuildTemplate = localBuildTemplate;
6
6
  const fs = require("fs");
7
7
  const os = require("os");
8
8
  const path = require("path");
9
- const constants_1 = require("../../apphosting/constants");
10
9
  const localbuilds_1 = require("../../apphosting/localbuilds");
11
10
  const error_1 = require("../../error");
12
11
  const artifactregistry = require("../../gcp/artifactregistry");
13
12
  const runv2 = require("../../gcp/runv2");
14
13
  const gcs = require("../../gcp/storage");
15
14
  const getProjectNumber_1 = require("../../getProjectNumber");
16
- const logger_1 = require("../../logger");
17
15
  const utils_1 = require("../../utils");
18
- const prepare_1 = require("../apphosting/prepare");
16
+ const localBuild_1 = require("../apphosting/localBuild");
19
17
  const util_1 = require("../apphosting/util");
20
18
  const buildEnv_1 = require("./buildEnv");
21
19
  const util_2 = require("./util");
@@ -34,26 +32,22 @@ async function deploy(context, options, payload) {
34
32
  }
35
33
  async function deployRemoteBuild(context, options, svc) {
36
34
  const { projectId } = context;
37
- const source = await uploadSource(projectId, options, svc.config);
38
- const image = await buildImage(projectId, svc, source);
35
+ const { buildFolder, appFolder } = (0, util_1.buildDirectory)(options.config.projectDir, svc.config.rootDir);
36
+ const archive = await (0, util_1.createSourceDeployArchive)(toAppHostingConfig(svc.config), buildFolder, options.config.projectDir);
37
+ const source = await upload(projectId, options, svc.config, archive);
38
+ const image = await buildImage(projectId, svc, source, appFolder);
39
39
  const template = revisionTemplate(svc, image, options.message);
40
40
  svc.deployed = await rollOut(projectId, svc, template);
41
41
  }
42
42
  async function deployLocalBuild(context, options, svc) {
43
43
  const { projectId } = context;
44
- const scratchDir = fs.mkdtempSync(path.join(os.tmpdir(), `run-local-build-${svc.config.serviceId}-`));
45
- try {
46
- const build = await buildLocally(context, options, svc, scratchDir);
47
- const builtApp = await uploadBuiltApp(projectId, options, svc.config, scratchDir, build);
48
- const message = options.message;
49
- const template = localBuildTemplate(svc, build, builtApp, message);
50
- svc.deployed = await rollOut(projectId, svc, template);
51
- }
52
- finally {
53
- removeTempPath(scratchDir);
54
- }
44
+ const build = await buildServiceLocally(context, options, svc);
45
+ const builtApp = await upload(projectId, options, svc.config, build.archive);
46
+ const message = options.message;
47
+ const template = localBuildTemplate(svc, build, builtApp, message);
48
+ svc.deployed = await rollOut(projectId, svc, template);
55
49
  }
56
- async function buildLocally(context, options, svc, scratchDir) {
50
+ async function buildServiceLocally(context, options, svc) {
57
51
  const { config } = svc;
58
52
  const { serviceId, region } = config;
59
53
  const cfg = toAppHostingConfig(config);
@@ -64,31 +58,27 @@ async function buildLocally(context, options, svc, scratchDir) {
64
58
  (0, utils_1.logLabeledBullet)("run", `Reading build secrets ${secrets.join(", ")} with your credentials. Their values may be ` +
65
59
  `included in the build output.`);
66
60
  }
67
- await (0, prepare_1.prepareLocalBuildScratchDirectory)(options.config.projectDir, scratchDir, cfg);
68
- const { outputFiles, buildConfig } = await (0, localbuilds_1.localBuild)(context.projectId, scratchDir, (0, buildEnv_1.toLocalBuildEnv)(svc.buildEnv || {}), { rootDir: config.rootDir, allowLocalBuildSecrets: true });
61
+ const { archive, buildConfig } = await (0, localBuild_1.buildLocally)({
62
+ projectId: context.projectId,
63
+ projectDir: options.config.projectDir,
64
+ config: cfg,
65
+ env: (0, buildEnv_1.toLocalBuildEnv)(svc.buildEnv || {}),
66
+ scratchDir: fs.mkdtempSync(path.join(os.tmpdir(), `run-local-build-${serviceId}-`)),
67
+ allowLocalBuildSecrets: true,
68
+ });
69
69
  const env = (buildConfig.env || [])
70
70
  .filter((e) => e.variable &&
71
71
  e.value !== undefined &&
72
72
  (!e.availability || e.availability.includes("RUNTIME")))
73
73
  .map((e) => ({ name: e.variable, value: e.value }));
74
- return { outputFiles, runCommand: buildConfig.runCommand, env };
74
+ return { archive, runCommand: buildConfig.runCommand, env };
75
75
  }
76
- async function uploadSource(projectId, options, config) {
76
+ async function upload(projectId, options, config, archive) {
77
77
  const { serviceId, region } = config;
78
78
  const bucketName = await sourceBucket(projectId, options, region);
79
- const cfg = toAppHostingConfig(config);
80
- const archive = await (0, util_1.createSourceDeployArchive)(cfg, path.join(options.config.projectDir, cfg.rootDir));
81
- (0, utils_1.logLabeledBullet)("run", `Uploading source for service ${serviceId} in ${region}...`);
82
- const { bucket, object } = await gcs.uploadObject({ file: archive, stream: fs.createReadStream(archive) }, bucketName, gcs.ContentType.ZIP);
83
- return { bucket, object };
84
- }
85
- async function uploadBuiltApp(projectId, options, config, scratchDir, build) {
86
- const { serviceId, region } = config;
87
- const bucketName = await sourceBucket(projectId, options, region);
88
- const archive = await (0, util_1.createLocalBuildTarArchive)(toAppHostingConfig(config), scratchDir, build.outputFiles);
89
- (0, utils_1.logLabeledBullet)("run", `Uploading built app for service ${serviceId} in ${region}...`);
90
- const { bucket, object } = await gcs.uploadObject({ file: archive, stream: fs.createReadStream(archive) }, bucketName, gcs.ContentType.TAR, constants_1.CLOUD_RUN_SIZE_LIMIT_BYTES);
91
- return { bucket, object };
79
+ const what = config.localBuild ? "built app" : "source";
80
+ (0, utils_1.logLabeledBullet)("run", `Uploading ${what} for service ${serviceId} in ${region}...`);
81
+ return (0, util_1.uploadArchive)(archive, bucketName, !!config.localBuild);
92
82
  }
93
83
  async function sourceBucket(projectId, options, region) {
94
84
  const baseName = `firebase-run-src-${await (0, getProjectNumber_1.getProjectNumber)(options)}-${region.toLowerCase()}`;
@@ -104,13 +94,14 @@ async function sourceBucket(projectId, options, region) {
104
94
  },
105
95
  });
106
96
  }
107
- async function buildImage(projectId, svc, source) {
97
+ async function buildImage(projectId, svc, source, appFolder) {
108
98
  const { serviceId, region } = svc.config;
109
99
  await artifactregistry.ensureDockerRepository(projectId, region, "cloud-run-source-deploy");
110
100
  const imageUri = `${region}-docker.pkg.dev/${projectId}/cloud-run-source-deploy/${serviceId}:${Date.now()}`;
111
101
  (0, utils_1.logLabeledBullet)("run", `Building service ${serviceId} in ${region}...`);
112
102
  const buildpackBuild = {
113
103
  environmentVariables: {
104
+ ...(appFolder ? { GOOGLE_BUILDABLE: appFolder } : {}),
114
105
  ...(0, buildEnv_1.nonSecretValues)(svc.buildEnv || {}),
115
106
  X_GOOGLE_TARGET_PLATFORM: "fah",
116
107
  FIREBASE_OUTPUT_BUNDLE_DIR: "/workspace/.apphosting",
@@ -232,11 +223,3 @@ function toAppHostingConfig(config) {
232
223
  localBuild: config.localBuild,
233
224
  };
234
225
  }
235
- function removeTempPath(tempPath) {
236
- try {
237
- fs.rmSync(tempPath, { recursive: true, force: true });
238
- }
239
- catch (err) {
240
- logger_1.logger.debug(`Failed to clean up ${tempPath}: ${(0, error_1.getErrMsg)(err)}`);
241
- }
242
- }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getServiceSpec = getServiceSpec;
4
+ const clc = require("colorette");
5
+ const error_1 = require("../../error");
6
+ const run = require("../../gcp/run");
7
+ const runv2 = require("../../gcp/runv2");
8
+ const util_1 = require("./util");
9
+ async function getServiceSpec(projectId, serviceId, region) {
10
+ let matches = (await runv2.listServices(projectId, false)).filter((s) => {
11
+ const name = (0, util_1.parseServiceName)(s.name);
12
+ return name.serviceId === serviceId && (!region || name.region === region);
13
+ });
14
+ if (matches.length > 1) {
15
+ const unmanaged = matches.filter((s) => !(0, util_1.isManagedByAnotherProduct)(s));
16
+ if (unmanaged.length) {
17
+ matches = unmanaged;
18
+ }
19
+ }
20
+ if (!matches.length) {
21
+ throw new error_1.FirebaseError(`Cloud Run service ${serviceId} doesn't exist in ${region ?? `project ${projectId}`}. ` +
22
+ `See your services with ${clc.bold("firebase run:services:list")}.`);
23
+ }
24
+ if (matches.length > 1) {
25
+ const regions = matches.map((s) => (0, util_1.parseServiceName)(s.name).region);
26
+ throw new error_1.FirebaseError(`Cloud Run service ${serviceId} exists in more than one region: ${regions.join(", ")}. ` +
27
+ `Choose one with --region.`);
28
+ }
29
+ return run.getService(matches[0].name);
30
+ }
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.listRunServices = listRunServices;
4
+ exports.serviceRows = serviceRows;
5
+ const Table = require("cli-table3");
6
+ const runv2 = require("../../gcp/runv2");
7
+ const logger_1 = require("../../logger");
8
+ const utils_1 = require("../../utils");
9
+ const util_1 = require("./util");
10
+ async function listRunServices(projectId) {
11
+ const allServices = await runv2.listServices(projectId, false);
12
+ const services = allServices.filter((s) => !(0, util_1.isManagedByAnotherProduct)(s));
13
+ if (services.length) {
14
+ const table = new Table({
15
+ head: ["Service ID", "Region", "Base Image"],
16
+ style: { head: ["green"] },
17
+ });
18
+ table.push(...serviceRows(services));
19
+ logger_1.logger.info(table.toString());
20
+ }
21
+ else {
22
+ logger_1.logger.info(`No Cloud Run services found in project ${projectId}.`);
23
+ }
24
+ const skipped = allServices.length - services.length;
25
+ if (skipped) {
26
+ (0, utils_1.logBullet)(`Not listed: ${skipped} ${skipped === 1 ? "service" : "services"} that another product, ` +
27
+ `like Cloud Functions or App Hosting, manages.`);
28
+ }
29
+ return services;
30
+ }
31
+ function serviceRows(services) {
32
+ const rows = services.map((service) => {
33
+ const { serviceId, region } = (0, util_1.parseServiceName)(service.name);
34
+ const baseImage = (0, util_1.mainContainer)(service.template)?.baseImageUri?.split("/").pop();
35
+ return [serviceId, region, baseImage || "None"];
36
+ });
37
+ return rows.sort(([idA, regionA], [idB, regionB]) => idA.localeCompare(idB) || regionA.localeCompare(regionB));
38
+ }
@@ -12,11 +12,8 @@ exports.RUN_PERMISSIONS = [
12
12
  "run.operations.get",
13
13
  "cloudbuild.builds.create",
14
14
  "cloudbuild.builds.get",
15
- "storage.buckets.get",
16
15
  "storage.buckets.list",
17
16
  "storage.buckets.create",
18
- "storage.buckets.update",
19
- "storage.objects.create",
20
17
  "artifactregistry.repositories.get",
21
18
  "artifactregistry.repositories.create",
22
19
  "artifactregistry.repositories.downloadArtifacts",
@@ -7,6 +7,8 @@ exports.findServices = findServices;
7
7
  exports.fullServiceName = fullServiceName;
8
8
  exports.getExistingService = getExistingService;
9
9
  exports.mainContainer = mainContainer;
10
+ exports.parseServiceName = parseServiceName;
11
+ exports.isManagedByAnotherProduct = isManagedByAnotherProduct;
10
12
  const error_1 = require("../../error");
11
13
  const runv2 = require("../../gcp/runv2");
12
14
  const utils_1 = require("../../utils");
@@ -73,3 +75,10 @@ async function getExistingService(projectId, region, serviceId) {
73
75
  function mainContainer(template) {
74
76
  return template?.containers?.[0];
75
77
  }
78
+ function parseServiceName(name) {
79
+ const parts = name.split("/");
80
+ return { region: parts[3], serviceId: parts[5] };
81
+ }
82
+ function isManagedByAnotherProduct(service) {
83
+ return !!service.labels?.[runv2.CLIENT_NAME_LABEL];
84
+ }