Search code examples
google-cloud-platformgoogle-cloud-functionsgoogle-deployment-manager

Google Cloud Function doesn't update on change when using Deployment Manager


When using Deployment Manager and Cloud Function, it seems that when a code change is made to the function, deployment manager doesn't seem to detect and update the function.

To reproduce:

  1. Create helloworld function.
  2. Deploy function with deployment manager.
  3. Make code change.
  4. Deploy again.
  5. Observe that the deployed function has not been updated by visiting the console and examining the source.

How do I invalidate the function so that it is correctly deployed?


Solution

  • This is related to the provided link, github.com/hashicorp/terraform-provider-google/issues/1938.

    It seems a hash of the zip is not performed so some kind of change is required to the deployment name or other properties.

    My solution was to get the current deployment version, and increment it and pass it as a property to the function.

    increment_function_version() {
      FUN=$1
      [[ -z "$FUN" ]] && echo "error: require function name" && exit 1
    
      if ! gcloud functions describe $FUN --region=europe-west2 2> /dev/null; then
        NEW_VERSION=1
      else
        VERSION=$(gcloud functions describe $FUN --region=europe-west2 | grep versionId | awk '{ print $2 }' | sed "s|\'||g")
        NEW_VERSION=$(expr $(($VERSION + 1)))
      fi
    }
    

    In order to do this with deployment manager, I had to transition from YAML to full python schemas (or Jinja), as properties cannot be passed with using the --config flag.

    gcloud deployment-manager --project $PROJECT_ID deployments update $NAME $TEMPLATE_FLAG --template mytemplate.py --properties version:$NEW_VERSION
    

    It's important that you provide a schema with the python template for your imports otherwise the deploy with fail.