GitlabCI: Failed to authenticate, have you run firebase login?

Viewed 2181

I followed this tutorial to deploy my web project using firebase and Gitlab CI.

I use this conf to deploy on firebase:

# .gitlab-ci.yml

deploy:
  stage: deploy
  environment: production
  before_script:
    - npm i -g firebase-tools
  script:
    - firebase deploy --token "$FIREBASE_TOKEN" -P "$PROJECT_NAME" --debug
  only:
    refs:
      - master
    changes:
      - src/**/*

I stored the FIREBASE_TOKEN variable in my GitLab as protected and masked.

But when it's run in the pipeline it failed and return this error message:

$ firebase deploy --token "$FIREBASE_TOKEN" -P "$PROJECT_NAME" --debug
 [2020-06-27T21:26:52.339Z] > command requires scopes: ["email","openid","https://www.googleapis.com/auth/cloudplatformprojects.readonly","https://www.googleapis.com/auth/firebase","https://www.googleapis.com/auth/cloud-platform"]
 [2020-06-27T21:26:55.356Z] Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
     at GoogleAuth.getApplicationDefaultAsync (/usr/local/lib/node_modules/firebase-tools/node_modules/google-auth-library/build/src/auth/googleauth.js:160:19)
     at processTicksAndRejections (internal/process/task_queues.js:97:5)
     at runNextTicks (internal/process/task_queues.js:66:3)
     at listOnTimeout (internal/timers.js:518:9)
     at processTimers (internal/timers.js:492:7)
     at async GoogleAuth.getClient (/usr/local/lib/node_modules/firebase-tools/node_modules/google-auth-library/build/src/auth/googleauth.js:502:17)
     at async GoogleAuth.getAccessToken (/usr/local/lib/node_modules/firebase-tools/node_modules/google-auth-library/build/src/auth/googleauth.js:524:24)
 Error: Failed to authenticate, have you run firebase login?
 ERROR: Job failed: exit code 1

I don't know why I get:

Failed to authenticate, have you run firebase login?

because I use a token to authenticate using the --token flag.

Have someone an idea why I get this error?

2 Answers

Ok so it was a dummy error. My variable $FIREBASE_TOKEN was not taken into account because I checked the Protect variable (Export variable to pipelines running on protected branches and tags only.) option. But I was working on a feature branch to test it that was not protected.

I encountered the same error, but through GitHub Actions. The solution was to run the command firebase init hosting:github locally, as indicated by the documentation at https://github.com/marketplace/actions/deploy-to-firebase-hosting. After signing in, this will set a GitHub Secret for you in your repository.

The following was taken from the above-mentioned documentation:

"It's important to store this token as an encrypted secret to prevent unintended access to your Firebase project. Set it in the "Secrets" area of your repository settings and add it as FIREBASE_SERVICE_ACCOUNT: https://github.com/USERNAME/REPOSITORY/settings/secrets. "

Related