我们能在运行时访问GitHub的机密变量吗?如果可以,请用示例解释该过程。

huangapple go评论97阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年7月20日 18:22:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定