Ruben
Ruben4mo ago

Preview deployment fails with: '✖ Please set CONVEX_DEPLOY_KEY to a new key which you can [...]'

Since last week I'm unable to make preview deployments for my Vercel app. The deployment is able to deploy all convex functions and even prints: Ran "npm run build" with environment variable "NEXT_PUBLIC_CONVEX_URL" set. However after logging ✔ Deployed Convex functions to https:[...].convex.cloud it errors with the following error message: ✖ Please set CONVEX_DEPLOY_KEY to a new key which you can find on your Convex dashboard. Which seems odd as the functions itself have been successfully been deployed so it would seem the CONVEX_DEPLOY_KEY is valid. This error seems to originate from here: https://github.com/get-convex/convex-backend/blob/aef1e1bd991d0aa46e530f3dfc88a4e8335aca7d/npm-packages/convex/src/cli/lib/deployment.ts#L181 A small snippet from the copied github link:
export async function deploymentNameFromAdminKeyOrCrash(
ctx: Context,
adminKey: string,
) {
const deploymentName = deploymentNameFromAdminKey(adminKey);
if (deploymentName === null) {
return await ctx.crash({
exitCode: 1,
errorType: "fatal",
printedMessage: `Please set ${CONVEX_DEPLOY_KEY_ENV_VAR_NAME} to a new key which you can find on your Convex dashboard.`,
});
}
return deploymentName;
}

function deploymentNameFromAdminKey(adminKey: string) {
const parts = adminKey.split("|");
if (parts.length === 1) {
return null;
}
if (isPreviewDeployKey(adminKey)) {
// Preview deploy keys do not contain a deployment name.
return null;
}
return stripDeploymentTypePrefix(parts[0]);
}
export async function deploymentNameFromAdminKeyOrCrash(
ctx: Context,
adminKey: string,
) {
const deploymentName = deploymentNameFromAdminKey(adminKey);
if (deploymentName === null) {
return await ctx.crash({
exitCode: 1,
errorType: "fatal",
printedMessage: `Please set ${CONVEX_DEPLOY_KEY_ENV_VAR_NAME} to a new key which you can find on your Convex dashboard.`,
});
}
return deploymentName;
}

function deploymentNameFromAdminKey(adminKey: string) {
const parts = adminKey.split("|");
if (parts.length === 1) {
return null;
}
if (isPreviewDeployKey(adminKey)) {
// Preview deploy keys do not contain a deployment name.
return null;
}
return stripDeploymentTypePrefix(parts[0]);
}
Here deploymentNameFromAdminKeyOrCrash will throw the specified error when it's called with preview adminKey, which it probably does because this is a preview deployment. Hopefully someone can help us out!
GitHub
convex-backend/npm-packages/convex/src/cli/lib/deployment.ts at aef...
The Convex open-source backend. Contribute to get-convex/convex-backend development by creating an account on GitHub.
8 Replies
Convex Bot
Convex Bot4mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
sshader
sshader4mo ago
What’s your full build command in Vercel? Any other npx convex commands in there or additional flags to the deploy command? Does your preview key look something like preview:team-slug:project-slug|some stuff
Ruben
RubenOP4mo ago
The build command is: npx convex deploy --cmd 'npm run build' && npx convex run migrations --prod and npm run build just runs next build And yes the preview key looks like preview:[team-name]:[project-name]|ey...
sshader
sshader4mo ago
So that error is almost certainly from the npx convex run afterwards (and the npx convex deploy is succeeding). We can fix on our end to allow npx convex run --prod with a preview deploy key, but also I'm guessing you don't necessarily want to run migrations against prod on every deploy to a preview. I'd suggest checking the build environment in your build command and only doing the npx convex run --prod in the production environment (https://vercel.com/docs/projects/environment-variables/system-environment-variables#VERCEL_ENV)
System environment variables
Vercel environment variables that are automatically populated by the system, such as the URL of the deployment or the name of the Git branch deployed.
Ruben
RubenOP4mo ago
But I would like to run the migrations on the preview environment, so would that be —preview then? Also wouldn’t it be nice to be call it with —auto where convex automatically infers which environment to run on depending on the specified token? So the same commands works independently of the environment it is called from? While still allowing someone to call it with —prod to force someone to run the migrations on prod
sshader
sshader4mo ago
npx convex run --preview-name $VERCEL_GIT_COMMIT_REF sounds like what you want
Ruben
RubenOP4mo ago
Ah thanks a lot, of course it does'nt make much sense to do run production migrations on a preview deploy, thanks for the help! I did however find another issue, and I'm not sure if it intentional or I just missed some documentation. Everytime I do a preview deployment a new environment is created and the NEXT_PUBLIC_CONVEX_URL is different and the newly created database is empty even though I'm releasing from the same branch. Do you know if this is intentional or/or if I can disable this and reuse the same preview environment for the same branch?
sshader
sshader4mo ago
Check out https://docs.convex.dev/production/hosting/preview-deployments + https://stack.convex.dev/seeding-data-for-preview-deployments Short answer is yes, it's intentional that the database is always reset, and there's no way to reuse a deployment (even on the same branch, there are things like schema changes that might make it impossible to reuse an exisiting deployment)
Preview Deployments | Convex Developer Hub
Use Convex with your hosting provider's preview deployments
Seeding Data for Preview Deployments
Now that we've launched Preview Deployments on Convex, you can test out backend changes easier than ever. But you may want to seed your project with d...

Did you find this page helpful?