Something is wrong when reading JSON file in Jenkins pipeline

Hi, I’m trying to start Firebase emulator in Jenkins Pipeline but I’m getting an error.
This is the command I’m using to start the emulator:

firebase emulators:start --token \"$FIREBASE_TOKEN\" --project stormdevelopment-64ea9 --import ~/firestore/2023-01-15T10:30:05_94356

I have a dev.json file inside my folder that I use to authenticate my account in CLI and it sits at the root folder of Jenkins.
When I run my pipeline everything works fine until I get this error:

Failed to load function definition from source: FirebaseError: Failed to load function definition from source: Failed to generate manifest from function source: SyntaxError: /var/jenkins_home/workspace/storm-backend_jenkins_2/functions/lib/utils/dev.json: Expected property name or '}' in JSON at position 2

Have you ever seen something like this?
This is my pipeline in Jenkinsfile.

pipeline {
    agent any
    tools {
        nodejs '19.4.0'
    }
    environment {
        API_KEY_OPEN_DOTA = credentials('API_KEY_OPEN_DOTA')
        FORTNITE_API_KEY = credentials('FORTNITE_API_KEY')
        CLIENT_ID = credentials('CLIENT_ID')
        STORM_TOKEN = credentials('STORM_TOKEN')
        STRIPE_KEY = credentials('STRIPE_KEY')
        FIREBASE_LOGIN_TOKEN = credentials('FIREBASE_LOGIN_TOKEN')
        DEV_JSON_SERVICE = credentials('DEV_JSON_SERVICE')
        GOOGLE_APPLICATION_CREDENTIALS = "./dev.json"
        FIREBASE_TOKEN = credentials('FIREBASE_LOGIN_TOKEN')
    }
    
    stages {    
        stage('setup') {
            steps {
                sh 'echo API_KEY_OPEN_DOTA=\"$API_KEY_OPEN_DOTA\" >> .env'
                sh 'echo FORTNITE_API_KEY=\"$FORTNITE_API_KEY\"  >> .env'
                sh 'echo CLIENT_ID=\"$CLIENT_ID\" >> .env'
                sh 'echo STORM_TOKEN=\"$STORM_TOKEN\" >> .env'
                sh 'echo TESTING_ENDPOINT=\"TRUE\" >> .env'
                sh 'echo DEPLOYED=\"FALSE\" >> .env'
                sh 'echo GOOGLE_APPLICATION_CREDENTIALS=$GOOGLE_APPLICATION_CREDENTIALS >> .env'
                sh 'cp ./.env ./functions'
                sh 'cp ../../firebase_keys/dev.json ./functions/src/utils'
                sh 'echo $DEV_JSON_SERVICE >> dev.json'    
                sh 'ls'
            }            
        }
        stage('installing npm packages') {
            steps {
                sh 'npm install'
                sh 'npm --prefix ./functions install ./functions'
                sh 'npm install -g firebase-tools'
            }
        }
        stage('jenkins') {
                when {
                    branch 'jenkins_2'
                }
                steps {       
                    sh 'cd ./functions && firebase emulators:start --token \"$FIREBASE_TOKEN\" --project stormdevelopment-64ea9 --import ~/firestore/2023-01-15T10:30:05_94356'                        
                }                        
            }        
        }
    }

why do you think jenkins is reading that file? it feels like the firebase emulator is trying to do it, and that file isn’t valid json.

That’s so strange because when I run the same command outside of Jenkins that file is JSON valid. Maybe it has something to do with the way I’m creating the JSON file in the pipeline?

probably. I can’t imagine concatenating two json files becomes valid. You could use something like jq to try and merge them or something.

Hi, thank you for your answer. Is there anyway to insert the file manually inside the root folder of Jenkins and read it when the pipeline starts?

For example: I’ve found this answer in stackoverflow. node.js - JSON on the command line with jq in Jenkins - Stack Overflow
Is this a valid answer in your opinion?

Check Référence de l'interface de ligne de commande Firebase  |  Firebase Documentation, you should not need to put anything in a dev.json file for CI.

Hi, I’ve already tried that but the emulator continue to tells me that I don’t have the permission because I’m not logged. Even though as you can see from my pipeline code I insert the “token” flag when I use the command to start the tests.
I’ve followed that tutorial

Then I actually suspect the token is invalid/expired. I would try to create a fresh token and check:

  1. it can be used from local context
  2. update the jenkins credential and check if it can be used now (be careful not to append whitespace at the front/end!)

I’ll try what you suggest and then come back tomorrow. Thank you very much for your patience and time

I made it work.
This is my pipeline updated.

pipeline {
    agent any
    tools {
        nodejs '19.4.0'
    }
    environment {
        API_KEY_OPEN_DOTA = credentials('API_KEY_OPEN_DOTA')
        FORTNITE_API_KEY = credentials('FORTNITE_API_KEY')
        CLIENT_ID = credentials('CLIENT_ID')
        STORM_TOKEN = credentials('STORM_TOKEN')
        STRIPE_KEY = credentials('STRIPE_KEY')
        FIREBASE_LOGIN_TOKEN = credentials('FIREBASE_LOGIN_TOKEN')
        DEV_JSON_SERVICE = credentials('DEV_JSON_SERVICE')
        GOOGLE_APPLICATION_CREDENTIALS = "./dev.json"
        FIREBASE_TOKEN = credentials('FIREBASE_LOGIN_TOKEN')
    }
    
    stages {    
        stage('setup') {
            steps {
                sh 'echo API_KEY_OPEN_DOTA=\"$API_KEY_OPEN_DOTA\" >> .env'
                sh 'echo FORTNITE_API_KEY=\"$FORTNITE_API_KEY\"  >> .env'
                sh 'echo CLIENT_ID=\"$CLIENT_ID\" >> .env'
                sh 'echo STORM_TOKEN=\"$STORM_TOKEN\" >> .env'
                sh 'echo TESTING_ENDPOINT=\"TRUE\" >> .env'
                sh 'echo DEPLOYED=\"FALSE\" >> .env'
                sh 'echo GOOGLE_APPLICATION_CREDENTIALS=$GOOGLE_APPLICATION_CREDENTIALS >> .env'
                sh 'cp ./.env ./functions'
                sh 'cp ../../../jenkins_home/firebase_keys/dev.json ./functions'
                sh 'cd ./functions && npm run build'
            }            
        }
        stage('installing npm packages') {
            steps {
                sh 'npm install'
                sh 'npm --prefix ./functions install ./functions'
                sh 'npm install -g firebase-tools'
            }
        }
        stage('jenkins') {
                when {
                    branch 'jenkins_2'
                }
                steps {       
                    sh 'cd ./functions && firebase emulators:exec --project stormdevelopment-64ea9 --import ~/firestore/2023-01-15T10:30:05_94356 \"npm run testUnix ./tests/endPoints/auth.test.ts\"'
                    sh 'cd ./functions && firebase emulators:exec --project stormdevelopment-64ea9 --import ~/firestore/2023-01-15T10:30:05_94356 \"npm run testUnix ./tests/endPoints/leaderboard.test.ts\"'
                    sh 'cd ./functions && firebase emulators:exec --project stormdevelopment-64ea9 --import ~/firestore/2023-01-15T10:30:05_94356 \"npm run testUnix ./tests/endPoints/users.test.ts\"'
                }                        
            }        
        }
    }

A big thank you to @halkeye and @bpedersen2.
The problem was the .json file. Now I’m importing directly using cp command.