在 github 操作上运行 firebase 模拟器

Posted

技术标签:

【中文标题】在 github 操作上运行 firebase 模拟器【英文标题】:Run firebase emulators on github actions 【发布时间】:2020-09-15 05:08:09 【问题描述】:

我正在尝试在 Github Actinos 上运行我的测试,但经过多次尝试后我无法这样做。

接收

Error: firestore: Firestore Emulator has exited because java is not installed, you can install it from https://openjdk.java.net/install/


  - name: Test
    uses: w9jds/firebase-action@v1.3.1
    env:
      JAVA_HOME: $ env.JAVA_HOME 
      FIREBASE_TOKEN: $ secrets.FIREBASE_TOKEN 
      GCLOUD_PROJECT: $ secrets.nextGCLOUD_PROJECT 
      PROJECT_ID: $ secrets.nextFIREBASE_PROJECTID 
    with:
      args: |
        setup:emulators:firestore --debug
        firebase emulators:exec --debug --only functions,firestore \"npm run test --exit -- --forceExit --detectOpenHandles\"

  - name: Print debug logs
    if: failure()
    run: |
      java --version
      ls -la
      find . -type f -name "fire*-debug.log" | xargs cat

这是动作name: Test的输出

/usr/bin/docker run --name d38e5ad93a422d456db01475acdae41155_521526 --label 3888d3 --workdir /github/workspace --rm -e JAVA_HOME -e FIREBASE_TOKEN -e GCLOUD_PROJECT -e PROJECT_ID -e INPUT_ARGS -e HOME -e GITHUB_JOB -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_REPOSITORY_OWNER -e GITHUB_RUN_ID -e GITHUB_RUN_NUMBER -e GITHUB_ACTOR -e GITHUB_WORKFLOW -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e GITHUB_EVENT_NAME -e GITHUB_SERVER_URL -e GITHUB_API_URL -e GITHUB_GRAPHQL_URL -e GITHUB_WORKSPACE -e GITHUB_ACTION -e GITHUB_EVENT_PATH -e RUNNER_OS -e RUNNER_TOOL_CACHE -e RUNNER_TEMP -e RUNNER_WORKSPACE -e ACTIONS_RUNTIME_URL -e ACTIONS_RUNTIME_TOKEN -e ACTIONS_CACHE_URL -e GITHUB_ACTIONS=true -e CI=true --network github_network_2107949b3a2c43d499d641b1c2f0fd12 -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/pwa/pwa":"/github/workspace" 3888d3:8e5ad93a422d456db01475acdae41155 setup:emulators:firestore --debug
firebase emulators:exec --debug --only functions,firestore \"npm run test --exit -- --forceExit --detectOpenHandles\"
setting firebase project to ***
Now using project ***
i  firestore: downloading cloud-firestore-emulator-v1.11.4.jar... "metadata":"emulator":"name":"firestore","message":"downloading cloud-firestore-emulator-v1.11.4.jar..."

[2020-06-06T12:59:30.184Z] > command requires scopes: ["email","openid","https://www.googleapis.com/auth/cloudplatformprojects.readonly","https://www.googleapis.com/auth/firebase","https://www.googleapis.com/auth/cloud-platform"]
[2020-06-06T12:59:30.186Z] > authorizing via FIREBASE_TOKEN environment variable
i  emulators: Starting emulators: functions, firestore "metadata":"emulator":"name":"hub","message":"Starting emulators: functions, firestore"
[2020-06-06T12:59:30.217Z] [hub] writing locator at /tmp/hub-***.json
✔  functions: Using node@10 from host. "metadata":"emulator":"name":"functions","message":"Using node@10 from host."
[2020-06-06T12:59:30.249Z] Ignoring unsupported arg: projectId "metadata":"emulator":"name":"firestore","message":"Ignoring unsupported arg: projectId"
[2020-06-06T12:59:30.249Z] Ignoring unsupported arg: auto_download "metadata":"emulator":"name":"firestore","message":"Ignoring unsupported arg: auto_download"
[2020-06-06T12:59:30.249Z] Starting Firestore Emulator with command "binary":"java","args":["-Duser.language=en","-jar","/github/home/.cache/firebase/emulators/cloud-firestore-emulator-v1.11.4.jar","--host","localhost","--port",8080,"--rules","/github/workspace/firestore.rules","--functions_emulator","localhost:5001"],"optionalArgs":["port","webchannel_port","host","rules","functions_emulator","seed_from_export"],"joinArgs":false "metadata":"emulator":"name":"firestore","message":"Starting Firestore Emulator with command \"binary\":\"java\",\"args\":[\"-Duser.language=en\",\"-jar\",\"/github/home/.cache/firebase/emulators/cloud-firestore-emulator-v1.11.4.jar\",\"--host\",\"localhost\",\"--port\",8080,\"--rules\",\"/github/workspace/firestore.rules\",\"--functions_emulator\",\"localhost:5001\"],\"optionalArgs\":[\"port\",\"webchannel_port\",\"host\",\"rules\",\"functions_emulator\",\"seed_from_export\"],\"joinArgs\":false"
i  firestore: Firestore Emulator logging to firestore-debug.log "metadata":"emulator":"name":"firestore","message":"Firestore Emulator logging to \u001b[1mfirestore-debug.log\u001b[22m"

