Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track usage of runtime config and dotenv on function deploys. #3704

Merged
merged 4 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Track use of runtime config and environment variables on function deploys. (#3704)
25 changes: 23 additions & 2 deletions src/deploy/functions/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,24 @@ import * as backend from "./backend";
import * as ensureApiEnabled from "../../ensureApiEnabled";
import * as functionsConfig from "../../functionsConfig";
import * as functionsEnv from "../../functions/env";
import { previews } from "../../previews";
import { needProjectId } from "../../projectUtils";
import { track } from "../../track";
import * as runtimes from "./runtimes";
import * as validate from "./validate";
import * as utils from "../../utils";
import { logger } from "../../logger";

function hasUserConfig(config: Record<string, unknown>): boolean {
// "firebase" key is always going to exist in runtime config.
// If any other key exists, we can assume that user is using runtime config.
return Object.keys(config).length > 1;
}

function hasDotenv(opts: functionsEnv.UserEnvsOpts): boolean {
return previews.dotenv && functionsEnv.hasUserEnvs(opts);
}

export async function prepare(
context: args.Context,
options: Options,
Expand Down Expand Up @@ -58,11 +70,20 @@ export async function prepare(
);
const source = options.config.src.functions.source;
const firebaseEnvs = functionsEnv.loadFirebaseEnvs(firebaseConfig, projectId);
const userEnvs = functionsEnv.loadUserEnvs({
const userEnvOpt = {
functionsSource: options.config.path(source),
projectId: projectId,
projectAlias: options.projectAlias,
});
};
const userEnvs = functionsEnv.loadUserEnvs(userEnvOpt);
const tag = hasUserConfig(runtimeConfig)
? hasDotenv(userEnvOpt)
? "mixed"
: "runtime_config"
: hasDotenv(userEnvOpt)
? "dotenv"
: "none";
track("functions_codebase_deploy_env_method", tag);

logger.debug(`Analyzing ${runtimeDelegate.name} backend spec`);
const wantBackend = await runtimeDelegate.discoverSpec(runtimeConfig, firebaseEnvs);
Expand Down
6 changes: 5 additions & 1 deletion src/deploy/functions/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ export async function release(context: args.Context, options: Options, payload:
// AND there are user specified environment variables.
overwriteEnvs:
previews.dotenv &&
hasUserEnvs(options.config.path(functionsSource), projectId, options.projectAlias),
hasUserEnvs({
functionsSource: options.config.path(functionsSource),
projectId,
projectAlias: options.projectAlias,
}),
}
);

Expand Down
2 changes: 1 addition & 1 deletion src/emulator/functionsEmulatorRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ async function initializeEnvironmentalVariables(frb: FunctionsRuntimeBundle): Pr
process.env.GCLOUD_PROJECT = frb.projectId;
process.env.FUNCTIONS_EMULATOR = "true";

if (functionsEnv.hasUserEnvs(frb.cwd, "local")) {
if (functionsEnv.hasUserEnvs({ functionsSource: frb.cwd, projectId: "local" })) {
// Load user-specified environment variables.
try {
const userEnvs = functionsEnv.loadUserEnvs({ functionsSource: frb.cwd, projectId: "local" });
Expand Down
18 changes: 8 additions & 10 deletions src/functions/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,18 @@ function findEnvfiles(functionsSource: string, projectId: string, projectAlias?:
.map((p) => path.basename(p));
}

export interface UserEnvsOpts {
functionsSource: string;
projectId: string;
projectAlias?: string;
}

/**
* Checks if user has specified any environment variables for their functions.
*
* @return True if there are any user-specified environment variables
*/
export function hasUserEnvs(
functionsSource: string,
projectId: string,
projectAlias?: string
): boolean {
export function hasUserEnvs({ functionsSource, projectId, projectAlias }: UserEnvsOpts): boolean {
return findEnvfiles(functionsSource, projectId, projectAlias).length > 0;
}

Expand All @@ -223,11 +225,7 @@ export function loadUserEnvs({
functionsSource,
projectId,
projectAlias,
}: {
functionsSource: string;
projectId: string;
projectAlias?: string;
}): Record<string, string> {
}: UserEnvsOpts): Record<string, string> {
if (!previews.dotenv) {
return {};
}
Expand Down