-1

I am trying to deploy a cloud function in GCP using CI-CD process. I have written a yaml file. Below is my code: cloudbuild.yaml

# Deploy Cloud Function
  - name: "gcr.io/cloud-builders/gcloud"
    id: deploy
    dir: "/workspace/API/audit_from_storage"
    args:
      [
        'gcloud', 'functions', 'deploy', 'audit_deployed',
        '--set-env-vars', 'BASEURL=${_CP_BASE_URL},BUCKETNAME=${_BUCKETNAME},FUNCTION=${_FUNCTION}',
        '--region=us-central1',
        '--source=.',
        '--trigger-resource=${_BUCKETNAME}',
        '--trigger-event=google.storage.object.finalize',
        '--runtime=nodejs10',
        '--entry-point=collectAudit',
        '--service-account=${_SERVICE_ACCOUNT}',
      ]

Below are the logs:

Finished Step #0 - "installing_npm"
Starting Step #1 - "deploy"
Step #1 - "deploy": Already have image (with digest): gcr.io/cloud-builders/gcloud
Step #1 - "deploy": ERROR: (gcloud) Invalid choice: 'gcloud'.
Step #1 - "deploy": Maybe you meant:
Step #1 - "deploy":   gcloud functions deploy
Step #1 - "deploy":   gcloud run deploy
Step #1 - "deploy":   gcloud deployment-manager deployments cancel-preview
Step #1 - "deploy":   gcloud deployment-manager deployments create
Step #1 - "deploy":   gcloud deployment-manager deployments delete
Step #1 - "deploy":   gcloud deployment-manager deployments describe
Step #1 - "deploy":   gcloud deployment-manager deployments list
Step #1 - "deploy":   gcloud deployment-manager deployments stop
Step #1 - "deploy":   gcloud deployment-manager deployments update
Step #1 - "deploy": 
Step #1 - "deploy": To search the help text of gcloud commands, run:
Step #1 - "deploy":   gcloud help -- SEARCH_TERMS
Finished Step #1 - "deploy"
ERROR
ERROR: build step 1 "gcr.io/cloud-builders/gcloud" failed: step exited with non-zero status: 2

Please help me resolve this. Below is the script after removing the gcloud argument from the array list:

Edit:

# Deploy Cloud Function
- name: "gcr.io/cloud-builders/gcloud"
  id: deploy
  dir: "/workspace/API/audit_from_storage"
  args:
    [
      'functions', 'deploy', 'audit_from_storage',
      '--set-env-vars', 'BASEURL=${_CP_BASE_URL},BUCKETNAME=${_BUCKETNAME},FUNCTION=${_FUNCTION}',
      '--region=us-central1',
      '--source=.',
      '--trigger-resource=${_BUCKETNAME}',
      '--trigger-event=google.storage.object.archive',
      '--runtime=nodejs10',
      '--entry-point=collectAudit',
      '--service-account=${_SERVICE_ACCOUNT}',
    ]

Below are the logs:

Starting Step #1 - "deploy"
Step #1 - "deploy": Already have image (with digest): gcr.io/cloud-builders/gcloud
Step #1 - "deploy": ERROR: (gcloud.functions.deploy) could not parse resource []
Finished Step #1 - "deploy"
ERROR
ERROR: build step 1 "gcr.io/cloud-builders/gcloud" failed: step exited with non-zero status: 1

enter image description here

Thanks Pritish

Pritish
  • 658
  • 3
  • 16
  • 38

1 Answers1

2

You don't need gcloud in the args array; it's the entrypoint for the container.

So, just:

args: ["functions","deploy",...]
DazWilkin
  • 32,823
  • 5
  • 47
  • 88
  • Hi, I tried your solution, but getting this error: `"deploy": ERROR: (gcloud.functions.deploy) could not parse resource []` – Pritish Aug 21 '20 at 19:37
  • Can you share more context? Where do you run this command? Where is your code? What your project structure? – guillaume blaquiere Aug 21 '20 at 21:38
  • Could you also post updated the yaml file that you're trying to deploy after the change that was suggested by DazWilkin? – Emmanuel Aug 21 '20 at 21:51
  • Hi, see my 'Edit' section. – Pritish Aug 23 '20 at 05:44
  • I have also added my project structure. My code is in `bitbucket` repository. I have mirrored this repository in my `google cloud source repository`. – Pritish Aug 23 '20 at 06:07
  • it seems that you're missing the `--zone` parameter, try to run the `gcloud` command outside of the cloud-build (in a console) and deploy the cloud function to verify if all the parameters that are being sent are enough to build it correctly – Emmanuel Aug 25 '20 at 21:51