Error: firestore: Firestore Emulator has exited because java is not installed, you can install it from https://openjdk.java.net/install/

这是动作name: Print debug logs的输出

[debug] [2020-06-06T12:59:27.289Z] ----------------------------------------------------------------------
[debug] [2020-06-06T12:59:27.291Z] Command:       /usr/local/bin/node /usr/local/bin/firebase setup:emulators:firestore --debug
[debug] [2020-06-06T12:59:27.292Z] CLI Version:   8.4.2
[debug] [2020-06-06T12:59:27.292Z] Platform:      linux
[debug] [2020-06-06T12:59:27.292Z] Node Version:  v10.21.0
[debug] [2020-06-06T12:59:27.292Z] Time:          Sat Jun 06 2020 12:59:27 GMT+0000 (Coordinated Universal Time)
[debug] [2020-06-06T12:59:27.293Z] ----------------------------------------------------------------------
[debug] [2020-06-06T12:59:27.293Z]
[info] i  firestore: downloading cloud-firestore-emulator-v1.11.4.jar... "metadata":"emulator":"name":"firestore","message":"downloading cloud-firestore-emulator-v1.11.4.jar..."
[debug] [2020-06-06T12:59:30.168Z] ----------------------------------------------------------------------
[debug] [2020-06-06T12:59:30.170Z] Command:       /usr/local/bin/node /usr/local/bin/firebase emulators:exec --debug --only functions,firestore npm run test --exit -- --forceExit --detectOpenHandles
[debug] [2020-06-06T12:59:30.171Z] CLI Version:   8.4.2
[debug] [2020-06-06T12:59:30.171Z] Platform:      linux
[debug] [2020-06-06T12:59:30.171Z] Node Version:  v10.21.0
[debug] [2020-06-06T12:59:30.171Z] Time:          Sat Jun 06 2020 12:59:30 GMT+0000 (Coordinated Universal Time)
[debug] [2020-06-06T12:59:30.171Z] ----------------------------------------------------------------------
[debug] [2020-06-06T12:59:30.171Z]
[debug] [2020-06-06T12:59:30.184Z] > command requires scopes: ["email","openid","https://www.googleapis.com/auth/cloudplatformprojects.readonly","https://www.googleapis.com/auth/firebase","https://www.googleapis.com/auth/cloud-platform"]
[debug] [2020-06-06T12:59:30.186Z] > authorizing via FIREBASE_TOKEN environment variable
[info] i  emulators: Starting emulators: functions, firestore "metadata":"emulator":"name":"hub","message":"Starting emulators: functions, firestore"
[debug] [2020-06-06T12:59:30.217Z] [hub] writing locator at /tmp/hub-***.json
[info] ✔  functions: Using node@10 from host. "metadata":"emulator":"name":"functions","message":"Using node@10 from host."
[debug] [2020-06-06T12:59:30.249Z] Ignoring unsupported arg: projectId "metadata":"emulator":"name":"firestore","message":"Ignoring unsupported arg: projectId"
[debug] [2020-06-06T12:59:30.249Z] Ignoring unsupported arg: auto_download "metadata":"emulator":"name":"firestore","message":"Ignoring unsupported arg: auto_download"
[debug] [2020-06-06T12:59:30.249Z] Starting Firestore Emulator with command "binary":"java","args":["-Duser.language=en","-jar","/github/home/.cache/firebase/emulators/cloud-firestore-emulator-v1.11.4.jar","--host","localhost","--port",8080,"--rules","/github/workspace/firestore.rules","--functions_emulator","localhost:5001"],"optionalArgs":["port","webchannel_port","host","rules","functions_emulator","seed_from_export"],"joinArgs":false "metadata":"emulator":"name":"firestore","message":"Starting Firestore Emulator with command \"binary\":\"java\",\"args\":[\"-Duser.language=en\",\"-jar\",\"/github/home/.cache/firebase/emulators/cloud-firestore-emulator-v1.11.4.jar\",\"--host\",\"localhost\",\"--port\",8080,\"--rules\",\"/github/workspace/firestore.rules\",\"--functions_emulator\",\"localhost:5001\"],\"optionalArgs\":[\"port\",\"webchannel_port\",\"host\",\"rules\",\"functions_emulator\",\"seed_from_export\"],\"joinArgs\":false"
[info] i  firestore: Firestore Emulator logging to firestore-debug.log "metadata":"emulator":"name":"firestore","message":"Firestore Emulator logging to \u001b[1mfirestore-debug.log\u001b[22m"
[error]
[error] Error: firestore: Firestore Emulator has exited because java is not installed, you can install it from https://openjdk.java.net/install/

