2

I'm using Firebase Cloud Functions for a project and I would like to automatically deploy them when I push in a specific branch on GitHub. Here is my current workflow :

name: Dev deploy

on:
  workflow_dispatch:
  push:
    branches:
      - develop

jobs:
  deploy_functions:
    name: Deploy Functions
    runs-on: ubuntu-latest
    steps:
      - name: Check out repo
        uses: actions/checkout@v3

      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: 16

      - name: Cache npm
        uses: actions/cache@v1
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-16-${{ hashFiles('**/package-lock.json') }}

      - name: Node install
        run: |
          cd functions
          npm install
          npm ci

      - name: Create SA key
        run: echo '${{ secrets.FIREBASE_CREDENTIAL_FILE_CONTENT_DEV }}' > gcloud.json

      - name: Create .firebaserc
        run: ./.github/actions-scripts/build-firebaserc.sh > .firebaserc
        env:
          PROJECT_ID: ${{ secrets.FIREBASE_PROJECT_ID_DEV }}

      - name: Debug
        run: |
          cat .firebaserc
          npx firebase-tools projects:list
        env:
          GOOGLE_APPLICATION_CREDENTIALS: gcloud.json

      - name: Deploy Cloud Functions
        run: npx firebase-tools deploy --only functions
        env:
          GOOGLE_APPLICATION_CREDENTIALS: gcloud.json

The Debug step gives me this:

Project Display Name Project ID Project Number Resource Location ID
My project name *** (current) *** europe-west

Therefore, my credentials have successfully access to my project. However, when I try to deploy in the next step I have the output:

 === Deploying to '***'...

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint

> lint
> eslint .

✔  functions: Finished running predeploy script.

Error: Failed to get Firebase project ***. Please make sure the project exists and your account has permission to access it.
Error: Process completed with exit code 2.

Any idea how to fix it?

  • I cannot do firebase login:reauth on a CI.
  • Running firebase use ${{ secrets.FIREBASE_PROJECT_ID_DEV }} raises the same error.

Thank you for your help.

Raida
  • 1,206
  • 5
  • 17
  • Try `firebase logout` and `firebase login` as per this [stackoverflow thread](https://stackoverflow.com/questions/63473787/error-failed-to-get-firebase-project-project-name-please-make-sure-the-project) – Rohit Kharche Mar 07 '23 at 12:40
  • It is not possible to run `firebase login` on CI. – Raida Mar 07 '23 at 13:09
  • 1
    Maybe try adding explicitly the firebase project id and firebase token as follows: ```- name: Use Firebase project run: npx firebase-tools use --add ${{ secrets.FIREBASE_PROJECT_ID_DEV }} --token ${{ secrets.FIREBASE_TOKEN }}``` while adding FIREBASE_TOKEN in your secret to your repository's secrets in the GitHub settings. If that didn't work then also try with adding `npx firebase-tools deploy --only functions --token ${{ secrets.FIREBASE_TOKEN }}` in Deploy Cloud Functions name – Rohit Kharche Mar 07 '23 at 13:56
  • 1
    I did not try this way for the moment since FIREBASE_TOKEN is deprecated according to the documentation, but maybe GOOGLE_APPLICATION_CREDENTIALS is not fully supported for the moment. I will try and I will tell you, thanks. – Raida Mar 07 '23 at 14:06
  • It works ! Looks like GOOGLE_APPLICATION_CREDENTIALS is not fully supported yet. – Raida Mar 07 '23 at 17:52
  • 1
    As it works for you I will post as an answer with your Note of `GOOGLE_APPLICATION_CREDENTIALS` so that other community members will also get answer for this question. – Rohit Kharche Mar 08 '23 at 04:59

1 Answers1

0

It looks like you have multiple firebase projects running and the CICD is not able to find the default project and its credentials. So Instead Adding the firebase project id and firebase token explicitly should solve your issue.

The updated config file will have the following extra step after adding :

- name: Use Firebase project         
   run: npx firebase-tools use --add ${{ secrets.FIREBASE_PROJECT_ID_DEV }} --token ${{ secrets.FIREBASE_TOKEN }}

while adding FIREBASE_TOKEN in your secret to your repository's secrets in the CI settings. And on Deploy Cloud Functions step update:

- name: Deploy Cloud Functions
   run: npx firebase-tools deploy --only functions --token ${{ secrets.FIREBASE_TOKEN }}
   env:
     GOOGLE_APPLICATION_CREDENTIALS: gcloud.json

Notice that I have added --token to authorize.

And In my opinion this will also may solve the issue:

  1. Run firebase login:ci, running this command will require authentication and Once you complete the authentication process, you will see a Firebase authentication token generated in your terminal. Copy this token to your clipboard.

  2. And add this token to your repository's Settings -> Secrets and create a new secret with the name FIREBASE_TOKEN and the value set to the authentication token you copied.

  3. And finally add the --token ${{ secrets.FIREBASE_TOKEN }} tag in step Deploy Cloud Functions.

  4. Then just commit and push your changes to your develop branch that will trigger the above GitHub Actions.

Reference Taken from: Use the CLI with CI systems, Encrypted secrets, GitHub Variables

Rohit Kharche
  • 2,541
  • 1
  • 2
  • 13