Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How can we deploy a gen2 cloud function through cloud build?

Hi Everyone. 
I am dealing with an issue with Cloud Build failing to deploy a gen2 cloud function. 
The build is triggered every time I push to a repository on GitHub. 

Here is a snippet that I have in my cloud build file (the part which fails):

 

 

  - name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
    args:
      - gcloud
      - functions
      - deploy
      - function_name
      - --gen2
      - --entry-point=main
      - --runtime=python39
      - --region=${_CF_REGION}
      - --source=./sample/dir
      - --trigger-event-filters="type=google.cloud.firestore.document.v1.written"
      - --trigger-event-filters="database=(default)"
      - --trigger-event-filters-path-pattern="document=Sample/{docId}"
      - --trigger-location=${_TRIGGER_REGION}
      - --trigger-service-account=${_TRIGGER_SERVICE_ACCOUNT}

 

 

However, with this, the build fails citing the following: 

ERROR: (gcloud.functions.deploy) ResponseError: status=[400], code=[Ok], message=[Trigger event type must be specified.]

However, the same CLI command works and deploys the gen2 cloud function when run in the terminal.

What am I doing wrong here?

Solved Solved
0 2 6,749
1 ACCEPTED SOLUTION

Hi @anjelisa !
Thank you for your reply.
I figured out a solution to the above problem.
Earlier, I could deploy the gen2 cloud functions using the same CLI command but it wasn't working in the cloud build file. The issue was with the way I was trying to execute it. The entrypoint needs to be specified. This happens only for gen2 cloud functions, and not for gen1 functions. 

Here is an updated step to be included in the cloud build file that works perfectly fine.

  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    entrypoint: 'bash'
    args:
      - "-c"
      - |
        gcloud functions deploy function_name \
        --gen2 \
        --trigger-event-filters='type=google.cloud.firestore.document.v1.written' \
        --trigger-event-filters='database=(default)' \
        --trigger-event-filters-path-pattern='document=Sample/{docId}' \
        --trigger-location=${_FIRESTORE_TRIGGER_REGION} \
        --trigger-service-account='${_TRIGGER_SERVICE_ACCOUNT}' \
        --runtime python39 \
        --entry-point main \
        --region=${_FIRESTORE_CF_REGION} \
        --source=./Source/Directory \
        --set-env-vars var1=${_TRIGGER_VAR_1},db_url=${_URL}

 

View solution in original post

2 REPLIES 2

Hi @kmursil,

Welcome to Google Cloud Community!

I don't see any error from your cloud build file but then I saw this documentation from Cloud Function triggers
The error shown comes from your trigger event or in other words, from the Eventarc trigger. Currently, Eventarc does not support direct events from Firestore and it is recommended to use Cloud Functions (1st gen) instead as it supports firestore triggers.

The solution from this discussion might help.

If you want this feature, you can submit a request through here
Hope this helps.

 

Hi @anjelisa !
Thank you for your reply.
I figured out a solution to the above problem.
Earlier, I could deploy the gen2 cloud functions using the same CLI command but it wasn't working in the cloud build file. The issue was with the way I was trying to execute it. The entrypoint needs to be specified. This happens only for gen2 cloud functions, and not for gen1 functions. 

Here is an updated step to be included in the cloud build file that works perfectly fine.

  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    entrypoint: 'bash'
    args:
      - "-c"
      - |
        gcloud functions deploy function_name \
        --gen2 \
        --trigger-event-filters='type=google.cloud.firestore.document.v1.written' \
        --trigger-event-filters='database=(default)' \
        --trigger-event-filters-path-pattern='document=Sample/{docId}' \
        --trigger-location=${_FIRESTORE_TRIGGER_REGION} \
        --trigger-service-account='${_TRIGGER_SERVICE_ACCOUNT}' \
        --runtime python39 \
        --entry-point main \
        --region=${_FIRESTORE_CF_REGION} \
        --source=./Source/Directory \
        --set-env-vars var1=${_TRIGGER_VAR_1},db_url=${_URL}