简体   繁体   English

GitHub 持续部署到 Firebase 托管和环境变量

[英]GitHub continuous deployment to Firebase hosting and env variables

I am currently working on a React application with Firebase initialised.我目前正在开发一个初始化 Firebase 的 React 应用程序。 I am initialising my React App with Firebase by doing the following:我正在通过执行以下操作使用 Firebase 初始化我的 React 应用程序:

import firebase from 'firebase/app'
import 'firebase/auth'
import 'firebase/firestore'

const app = firebase.initializeApp({
apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
  authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
  projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
  storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
  messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID,
  appId: process.env.REACT_APP_FIREBASE_APP_ID
})

const db = firebase.firestore()

export { db, app }

For obvious reasons, I do not want to push my env file to GitHub.出于显而易见的原因,我不想将我的 env 文件推送到 GitHub。 But if I do not, my application build fails.但如果我不这样做,我的应用程序构建就会失败。 I am wondering how I can configure GitHub to house my env variables and for my production build to use the GitHub env variables.我想知道如何配置 GitHub 来容纳我的环境变量,并让我的生产构建使用 GitHub 环境变量。 I have attempted to set up a GitHub build workflow, which contains the env variables, but my build still seems to fail.我试图建立一个包含环境变量的 GitHub 构建工作流,但我的构建似乎仍然失败。

On a separate note, I am curious how much of a security risk it is for my Firebase config to be exposed.另外,我很好奇我的 Firebase 配置暴露有多大的安全风险。 I read if my application is using an email/password sign-in method, I should protect these variables.我读到如果我的应用程序使用电子邮件/密码登录方法,我应该保护这些变量。 Any advice, suggestions, critiques would be much appreciated.任何意见,建议,批评将不胜感激。

Just use GitHub Actions with secrets and echo with the runner:只需使用带有秘密的 GitHub 操作并与跑步者呼应:

- name: 🗄️ Provide credentials
  id: firebase-credentials
  run: echo 'const app = firebase.initializeApp({apiKey: "${{ secrets.FIREBASE_API_KEY }}", ... })' > ./some.js

You can use Firebase Cloud Functions and set an environment variable in the terminal:您可以使用Firebase Cloud Functions并在终端中设置环境变量

firebase functions:config:set someservice.key="API KEY" someservice.id="CLIENT ID"

For example, to set up the Cloud Function for Firebase:例如,要为 Firebase 设置云 Function:

firebase init functions
npm install --save firebase-functions@latest
npm install -g firebase-tools

You can then access the variables from a function:然后,您可以从 function 访问变量:

const functions = require('firebase-functions');
const request = require('request-promise');

exports.userCreated = functions.database.ref('/users/{id}').onWrite(event => {
  let email = event.data.child('email').val();

  return request({
    url: 'https://someservice.com/api/some/call',
    headers: {
      'X-Client-ID': functions.config().someservice.id,
      'Authorization': `Bearer ${functions.config().someservice.key}`
    },
    body: {email: email}
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM