google cloud functions command to package without deploying

Viewed 919

I must be missing something because I cant find this option here: https://cloud.google.com/sdk/gcloud/reference/beta/functions/deploy

I want to package and upload my function to a bucket: --stage-bucket But not actually deploy the function

I'm going to deploy multiple functions (different handlers) from the same package with a Deployment Manager template: type: 'gcp-types/cloudfunctions-v1:projects.locations.functions'

gcloud beta functions deploy insists on packaging AND deploying the function. Where is the gcloud beta functions package command?

Here is an example of the DM template I plan to run:

resources:
  - name: resource-name
    type: 'gcp-types/cloudfunctions-v1:projects.locations.functions'
    properties:
      labels:
        testlabel1: testlabel1value
        testlabel2: testlabel2value
      parent: projects/my-project/locations/us-central1
      location: us-central1
      function: function-name
      sourceArchiveUrl: 'gs://my-bucket/some-zip-i-uploaded.zip'
      environmentVariables:
        test: '123'
      entryPoint: handler
      httpsTrigger: {}
      timeout: 60s
      availableMemoryMb: 256
      runtime: nodejs8

EDIT: I realized I have another question. When I upload a zip does that zip need to include dependencies? Do I have to do npm install or pip install first and include those packages in the zip or does cloud functions read my requirements.txt and packages.json and do that for me?

2 Answers

The SDK CLI does not provide a command to package your function.

This link will provide you with detail on how to zip your files together. There are just two points to follow:

  • File type should be a zip file.
  • File size should not exceed 100MB limit.

Then you need to call an API, which returns a Signed URL to upload the package.

Once uploaded you can specify the URL minus the extra parameters as the location.

There is no gcloud functions command to "package" your deployment, presumably because this amounts to just creating a zip file and putting it into the right place, then referencing that place.

Probably the easiest way to do this is to generate a zip file and copy it into a GCS bucket, then set the sourceArchiveUrl on the template to the correct location.

There are 2 other methods:

  • You can point to source code in source repository (this would use the sourceRepository part of the template).
  • You can get a direct url (using this API) to upload a ZIP file to using a PUT request, upload the code there, and then pass this same URL to the signedUploadUrl on the template. This is the method discussed in @John's answer. It does not require you to do any signing yourself, and likewise does not require you to create your own bucket to store the code in (the "Signed URL" refers to a private cloud functions location).

At least with the two zip file methods you do not need to include the (publicly available) dependencies -- the package.json (or requirements.txt) file will be processed by cloud functions to install them. I don't know about the SourceRepository method but I would expect it would work similarly. There's documentation about how cloud functions installs dependencies during deployment of a function for node and python.

Related