Are you saying you're getting this in your unit test? You'll need to monkeypatch project_id as well, as GCP_PROJECT won't exist as an environment variable in your testing environment.
No, this is in the context of Google Cloud Build. I have a trigger for my master branch and once a new commit is pushed a build gets fired. I just did:
Hmm, not sure I totally understand your setup. Why is your build executing your function?
If you could include any more details like your Cloud Build configuration I might be able to help.
Otherwise, an alternative would be checking whether this variable is set or not and not continuing if it is:
project_id=os.environ.get("GCP_PROJECT")# If project_id exists, this is being executed on Cloud Functions
# Otherwise, it's being executed somewhere else and the following
# will fail.
ifproject_id:client=secretmanager.SecretManagerServiceClient()secret_name="my-secret"resource_name=f"projects/{project_id}/secrets/{secret_name}/versions/latest"response=client.access_secret_version(resource_name)secret_string=response.payload.data.decode('UTF-8')
So, I have a trigger defined on Cloud Build linked to my master branch. The .yaml file in my project looks like this: dev-to-uploads.s3.amazonaws.com/i/...
And, when executed, the build step creates a new container and I am not sure if inside of it:
I also tested without this step and it gets successfully deployed and the project id is available in a "production" situation. Maybe I will have to mock/stub the code for my tests.
importosfromgoogle.cloudimportsecretmanagerimportloggingclient=secretmanager.SecretManagerServiceClient()secret_name="my-secret"project_id=os.environ.get('GCP_PROJECT')resource_name="projects/{}/secrets/{}/versions/latest".format(project_id,secret_name)response=client.access_secret_version(resource_name)secret_string=response.payload.data.decode('UTF-8')defnew_measures_handler(data,context):"""Background Cloud Function to be triggered by Cloud Storage.
Args:
event (dict): The dictionary with data specific to this type of event.
context (google.cloud.functions.Context): The Cloud Functions
event metadata.
"""logging.info(secret_string)print('File: {}.'.format(data['name']))
and if I deploy it like this on GCP it works as expected. Google Cloud Build builds the function and deploys it. Project and respectively project secret can be accessed. But, when I uncomment my test step in .yaml and it gets executed on Google Cloud Build
Are you saying you're getting this in your unit test? You'll need to monkeypatch
project_id
as well, asGCP_PROJECT
won't exist as an environment variable in your testing environment.No, this is in the context of Google Cloud Build. I have a trigger for my master branch and once a new commit is pushed a build gets fired. I just did:
project_id = os.environ["GCP_PROJECT"]
print(project_id)
to see what is the result. And, it returns 'None' in the cloud execution environment.
Hmm, not sure I totally understand your setup. Why is your build executing your function?
If you could include any more details like your Cloud Build configuration I might be able to help.
Otherwise, an alternative would be checking whether this variable is set or not and not continuing if it is:
I am using Google's Cloud Build (cloud.google.com/cloud-build) as a CI/CD tool for my cloud function written in Python (cloud.google.com/functions/docs/ca...)
So, I have a trigger defined on Cloud Build linked to my master branch. The .yaml file in my project looks like this: dev-to-uploads.s3.amazonaws.com/i/...
And, when executed, the build step creates a new container and I am not sure if inside of it:
os.environ.get("GCP_PROJECT")
is relevant.
Which step is failing here, the test step or the deploy step?
Can you include the test that is testing the function in question?
The step which fails is the one which executes pytest: dev-to-uploads.s3.amazonaws.com/i/...
I also tested without this step and it gets successfully deployed and the project id is available in a "production" situation. Maybe I will have to mock/stub the code for my tests.
Yes, so this is happening in your tests? You'll need to monkeypatch
project_id
like I mentioned in my original reply.If you can include the test that's failing I can try to show you how to do that.
Also it's a lot easier for me to help if you share actual text and not screenshots!
My main.py looks like this:
and if I deploy it like this on GCP it works as expected. Google Cloud Build builds the function and deploys it. Project and respectively project secret can be accessed. But, when I uncomment my test step in .yaml and it gets executed on Google Cloud Build
I start getting the error. As you say, I need to mock it somehow. This is how my current test looks like:
OK, so your test should monkeypatch the environment like this:
You'll probably need to monkeypatch
secretmanager.SecretManagerServiceClient
as well.