在 GitHub Actions 中使用 Python 运行 Firestore 模拟器

Aze*_*der 3 python google-cloud-platform google-cloud-firestore github-actions

我有一个使用Firestore. 在此之前我正在尝试编写 GitHub 操作,我现在使用Travis,现在我想转向 GitHub 操作。在Travis上一切正常,但在 GitHub Actions 上出现错误。

错误:(此错误不是问题模拟器应该在没有身份验证的情况下工作,因为它在其他一些存储库中工作但不在此上)

E: google.auth.exceptions.DefaultCredentialsError: 无法自动确定凭据。请设置 GOOGLE_APPLICATION_CREDENTIALS 或明确创建凭据并重新运行应用程序。有关更多信息,请参阅https://cloud.google.com/docs/authentication/getting-started

工作流程(不工作): Repo FireO Python

name: Python package

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python-version: [3.7]

    steps:
      - uses: actions/checkout@v2
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          python -m pip install pytest
          python -m pip install -e .

      - name: Setup NodeJS
        uses: actions/setup-node@v2
        with:
          node-version: "14.x"
      - name: Start Firestore emulator
        run: |
          npm i -g firebase firebase-tools
          firebase setup:emulators:firestore
          firebase emulators:start --only firestore &

      - name: Test with pytest
        run: |
          pytest
        env:
          FIRESTORE_EMULATOR_HOST: localhost:8080
Run Code Online (Sandbox Code Playgroud)

我不知道为什么 firestore 模拟器不工作。我有另一个回购,它工作正常。

在另一个 Repo WorkFlow Repo FireO NodeJs 中工作

    name: Node.js CI

    on:
    push:
        branches: [main]
    pull_request:

    jobs:
    build:
        runs-on: ubuntu-latest

        strategy:
        matrix:
            node-version: [12.x, 14.x, 15.x]
            # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

        steps:
        - uses: actions/checkout@v2
        - name: Use Node.js ${{ matrix.node-version }}
            uses: actions/setup-node@v1
            with:
            node-version: ${{ matrix.node-version }}

        - name: Install Firestore CLI and start emulators
            run: |
                npm i -g firebase firebase-tools
                firebase setup:emulators:firestore
                firebase emulators:start --only firestore &
        - name: Test Fireo Package
            run: |
                npm ci
                npm test
            env:
                FIRESTORE_EMULATOR_HOST: localhost:8080
Run Code Online (Sandbox Code Playgroud)

当我尝试用 python 测试时,你能指出我遗漏了什么吗?

要运行模拟器,您无需对任何内容进行身份验证 在此Repo 中,它无需身份验证即可工作,但在我拥有 python 的此Repo 中,它无法正常工作。我想知道为什么它在某个地方有效而不是在其他地方

更新

我尝试通过 envGCLOUD_PROJECT="your-project-id"和命令标志设置项目 ID --project,但它仍然无法正常工作。因为问题与id我认为pythonpytest无法检测到firestore模拟器正在运行的一些原因无关。我不知道为什么会发生这种情况,可能是由于语言差异,firestore 正在运行npm,测试正在运行,python也许 GitHub 在不同的地方设置了这两个东西。因为一切都在NodeJS Repo 上运行,因为 firestore 和测试都在运行,nodeJS我认为我们必须以某种方式将它们同步到一个地方,以便它们可以相互检测。

小智 7

我想你只需要设置一个项目ID,如文档描述这里

从任何其他环境连接到 Cloud Firestore 模拟器时,您需要指定项目 ID。您可以直接将项目 ID 传递给 initializeApp 或设置 GCLOUD_PROJECT 环境变量。请注意,您不需要使用真实的 Firebase 项目 ID;Cloud Firestore 模拟器将接受任何项目 ID,只要它具有有效的格式。

export GCLOUD_PROJECT="your-project-id"
Run Code Online (Sandbox Code Playgroud)