6

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.

Rodrigo
  • 135
  • 4
  • 45
  • 107

3 Answers3

15

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

banyan
  • 3,837
  • 2
  • 31
  • 25
  • Worked like a charm. Thank you! – Johnny Oshika Mar 14 '21 at 21:58
  • Worked but was missing key suffix – malibeg Dec 30 '21 at 13:54
  • 5
    I would propose to use ```key: ${{ runner.os }}-firebase-emulators-${{ github.sha }} restore-keys: | ${{ runner.os }}-firebase-emulators- ```. As github caches are immutable and `${{ hashFiles('~/.cache/firebase/emulators/**') }}` will return nothing on the first attempt, this will only cache the very first version of the emulators later on and not account for changes in the emulators used or version updates. – Jarno Jan 26 '22 at 16:15
3

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 }}
malibeg
  • 2,237
  • 2
  • 17
  • 21
  • 1
    set-output is already deprecated: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/ – Jesús Franco Oct 21 '22 at 21:36
0

According to documentation:
https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows

inside your .github/workflows/filename.yml include cache instructions:

on: [push, pull_request]
jobs:
  emulator_test:
    name: Run all tests using Firebase Emulator
    runs-on: ubuntu-latest
    steps:
...
# other instructions
...
      - name: Cache node modules
        id: cache-npm
        uses: actions/cache@v3
        env:
          cache-name: cache-node-modules
        with:
          # npm cache files are stored in `~/.npm` on Linux/macOS
          path: ~/.npm
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-build-${{ env.cache-name }}-
            ${{ runner.os }}-build-
            ${{ runner.os }}-

      - if: ${{ steps.cache-npm.outputs.cache-hit != 'true' }}
        name: List the state of node modules
        continue-on-error: true
        run: npm list

      - name: Cache firebase emulators
        uses: actions/cache@v2
        with:
          path: ~/.cache/firebase/emulators
          key: ${{ runner.os }}-firebase-${{ steps.cache-npm.outputs.firebase-tools }}
...
# other instructions
...

Without cache instructions:
enter image description here

Using cache instructions.
enter image description here

danilo
  • 7,680
  • 7
  • 43
  • 46