Single Build and multiple deployments
Unanswered
Swedish Elkhound posted this in #help-forum
Swedish ElkhoundOP
Hello Guys, I am trying to add google secrets valult into my nextjs project so that i can load secrets from there to env. And i want to have single build and many deployments, as by just change an env var from stagging to production the db urls and everything should change ! Any comments on how should i enable this ??
7 Replies
Roseate Spoonbill
Unfortunately there are no silver bullets. In general, you can inject process.env. variables to server side, in route handlers. In pages and components, you'd probably need to use sth like https://www.npmjs.com/package/next-runtime-env as simple environment variables tend to be inlined during build in many cases.
@Roseate Spoonbill Unfortunately there are no silver bullets. In general, you can inject process.env. variables to server side, in route handlers. In pages and components, you'd probably need to use sth like https://www.npmjs.com/package/next-runtime-env as simple environment variables tend to be inlined during build in many cases.
Swedish ElkhoundOP
I was trying to do this, export async function register() {
try {
const secrets = await initSecrets()
console.log('secrets', secrets)
} catch (error) {
console.log('Failed to initialize secrets:', error)
}
}
in my instrumentation.ts file so that when the server starts this script gets executed but ran into this error ReferenceError: __import_unsupported is not defined
try {
const secrets = await initSecrets()
console.log('secrets', secrets)
} catch (error) {
console.log('Failed to initialize secrets:', error)
}
}
in my instrumentation.ts file so that when the server starts this script gets executed but ran into this error ReferenceError: __import_unsupported is not defined
Roseate Spoonbill
I am not sure if this is related, but try to put import for init secrets within the register function like they show in the docs: https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation#importing-files-with-side-effects
@Roseate Spoonbill I am not sure if this is related, but try to put import for init secrets within the register function like they show in the docs: https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation#importing-files-with-side-effects
Swedish ElkhoundOP
export async function register() {
// console.log('NEXT_RUNTIME', process.env.NEXT_RUNTIME)
const { initSecrets } = await import('./src/lib/gcp-key-vault-config')
await initSecrets()
console.log(process.env.NEXTAUTH_SECRET, process.env.stag_DATABASE_URL)
}
Yep this fixed that issue but when i load my application i am getting [next-auth][warn][NO_SECRET]
https://next-auth.js.org/warnings#no_secret
// console.log('NEXT_RUNTIME', process.env.NEXT_RUNTIME)
const { initSecrets } = await import('./src/lib/gcp-key-vault-config')
await initSecrets()
console.log(process.env.NEXTAUTH_SECRET, process.env.stag_DATABASE_URL)
}
Yep this fixed that issue but when i load my application i am getting [next-auth][warn][NO_SECRET]
https://next-auth.js.org/warnings#no_secret
even though in this function nextauth secret is getting logged
@Roseate Spoonbill Unfortunately there are no silver bullets. In general, you can inject process.env. variables to server side, in route handlers. In pages and components, you'd probably need to use sth like https://www.npmjs.com/package/next-runtime-env as simple environment variables tend to be inlined during build in many cases.
Swedish ElkhoundOP
This package seems to be used specifically for client side variables ? i my usecase i am not able to access the server side env var too after populating it using this script !
Roseate Spoonbill
I wrote about it before you clarified the usage.
NEXTAUTH_SECRET
is generated in development mode, so that's why the warning is there. Are you seeing this as well in production mode? Plus, do you set the NEXTAUTH_SECRET
variable? I'm sorry, but it's not clear to me which variable s and where are you passing.