firebase-tools icon indicating copy to clipboard operation
firebase-tools copied to clipboard

Check for readyness

Open levino opened this issue 3 years ago • 3 comments

For CI we want to run the emulator suite in a docker service container (github actions). We need to able to check from the main script if the emulator is "ready". That means a couple of things:

  1. The emulator container will start with an empty folder. We then wait in our custom "firebase-tools" container for the firebase.json to show up.
  2. Once the firebase.json has shown up (because the main github action thread ran "checkout") we extract the location of the functions package.json and wait for it to show up.
  3. Once the functions package.json has shown up, we can finally start the emulators, as they would crash if the functions package.json is not present (btw: it suffices to put in {} to this package.json so the check is pretty pointless...). Here is the script for step 1. - 3. The functions package.json shows up once we have run our build step.
  4. In the main github action thread we now have to wait for the emulators to actually have loaded ALL FUNCTIONS. <--- This I do not know how to do! Currently I use waitFor localhost:4000 which returns too early!
  5. We can run our test suite.

Is there something like http://localhost:4000/functions/health or so?

levino avatar Sep 23 '22 09:09 levino

I now added a function health which just returns 200 for the time being. I mount this via the hosting emulator and ping it until it comes up. Okayish workaround.

levino avatar Sep 23 '22 11:09 levino

Have you tried firebase emulators:exec './your-test-script'? It will only run the argument script once all emulators have been started, and will shutdown the emulators when done.

yuchenshi avatar Sep 26 '22 20:09 yuchenshi

I've been working on this recently.

So far my approach is to use docker compose to bring up the stack (where the FB emulator suite is a dependent service) and I'm waiting for FB to be ready using depends_on and docker health checks.

version: "3.9"
services:
  myapp:
    build:
      context: .
      target: dev
    container_name: myapp
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    environment:
      NODE_ENV: dev
      FIREBASE_PROJECT: demo-project
      FIREBASE_AUTH_EMULATOR_HOST: fb-emulator-suite:9099
    depends_on:
      fb-emulator-suite:
        condition: service_healthy

 fb-emulator-suite:
    container_name: fb-emulator-suite
    image: us-central1-docker.pkg.dev/XXX/cicd/fb-emulator-suite:latest
    ports:
      - "4000:4000"
      - "4400:4400"
      - "9099:9099"
    restart: always
    healthcheck:
      test: wget --no-verbose --tries=1 --spider http://0.0.0.0:9099/emulator/v1/projects/demo-tests/config || exit 1
      interval: 60s
      retries: 5
      start_period: 20s
      timeout: 10s

Hope it helps. As you can see, I'm polling this REST endpoint to know wether the Auth service is up and running: https://firebase.google.com/docs/reference/rest/auth#section-auth-emulator-getconfig

hsuabina avatar Sep 27 '22 17:09 hsuabina