Harry
Harry

Reputation: 11

Unable to deploy firebase functions with Github actions, it doesn't recognize a firebase project using Google service account for authentification

I'm following this Medium article to deploy firebase functions using Github actions.

This is the workflow in .github/workflows/firebase-deploy.yml

name: Deploy Cloud Functions
on:
  workflow_dispatch:
  push:
    branches:
      - master
      - pre-production
      - github-actions
      - "feature/**"
    paths:
      - "functions/**"
jobs:
  build_and_deploy:
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 16.x
      - uses: actions/cache@v2
        with:
          path: ~/.npm
          key: macOS-node-${{ hashFiles('**/functions/package-lock.json') }}
          restore-keys: |
            macOS-node-
      - name: Build Cloud Functions
        run: npm ci
        working-directory: functions
      - name: Create SA key
        run: echo '${{ secrets.FIREBASE_DEV_SERVICE_ACCOUNT }}' > gcloud.json
        working-directory: functions
      - name: Print json
        run: echo "$(cat gcloud.json)"
        working-directory: functions
      - name: Install firebase tools
        run: npm install -g firebase-tools
        working-directory: functions
      - name: Deploy Cloud Functions
        run: export GOOGLE_APPLICATION_CREDENTIALS=gcloud.json && npx firebase-tools deploy --only functions --json
        working-directory: functions

In the step Deploy Cloud Functions of the github action, I get the following error:

Run export GOOGLE_APPLICATION_CREDENTIALS=gcloud.json && npx firebase-tools deploy --only functions --json
  export GOOGLE_APPLICATION_CREDENTIALS=gcloud.json && npx firebase-tools deploy --only functions --json
  shell: /bin/bash -e ***0***
  
***
  "status": "error",
  "error": "No project active, but project aliases are available.\n\nRun \u001b[1mfirebase use <alias>\u001b[22m with one of these options:\n\n  dev (web3army-pre-prod)\n  prod (blockchainarmy-6ed76)"
***
Error: Process completed with exit code 1.

When deploying normally on the terminal without github actions, I use firebase use dev. Here the project dev is specified inside the json that I created with the google service account and also used to create the secret in the repo as explained in the tutorial. It seems like that GOOGLE_APPLICATION_CREDENTIALS is not initialized correctly with gcloud.json.

I tried to print the content of the json to check if it was okay but the result is encrypted with ***, so it's difficult to understand what is going wrong. Thanks a lot.

Upvotes: 1

Views: 744

Answers (1)

Dante Arrighi
Dante Arrighi

Reputation: 11

You can try add one more step like this:

- name: Select Firebase Project
        run: |
          export GOOGLE_APPLICATION_CREDENTIALS=gcloud.json 
          npx firebase-tools use your-project-id

Or

- name: Select Firebase project
        run: export GOOGLE_APPLICATION_CREDENTIALS=gcloud.json && npx firebase-tools use your-project-id
      

The thing is, I know the project information should be there in the json file you've generated (if this is your case). For some reason is not taking the project id in consideration.

However, I had a similar issue and was able to solved it this way.

Now, I have one more step to execute the firebase use projectId command. If it works, you'll see the message Now using project *projectId*

Take into account that you can leave this one as the last step and put together the selection of the project and the command to deploy like this:

- name: Select Firebase Project
        run: |
          export GOOGLE_APPLICATION_CREDENTIALS=gcloud.json 
          npx firebase-tools use your-project-id
          npx firebase-tools deploy --only functions

This last example works faster ;)

Just a comment:

- name: Build Cloud Functions
        run: npm ci

run: npm ci didn't work for me because the output folder with .js files is just not created. I've used npm run build instead.

Unfortunately, I have a different issue and I'm still in pain with the CI configuration for Firebase functions. I hope this works for you and really wish you luck with this!

Upvotes: 0

Related Questions