英文:
Can We Access Github secrets variable in our firebase cloud functions at run time? if Yes, Please explain the process with example
问题
function GetPolicy(timeoutSeconds=60, maxInstances=15){
let config = functions.config()
let environment = config.app.enviornment;
if (environment == 'local') {
return {
timeoutSeconds: 60,
maxInstances: 2,
vpcConnector : "******",
vpcConnectorEgressSettings: '*********'
}
} else {
return {
timeoutSeconds: timeoutSeconds != null ? timeoutSeconds: 60,
// memory: '2GB',
// retry: true,
maxInstances: maxInstances != null ? maxInstances: 15,
vpcConnector : "********",
vpcConnectorEgressSettings: '**********'
}
}
}
英文:
function GetPolicy(timeoutSeconds=60, maxInstances=15){
let config = functions.config()
let enviornment = config.app.enviornment;
if (enviornment == 'local') {
return {
timeoutSeconds: 60,
maxInstances: 2,
vpcConnector : "******",
vpcConnectorEgressSettings: '*********'
}
} else {
return {
timeoutSeconds: timeoutSeconds != null ? timeoutSeconds: 60,
// memory: '2GB',
// retry: true,
maxInstances: maxInstances != null ? maxInstances: 15,
vpcConnector : "********",
vpcConnectorEgressSettings: '**********'
}
}
}
I am exploring GitHub secrets along with GitHub Actions to deploy firebase function on cloud. In my app I am using firebase.config() methods to retrieve my secret values like environment etc. which I want to put in GitHub secrets and want to retrieve from there instead of retrieving it from functions.config() method. Is there any way to do so, if yes please share it here with a short example.
答案1
得分: 1
配置 Firebase 函数环境时,可以使用以下方式组合 defineString :
const { onRequest } = require('firebase-functions/v2/https');
const { defineInt } = require('firebase-functions/params');
const minInstancesConfig = defineInt('EXAMPLE_MININSTANCES');
export const example = onRequest(
{ minInstances: minInstancesConfig },
(req, res) => { }
)
并且要在开发和生产环境之间进行委派:
const { onRequest } = require('firebase-functions/v2/https');
const environment = params.defineString('ENVIRONMENT', { default: 'dev' });
// 使用内置比较器
const minInstancesConfig = environment.equals('PRODUCTION').thenElse(10, 1);
export const helloWorld = onRequest(
{ minInstances: minInstancesConfig },
(req, res) => { }
)
根据您的函数配置,您必须像这样进行配置,当然,您也可以在全局配置中使用 setGlobalOptions
来使用这些技巧,如下所示:
const { setGlobalOptions } = require("firebase-functions/v2/options");
const environment = params.defineString('ENVIRONMENT', { default: 'dev' });
// 使用内置比较器
const minInstancesConfig = environment.equals('PRODUCTION').thenElse(10, 1);
setGlobalOptions({ maxInstances: minInstancesConfig });
参考:参数值和表达式
英文:
For configuring the firebase functions environment you can use defineString combinations as follows :
const { onRequest } = require('firebase-functions/v2/https');
const { defineInt } = require('firebase-functions/params');
const minInstancesConfig = defineInt('EXAMPLE_MININSTANCES');
export const example = onRequest(
{ minInstances: minInstancesConfig },
(req, res) => { }
)
And to delegate between development and production
const { onRequest } = require('firebase-functions/v2/https');
const environment = params.defineString(‘ENVIRONMENT’, {default: 'dev'});
// use built-in comparators
const minInstancesConfig = environment.equals('PRODUCTION').thenElse(10, 1);
export const helloWorld = onRequest(
{ minInstances: minInstancesConfig },
(req, res) => { }
)
As per your function configuration you have to configure like this way and offcourse you can use these tricks on global configuration using setGlobalOptions
as well like :
const {setGlobalOptions} = require("firebase-functions/v2/options");
const environment = params.defineString(‘ENVIRONMENT’, {default: 'dev'});
// use built-in comparators
const minInstancesConfig = environment.equals('PRODUCTION').thenElse(10, 1);
setGlobalOptions({maxInstances: minInstancesConfig});
Reference : Parameter values and expressions
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论