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

use*_*017 2 environment-variables google-cloud-functions env-file github-actions cicd

我一直在尝试使用 Github actions 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 }}

Run Code Online (Sandbox Code Playgroud)

该工作流程首先创建一个 .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)
Run Code Online (Sandbox Code Playgroud)

先感谢您

use*_*017 6

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

新的 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 }}


Run Code Online (Sandbox Code Playgroud)