0

I'm creating a lib using the Google Earth Engine Python API. I need to test this script in travis CI. When I'm launching the unittest into my env it work but when I push to Github the test fail because the virtual env doesn't know my Google credential.

Is there a way to add my Earth Engine credential into my test environment?

1 Answer 1

2

Obviously here, the big question will be :
How to authenticate programmatically to the Earth engine API ?
Once you've done that, it's pretty much strait forward to use your standard test suit

Set up

to authenticate, you will need to have a Google Cloud Project and enable the Earth Engine API for that project.

I assume that you are already registered to Earth engine, if it's not the case follow this link : https://signup.earthengine.google.com/

So first create a cloud project : https://console.cloud.google.com/projectcreate

Then enable your Earth Engine account in your cloud project https://console.cloud.google.com/apis/library/earthengine.googleapis.com

You can manage the service accounts for your Cloud project by going to the Cloud Console menu and selecting IAM & Admin > Service accounts. (Choose the project if prompted.)

To create a new service account, click the + CREATE SERVICE ACCOUNT link.

Once you have a service account, click the menu for that account, then Create key > JSON. Download the JSON key file.

transform your key into a variable

Usually CI services use variables to deal with private key

Here you will transform your private_key.json file inta a encoded string that you'll upload to your CI.

import json
import base64

with open('ee_api_key.json') as jsonfile:
    data = json.load(jsonfile)
    datastr = json.dumps(data)
    encoded = base64.b64encode(datastr.encode())

print(encoded)

copy past the content of the encoded variable into your CI system (travis CI, CircleCI...)

Usage

in your file, start with the following code:

# only use the key when I'm in my test env
if 'EE_PRIVATE_KEY' in os.environ:
    # key need to be decoded in a file to work 
    content = base64.b64decode(os.environ['EE_PRIVATE_KEY']).decode()
    with open('ee_private_key.json', 'w') as f:
        f.write(content)
    
    # connection to the service account
    service_account = '[email protected]'
    credentials = ee.ServiceAccountCredentials(service_account, 'ee_private_key.json')
    ee.Initialize(credentials)

else:
    ee.Initialize()

Then you'll be ready to go

troubleshooting

(2020-11-27) the six lib is not installed properly by EarthEngine API so if you bump into the following error :

AttributeError: module 'six.moves' has no attribute 'collections_abc'

Then force the version in your requirements.txt as six>=1.15.0

notes

This answer is a sum-up of 2 documentations pages of Google API, feel free to have a look for more specific information :
https://developers.google.com/earth-engine/earthengine_cloud_project_setup#create-a-cloud-project
https://developers.google.com/earth-engine/guides/service_account

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.