Reputation: 16066
I've been trying to set up a schedule for a pub-sub function, depending on the environment I want to deploy, the process I'm following is simply adding those cron string parameters as environment variables.
.env.dev:
CRON_4H=every 4 hours
CRON_12H=every 12 hours
CRON_24H=every 24 hours
And use that parameter on the function definition
exports.myfunction = functions.pubsub.schedule(process.env.CRON_12H)
.onRun(context => {
// ETC
});
I'm getting the following error:
Cannot create a scheduler job without a schedule
{
"id": "myfunction",
"project": "----",
"region": "----",
"entryPoint": "----",
"platform": "gcfv1",
"runtime": "nodejs16",
"scheduleTrigger": {
"schedule": "",
"timeZone": null
},
"labels": {
"deployment-tool": "cli-firebase"
},
"availableMemoryMb": 512,
"timeoutSeconds": 300,
"environmentVariables": {
"MIN_INSTANCES_1": "0",
"MIN_INSTANCES_3": "0",
"MIN_INSTANCES_5": "0",
CRON_4H=every 4 hours,
CRON_12H=every 12 hours,
CRON_24H=every 24 hours,
"FIREBASE_CONFIG": "--------",
"codebase": "default",
"securityLevel": "SECURE_ALWAYS"
}
it seems like the env vars are empty by the time of deployment, schedule and timezone indeed are empty, any advice on this?
EDIT: seems like I'm setting runtime env vars, how can I define build environment vars for a firebase function?
Upvotes: 1
Views: 189
Reputation: 16066
I couldn't find a way to set "build" variables, the only var I can use at that time was the project ID based on the firebase config which seems available during "build", so I changed the approach to the following:
In its own JS file to be imported:
let envProjectId = ''
if (process.env && process.env.FIREBASE_CONFIG) {
envProjectId = JSON.parse(process.env.FIREBASE_CONFIG).projectId
}
exports.instances =
envProjectId !== 'prod-projectI-id'
? {
MIN_INSTANCES_1: 0,
MIN_INSTANCES_3: 0,
MIN_INSTANCES_5: 0,
}
: {
MIN_INSTANCES_1: 1,
MIN_INSTANCES_3: 3,
MIN_INSTANCES_5: 5,
}
it's not as elegant as putting all those vars into a proper env file but worked for me.
Upvotes: 2