【问题标题】:Environment variables when deploying Firebase Cloud functions with Github Actions使用 Github Actions 部署 Firebase Cloud 函数时的环境变量
【发布时间】:2021-09-16 11:32:54
【问题描述】:

我一直在尝试使用 Github 操作 CI/CD 工作流自动部署 Firebase 云功能。 这些函数是使用 NodeJs、Express 和 Typescript 开发的。并且所有环境变量都保存在 .env 文件中,在 github 上没有跟踪(原因很明显)

main.yaml 文件(在 .github/workflows/ 中)

name: CI/CD

on:
  push:
    branches: [ deploy ]
  pull_request:
    branches: [ deploy ]

  workflow_dispatch:

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: create env file
        run: |
          cd functions
          touch .env
          echo "${{ secrets.ENV_VARS }}" >> .env
    

      - name: Install npm packages
        run: |
          cd functions
          npm install
    
      - name: Deploy to Firebase
        uses: w9jds/firebase-action@master
        with:
          args: deploy
        env:
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}

工作流首先创建一个 .env 文件,在其中写入 env 变量(保存在 github secrets 中) 然后安装依赖项, 最后部署云功能

执行这些步骤没有任何问题,直到出现此错误的部署部分

Error: Service account object must contain a string "project_id" property.
    at FirebaseAppError.FirebaseError [as constructor] (/github/workspace/functions/node_modules/firebase-admin/lib/utils/error.js:44:28)
    at FirebaseAppError.PrefixedFirebaseError [as constructor] (/github/workspace/functions/node_modules/firebase-admin/lib/utils/error.js:90:28)
    at new FirebaseAppError (/github/workspace/functions/node_modules/firebase-admin/lib/utils/error.js:125:28)
    at new ServiceAccount (/github/workspace/functions/node_modules/firebase-admin/lib/credential/credential-internal.js:134:19)
    at new ServiceAccountCredential (/github/workspace/functions/node_modules/firebase-admin/lib/credential/credential-internal.js:68:15)
    at Object.exports.cert (/github/workspace/functions/node_modules/firebase-admin/lib/credential/credential.js:34:54)
    at Object.<anonymous> (/github/workspace/functions/lib/config/firebase.js:10:34)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)

提前谢谢你

【问题讨论】:

  • 嗨!你检查过这个reference吗?看起来您的 project_id 在 firebase 凭据中丢失了。确保您的 FIREBASE_PROJECT_ID 变量实际上包含非空值。

标签: google-cloud-functions environment-variables github-actions cicd env-file


【解决方案1】:

我解决了这个问题。答案很简单:我没有遵循使用“w9jds/firebase-action@master”进行部署的不同教程,而是简单地使用了 firebase deploy :)

新的 main.yaml

name: CI/CD

on:
  push:
    branches: [ deploy]
  pull_request:
    branches: [ deploy]

  workflow_dispatch:

jobs:
  main:
    runs-on: ubuntu-latest

    steps:
      
      - uses: actions/checkout@v2

      # Environment variables
      - name: create env file
        run: |
          cd functions
          touch .env
          echo "${{ secrets.ENV_VARS }}" >> .env

      # Install npm packages and firebase
      - name: Install npm packages
        run: |
          cd functions
          npm install
          npm audit fix
          npm install firebase-tools
          
      # Run tests
      - name: Run tests
        run: |
          cd functions
          npm run test
          
      # Deploying the functions to firebase
      - name: Deploy to Firebase
        run: |
          cd functions
          npm run deploy
        env:
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}


【讨论】:

    猜你喜欢
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 2019-07-13
    • 2022-11-11
    • 2020-04-20
    • 2022-11-20
    相关资源
    最近更新 更多