Skip to content

Commit 3e4940b

Browse files
atrakhConvex, Inc.
authored andcommitted
add sentinels for cli CONVEX_DEPLOY_KEY (#43076)
GitOrigin-RevId: 5461b97c477132229f8a655eac06a2a50013309b
1 parent e1220c0 commit 3e4940b

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

src/cli/lib/deploymentSelection.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
CONVEX_SELF_HOSTED_URL_VAR_NAME,
2626
ENV_VAR_FILE_PATH,
2727
bigBrainAPI,
28+
processDeployKeyValue,
2829
} from "./utils/utils.js";
2930
import * as dotenv from "dotenv";
3031

@@ -84,7 +85,8 @@ export async function initializeBigBrainAuth(
8485
});
8586
}
8687
const config = dotenv.parse(existingFile);
87-
const deployKey = config[CONVEX_DEPLOY_KEY_ENV_VAR_NAME];
88+
const rawDeployKey = config[CONVEX_DEPLOY_KEY_ENV_VAR_NAME];
89+
const deployKey = await processDeployKeyValue(ctx, rawDeployKey);
8890
if (deployKey !== undefined) {
8991
const bigBrainAuth = getBigBrainAuth(ctx, {
9092
previewDeployKey: isPreviewDeployKey(deployKey) ? deployKey : null,
@@ -97,7 +99,8 @@ export async function initializeBigBrainAuth(
9799
}
98100
dotenv.config({ path: ENV_VAR_FILE_PATH });
99101
dotenv.config();
100-
const deployKey = process.env[CONVEX_DEPLOY_KEY_ENV_VAR_NAME];
102+
const rawDeployKey = process.env[CONVEX_DEPLOY_KEY_ENV_VAR_NAME];
103+
const deployKey = await processDeployKeyValue(ctx, rawDeployKey);
101104
if (deployKey !== undefined) {
102105
const bigBrainAuth = getBigBrainAuth(ctx, {
103106
previewDeployKey: isPreviewDeployKey(deployKey) ? deployKey : null,
@@ -425,8 +428,12 @@ async function getDeploymentSelectionFromEnv(
425428
): Promise<
426429
{ kind: "success"; metadata: DeploymentSelection } | { kind: "unknown" }
427430
> {
428-
const deployKey = getEnv(CONVEX_DEPLOY_KEY_ENV_VAR_NAME);
429-
if (deployKey !== null) {
431+
const rawDeployKey = getEnv(CONVEX_DEPLOY_KEY_ENV_VAR_NAME);
432+
const deployKey = await processDeployKeyValue(
433+
ctx,
434+
rawDeployKey === null ? undefined : rawDeployKey,
435+
);
436+
if (deployKey !== undefined) {
430437
const deployKeyType = isPreviewDeployKey(deployKey)
431438
? "preview"
432439
: isProjectKey(deployKey)

src/cli/lib/utils/utils.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,45 @@ const MAX_RETRIES = 6;
4040
// After 3 retries, log a progress message that we're retrying the request
4141
const RETRY_LOG_THRESHOLD = 3;
4242

43+
/**
44+
* Processes the CONVEX_DEPLOY_KEY value to handle special sentinel values.
45+
*
46+
* - If the value is `<ignore_deploy_key>`, treats it as if the env var isn't set (returns undefined)
47+
* - If the value matches `<missing_deploy_key:$STRING>`, crashes with the message in $STRING
48+
* - Otherwise returns the value as-is
49+
*
50+
* @param ctx Context for crashing if needed
51+
* @param deployKey The raw deploy key value from environment or config
52+
* @returns The processed deploy key value or undefined
53+
*/
54+
export async function processDeployKeyValue(
55+
ctx: Context,
56+
deployKey: string | undefined,
57+
): Promise<string | undefined> {
58+
if (deployKey === undefined) {
59+
return undefined;
60+
}
61+
62+
// Check for <ignore_deploy_key> sentinel
63+
if (deployKey === "<ignore_deploy_key>") {
64+
return undefined;
65+
}
66+
67+
// Check for <missing_deploy_key:$STRING> sentinel
68+
const missingKeyPattern = /^<missing_deploy_key:(.+)>$/;
69+
const match = deployKey.match(missingKeyPattern);
70+
if (match) {
71+
const errorMessage = match[1];
72+
return await ctx.crash({
73+
exitCode: 1,
74+
errorType: "fatal",
75+
printedMessage: errorMessage,
76+
});
77+
}
78+
79+
return deployKey;
80+
}
81+
4382
export function parsePositiveInteger(value: string) {
4483
const parsedValue = parseInteger(value);
4584
if (parsedValue <= 0) {

0 commit comments

Comments
 (0)