使用 Github 操作部署 Firebase Cloud 函数时的环境变量

Environment variables when deploying Firebase Cloud functions with Github Actions

我一直在尝试使用 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 文件,它在其中写入环境变量(保存在 github 秘密中) 然后安装依赖项, 最后部署云功能

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

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)

提前致谢

我解决了这个问题。答案很简单:我没有遵循使用“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 }}