google-cloud-platformgoogle-cloud-functionsgcloudgoogle-cloud-source-repos

Redeploy Google Cloud Function from command line using Source Repositories


I have a fairly simply Google Cloud Function that I'm deploying from Cloud Source Repositories.

I'm using the Google Cloud Shell as my development machine.

When I make updates to the function as I'm developing, I use the CLI to push updates to my Source Repository. However, running the gcloud functions deploy ... command from the command line doesn't seem to force GCF to pull in the latest source.

Occasionally, the deploy command after pushing new source code will simply state "Nothing to update." (which is incorrect.)

More often, it will go through the deployment process but the function will still run the previous version of the code.

When this happens the only way I can get the function to update is to use the dashboard, "Edit" the function, and then hit the Deploy button (even though I didn't change anything.)

Am I forgetting to do some kind of versioning or tagging that is required? Is there a way to force the CLI to pull the most current commit from the source repo?


Solution

  • I think you're looking for the --source=SOURCE gcloud functions deploy option to point to a source repository instead of the current directory (the default):

    --source=SOURCE

    Location of source code to deploy. Location of the source can be one of the following three options:

    • Source code in Google Cloud Storage (must be a .zip archive),
    • Reference to source repository or,
    • Local filesystem path (root directory of function source).

    Note that if you do not specify the --source flag:

    • Current directory will be used for new function deployments.
    • If the function is previously deployed using a local filesystem path, then function's source code will be updated using the current directory.
    • If the function is previously deployed using a Google Cloud Storage location or a source repository, then the function's source code will not be updated.

    The value of the flag will be interpreted as a Cloud Storage location, if it starts with gs://.

    The value will be interpreted as a reference to a source repository, if it starts with https://.

    Otherwise, it will be interpreted as the local filesystem path. When deploying source from the local filesystem, this command skips files specified in the .gcloudignore file (see gcloud topic gcloudignore for more information). If the .gcloudignore file doesn't exist, the command will try to create it.

    The minimal source repository URL is: https://source.developers.google.com/projects/${PROJECT}/repos/${REPO}

    By using the URL above, sources from the root directory of the repository on the revision tagged master will be used.

    If you want to deploy from a revision different from master, append one of the following three sources to the URL:

    • /revisions/${REVISION},
    • /moveable-aliases/${MOVEABLE_ALIAS},
    • /fixed-aliases/${FIXED_ALIAS}.

    If you'd like to deploy sources from a directory different from the root, you must specify a revision, a moveable alias, or a fixed alias, as above, and append /paths/${PATH_TO_SOURCES_DIRECTORY} to the URL.

    Overall, the URL should match the following regular expression:

      ^https://source\.developers\.google\.com/projects/
      (?<accountId>[^/]+)/repos/(?<repoName>[^/]+)
      (((/revisions/(?<commit>[^/]+))|(/moveable-aliases/(?<branch>[^/]+))|
      (/fixed-aliases/(?<tag>[^/]+)))(/paths/(?<path>.*))?)?$
    

    An example of a validly formatted source repository URL is:

      https://source.developers.google.com/projects/123456789/repos/testrepo/
      moveable-aliases/alternate-branch/paths/path-to=source