英文:
Google Secrets Manager with Firebase Functions
问题
如何为cron传递不同的值,以便在不同环境中安排函数在不同时间运行?
之前,cron值是硬编码在计划中的,像这样:
export const nameFunction = functions
.region(...regions)
.runWith({
timeoutSeconds: 540,
memory: '128MB',
})
.pubsub.schedule("*/5 * * * *")
.timeZone('Europe/Copenhagen')
.onRun(async (context) => {
我的目标是将其更改为配置或秘密,以便根据部署环境的不同而变化。例如:开发环境 - 5分钟,暂存环境 - 10分钟,生产环境 - 1天。
我尝试了一些方法,但它似乎不起作用。这是我最新的代码片段:
export const nameFunction = functions
.region(...regions)
.runWith({
timeoutSeconds: 540,
memory: '128MB',
secrets: ['CRON'],
})
.pubsub.schedule(process.env.CRON as string)
.timeZone('Europe/Copenhagen')
.onRun(async (context) => {
英文:
How can I pass different values for the cron so that I can schedule the function at different times in different environments?
Previously I had the cron value hardcoded in the schedule, like:
export const nameFunction = functions
.region(...regions)
.runWith({
timeoutSeconds: 540,
memory: '128MB',
})
.pubsub.schedule("*/5 * * * *")
.timeZone('Europe/Copenhagen')
.onRun(async (context) => {
My goal is to change that to a config or secret in order to make it variable accordingly to the environment that it's deployed. Example: Dev: 5 minutes, Staging - 10 minutes, Prod - 1 day.
I've tried a couple of things but it simply doesn't work. This is my latest piece of code.
export const nameFunction = functions
.region(...regions)
.runWith({
timeoutSeconds: 540,
memory: '128MB',
secrets: ['CRON'],
})
.pubsub.schedule(process.env.CRON as string)
.timeZone('Europe/Copenhagen')
.onRun(async (context) => {
答案1
得分: 3
以下是已翻译的内容:
你可以使用以下技巧来动态分配计划时间:
- 在你的Firebase项目中,为每个环境设置适当的秘密或环境变量。并在.env文件中定义1个ENV变量,如
dev
、staging
、prod
。
你可以通过运行以下命令设置环境变量:
# 为开发环境设置环境变量
firebase functions:config:set DEV_SCHEDULE_INTERVAL="5"
# 为暂存环境设置环境变量
firebase functions:config:set STAGING_SCHEDULE_INTERVAL="10"
# 为生产环境设置环境变量
# cron表达式*/1440 * * * * * 意味着"每1440分钟",或"每24小时"。
firebase functions:config:set PRODUCTION_SCHEDULE_INTERVAL="1440"
- 使用适当的计划间隔,基于环境,使用一些便捷的开关案例:
import * as functions from 'firebase-functions';
const regions = ["us-central1", "europe-west1"];
const getScheduleInterval = (): string => {
const environment = process.env.FUNCTION_ENV || 'dev';
switch (environment) {
case 'staging':
return `*/${process.env.STAGING_SCHEDULE_INTERVAL || '10'} * * * * *`;
case 'production':
return `*/${process.env.PRODUCTION_SCHEDULE_INTERVAL || '1440'} * * * * *`;
case 'dev':
default:
return `*/${process.env.DEV_SCHEDULE_INTERVAL || '5'} * * * * *`;
}
};
export const nameFunction = functions
.region(...regions)
.runWith({
timeoutSeconds: 540,
memory: '128MB',
secrets: ["DEV_SCHEDULE_INTERVAL",
"STAGING_SCHEDULE_INTERVAL",
"PRODUCTION_SCHEDULE_INTERVAL"]
})
.pubsub.schedule(getScheduleInterval()) // 动态设置计划间隔
.timeZone('Europe/Copenhagen')
.onRun(async (context) => {
// 在这里放置你的函数逻辑
});
在Firebase中设置的秘密可以在函数内使用process.env
引用,请参考以下链接:
参考:创建和使用秘密
英文:
You can use the following technique to dynamically assign schedule time :
- In your Firebase project, set the appropriate secrets or environment variables for each environment. And also define 1 ENV variable in the .env file like
dev
,staging
,prod
.
You can set the environment variables by running the following commands:
# Set the environment variables for the development environment
firebase functions:config:set DEV_SCHEDULE_INTERVAL="5"
# Set the environment variables for the staging environment
firebase functions:config:set STAGING_SCHEDULE_INTERVAL="10"
# Set the environment variables for the production environment
#The cron expression */1440 * * * * * means "every 1440 minutes," or "every 24 hours".
firebase functions:config:set PRODUCTION_SCHEDULE_INTERVAL="1440"
- Use the appropriate schedule interval based on the environment using some handy switch case :
import * as functions from 'firebase-functions';
const regions = ["us-central1", "europe-west1"];
const getScheduleInterval = (): string => {
const environment = process.env.FUNCTION_ENV || 'dev';
switch (environment) {
case 'staging':
return `*/${process.env.STAGING_SCHEDULE_INTERVAL || '10'} * * * * *`;
case 'production':
return `*/${process.env.PRODUCTION_SCHEDULE_INTERVAL || '1440'} * * * * *`;
case 'dev':
default:
return `*/${process.env.DEV_SCHEDULE_INTERVAL || '5'} * * * * *`;
}
};
export const nameFunction = functions
.region(...regions)
.runWith({
timeoutSeconds: 540,
memory: '128MB',
secrets: ["DEV_SCHEDULE_INTERVAL",
"STAGING_SCHEDULE_INTERVAL",
"PRODUCTION_SCHEDULE_INTERVAL"]
})
.pubsub.schedule(getScheduleInterval()) // dynamically set the schedule interval
.timeZone('Europe/Copenhagen')
.onRun(async (context) => {
// Your function logic here
});
Secrets set in firebase will be referenceable with process.env
inside the function refer the below link.
Reference : Create and use a secret
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论