Skip to content

Instantly share code, notes, and snippets.

@danahartweg
Last active September 30, 2022 04:10
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danahartweg/72600e0d30ae54290bf4deb197400ee9 to your computer and use it in GitHub Desktop.
Save danahartweg/72600e0d30ae54290bf4deb197400ee9 to your computer and use it in GitHub Desktop.
Running the firestore emulator in a CI environment without IPv6
#!/bin/sh
EMULATOR="cloud-firestore-emulator"
EMULATOR_TARGET=$(find ~/.cache/firebase/emulators/ -type f -name "$EMULATOR*.jar" | sort -r | head -n1)
if [ -z "$EMULATOR_TARGET" ]; then
echo "Could not find the firestore emulator. Ending test run."
exit 1
fi
# I've found that the java process is not always killed properly,
# causing issues on subsequent runs... so let's clean things up when we're done (or have errored)
killEmulatorPid()
{
EMULATOR_PID=$(pgrep -f "$EMULATOR")
if ! [ -z "$EMULATOR_PID" ]; then
kill -9 "$EMULATOR_PID"
fi
}
java -jar "$EMULATOR_TARGET" --host=127.0.0.1 --port=8080 > /dev/null 2> firestore-emulator.log &
RETRIES=0
RETRY_LIMIT=10
while [ $RETRIES -lt $RETRY_LIMIT ]; do
sleep 1
echo "Pinging firestore emulator"
if nc -z localhost 8080; then
break
fi
let RETRIES+=1
if [ $RETRIES -ge $RETRY_LIMIT ]; then
echo "Could not find the firestore emulator. Ending test run."
killEmulatorPid
exit 1
fi
done
yarn test
echo "End of test run. Cleaning up the firestore emulator."
killEmulatorPid
@danahartweg
Copy link
Author

The second revision of this script makes use of a custom Docker image that can be found here: https://hub.docker.com/r/dhartweg/node-java-yarn-firebase

That image includes:

  • JRE8
  • Node (which I believe installs without npm)
  • Yarn
  • netcat
  • Firebase tools

The most important change here is killing the java process when we're finished. With the previous iteration, subsequent CI runs would fail because the connection was already bound.

@jperasmus
Copy link

Hi @danahartweg,

Thanks for this script. From what I can tell, it assumes that you already have the cloud-firestore-emulator available in the ~/.cache/firebase/emulators/ directory - is that correct?

@pschneider
Copy link

Hi @danahartweg,

Thanks for this script. From what I can tell, it assumes that you already have the cloud-firestore-emulator available in the ~/.cache/firebase/emulators/ directory - is that correct?

Yes, this comes from already setting up the simulator on building the Docker image. E.g. via RUN firebase setup:emulators:firestore

@danahartweg
Copy link
Author

@pschneider your answer is spot on, thanks! @jperasmus thanks for your patience, I'm not entirely sure how I missed your comment from so long ago.

@jperasmus
Copy link

Thanks, got it working in the end. AFAIK, GitHub doesn't do notifications (or didn't at one point) for comments on Gists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment