How to cache firebase/emulators directory using GitHub Actions

Viewed 918

I am using the firebase emulator to run my tests and, I received a warning about a chance of optimization using the cache system.

How I can do this?

It appears you are running in a CI environment. You can avoid downloading the Firestore Emulator repeatedly by caching the /home/runner/.cache/firebase/emulators directory.

2 Answers

Define this in GitHub Actions:

    steps:
      - uses: actions/checkout@v2
      - name: Cache firebase emulators
        uses: actions/cache@v2
        with:
          path: ~/.cache/firebase/emulators
          key: ${{ runner.os }}-firebase-emulators-${{ hashFiles('~/.cache/firebase/emulators/**') }}
...

Then, for example, when we run this command firebase emulators:exec --project example 'npm test', if the emulator is not in the cache path, it will automatically start installing.

First run

  1. Cache is missing

enter image description here

  1. Download emulator when running test

enter image description here

  1. Save cache as post action of actions/cache

enter image description here

Second run

  1. Cache is found and restored and download doesn't happen.

enter image description here

If you need to update tool when npm package updates use something like this:

- name: Get Library Versions For Binary Caching
    id: cache-settings
    run: |
      echo "::set-output name=firebase-tools::$(yarn list -s --depth=0 --pattern firebase-tools | tail -n 1 | sed 's/.*@//g')"
      echo "::set-output name=firebase-tools::$(npm list -s --depth=0 | grep firebase-tools | tail -n 1 | sed 's/.*@//g')"

  - name: Cache Firebase Emulator Binaries
    uses: actions/cache@v2.1.2
    with:
      path: ~/.cache/firebase/emulators
      key: ${{ runner.os }}-firebase-${{ steps.cache-settings.outputs.firebase-tools }}
Related