如何在 Github Actions 上运行测试?

【问题讨论】:

【参考方案1】:

在撰写本文时,非官方的firebase-action 最近添加了 Java 支持。不幸的是,这是 Java 7,而模拟器需要 Java 8。(我在 repo 中打开了一个issue 来解决这个问题)。

或者,对于任何感兴趣的人,我发现以下方法效果很好:

name: Test Firebase rules on PR
'on': pull_request
jobs:
  test_rules:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: '12'
      - run: npm install -g firebase-tools
      - run: npm ci
      - run: firebase emulators:exec --only firestore \"npm run test --exit\"

上面的工作流文件有两个重要的说明:

    ubuntu-latest 是必需的,因为它包含 node 和 java。 需要actions/setup-node@v1 来设置节点以允许安装全局 npm 包。

另外,如果您想在上面添加部署步骤,您只需在setup-node 步骤上方添加此步骤即可进行身份验证:

      - uses: google-github-actions/setup-gcloud@master
        with:
          project_id: $ secrets.GCP_PROJECT_ID 
          service_account_key: $ secrets.GCP_SA_KEY 
          export_default_credentials: true

(这是因为export_default_credentials 将导出firebase-tools 将用于身份验证的GOOGLE_APPLICATION_CREDENTIALS 环境变量。

【讨论】:

【参考方案2】:

试试这个 -

name: next

on:
  push:
    branches: [ master ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Setting-up Node.js
        uses: actions/setup-node@v2-beta
        with:
          node-version: '10'

      - name: Installing npm dependencies
        run: npm ci --prefer-offline --no-audit --progress=false

      - name: Checking-out
        uses: actions/checkout@v2

      - name: Setting-up JAVA
        uses: actions/setup-java@v1.3.0
        with: 
          java-version: "12.x"

      - name: Setup and Test Emulator
        uses: w9jds/firebase-action@v1.3.1
        env:
          JAVA_HOME: $ env.JAVA_HOME 
          FIREBASE_TOKEN: $ secrets.FIREBASE_TOKEN 
          GCLOUD_PROJECT: $ secrets.nextGCLOUD_PROJECT 
          PROJECT_ID: $ secrets.nextFIREBASE_PROJECTID 
        with:
          args: |
            setup:emulators:firestore
            emulators:exec --only firestore \"npm run test --exit\"

【讨论】:

不..这不起作用,因为操作的方式..w9jds/firebase-action 错误..sh: 2: emulators:exec: not found 你可以在这里看到为什么...github.com/w9jds/firebase-action/blob/master/entrypoint.sh 尝试在我的答案中拆分参数 我试过了...我必须在emulator:exec 之前添加firebase 才能工作.. 但仍然是同样的问题...jmp.sh/uGMlYHb【参考方案3】:

这对我有用

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: '12'
      - name: Install Dependacies
        run: cd functions && npm install
      - name: Test to Firebase
        uses: w9jds/firebase-action@master
        with:
          args: emulators:exec --only firestore \"cd functions && npm run test --exit\"
        env:
          FIREBASE_TOKEN: $ secrets.FIREBASE_TOKEN 

【讨论】:

以上是关于在 github 操作上运行 firebase 模拟器的主要内容,如果未能解决你的问题,请参考以下文章

在颤振项目中替换 Firebase json 的 Github 操作

通过 github 操作使用 fastlane 的 Firebase 应用程序分发?

如何通过 github 操作部署 firebase 功能?

无法使用 github 操作将 React Web 应用程序部署到 Firebase 项目

在 Xcode 上打开 Firebase 分析

Firebase 托管和 Github 操作错误“auth/invalid-api-key”