How to test Firestore Security Rules with Jenkins?

Viewed 40

I'm developing some Firestore security rules locally. I use mocha to test the rules, and locally everything works. I've a Jenkins pipeline that every time I merge a PR on develop it published the rules on Firebase in cloud. What I want to do is running my unit tests within Jenkins. Anyway, every time Jenkins calls yarn test from the pipeline, I get an error that says

@firebase/firestore: Firestore (7.18.0): Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: FirebaseError: [code=internal]: 13 INTERNAL: Received RST_STREAM with code 2 triggered by internal client error: Protocol error This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.

Is there a way to run the firebase emulators from Jenkins?

Thanks!

1 Answers

I found a way to do that.

By using firebase-tools-docker I can easily run my tests inside a docker container that brings up the emulators suite.
The Jenkinsfile goes like this:

def jenkinsUser = 1001
def firebaseDocker = 'andreysenov/firebase-tools:9.14.0'

stage('Pull docker image') {
    sh "docker pull $firebaseDocker"
}

stage('Unit tests') {
    sh "docker run -d --rm \
        --user $jenkinsUser:$jenkinsUser \
        -p 8080:8080 \
        -v ${pwd()}:/home/node \
        --name firebase-emulators \
        $firebaseDocker \
        firebase emulators:start"

    sleep(5)

    sh "docker exec firebase-emulators /bin/bash -c 'cd tests && yarn test'"

    sh "docker stop firebase-emulators"
}

This is my folder structure (for reference):

Folder structure

Hope this helps

Related