1

As I am running my unit test through bitbucket pipelines, the below error occurs in some of the tests

Uncaught Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
      at GoogleAuth.getApplicationDefaultAsync (node_modules/google-auth-library/build/src/auth/googleauth.js:183:19)
      at processTicksAndRejections (internal/process/task_queues.js:93:5)
      at GoogleAuth.getClient (node_modules/google-auth-library/build/src/auth/googleauth.js:565:17)
      at GrpcClient._getCredentials (node_modules/google-gax/src/grpc.ts:202:20)
      at GrpcClient.createStub (node_modules/google-gax/src/grpc.ts:404:19)

This error occurs only for the test cases that are testing Cloud functions that use the Logging service which is imported from @google-cloud/logging

Please note that my project is initialized with a service key

const serviceAccount = environment.FirebaseCredentials as admin.ServiceAccount;
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: `https://${projectId}.firebaseio.com`,
    storageBucket: `${projectId}.appspot.com`,
});

When running the unit tests locally they usually work as well, however, they return an error when running through bitbucket pipelines

I would like to try this solution https://stackoverflow.com/a/42059661, but I think this could only work if you're doing it manually since you have to choose an email to login with.

I would like to know how to run this command or an alternative command in the bitbucket pipeline that may solve my problem.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
AliOz
  • 335
  • 2
  • 10

1 Answers1

1

From the documentation on deploy to Google Cloud

1.Create a Google service account key. For more guidance see Google's guide to creating service keys.

2.Once you have your key file, open up a terminal and browse to the location of your key file.

3.Encode your file in base64 format: base64 -w 0 and copy the output of the command.

Note: for some versions of MacOS the -w 0 is not necessary.

4.In your repo go to Repository settings, under Pipelines, select Repository variables and create a new variable named KEY_FILE and paste the encoded service account credentials.

5.Configure the pipe and add the variables: PROJECT and KEY_FILE

Activate the service account as follows

gcloud auth activate-service-account --key-file gcloud-api-key.json
Activated service account credentials for: [service-account-name@project-id.iam.gserviceaccount.com]
echo "$(gcloud auth list)"
To set the active account, run: $ gcloud config set account ACCOUNT
ACTIVE ACCOUNT
service-account-name@project-id.iam.gserviceaccount.com

Additionally you may have a look this stackoverflow link1 & link2

Sathi Aiswarya
  • 2,068
  • 2
  • 11