1

I have created a http-triggered Cloud Function via the GUI, which uses source code from a repository of mine. If the source code changes, I can fairly easily re-deploy(update) the Cloud Function manually via the UI by clicking on Edit -> Code -> Deploy.

I would like to set up a CI/CD pipeline, using Cloud Build, with a trigger on the repo, so that when the source code changes (on master branch), the Cloud Function is re-deployed. In order to do this, I need to figure out how to re-deploy the Cloud Function via the CLI. Reading the cloud functions deploy docs, it says "create or update a Google Cloud Function". However, how much I try, I don't manage to update my existing Cloud Function, only create new ones.

I've tried updating it like specified in this SO answer, by running;

gcloud functions deploy <MY-FUNCTION-NAME> --source=https://source.developers.google.com/projects/<MY-PROJECT>/repos/<MY-REPO>/paths/<FOLDER-PATH>              

which gives the error One of arguments [--trigger-topic, --trigger-bucket, --trigger-http, --trigger-event] is required: You must specify a trigger when deploying a new function. Notice the ... when deploying a new function.

Any ideas of how I can re-deploy (update) my existing one, and then (automatically), also use the latest source code?

4
  • For the sake of asking the obvious question, are you sure that you are in the correct project and that your function name is correct? You can use gcloud functions list to list all existing cloud functions in the current project. Commented Jul 11, 2022 at 12:32
  • No worries! When I run gcloud functions list, the cloud function which I'm interested in is listed there. Also, when I check my current project, and run gcloud config list --format 'value(core.project)' 2>/dev/null I am in the correct project. Commented Jul 11, 2022 at 13:07
  • The error is expected because you're missing how the function will be triggered. when deploying a new function does not matter, it's just a generic message. You may need to specify the trigger for the function. A lot of things can change from now to a response from 2020 Commented Jul 11, 2022 at 16:17
  • 1
    Even when I specify another 2 required arguments, --trigger-http and --runtime=python38, gcloud deploys a completely new Cloud Function, with the same name, in the same project. Not the update/redeploy I'm expecting unfortunately... Commented Jul 11, 2022 at 16:32

3 Answers 3

2

After a lot of testing-different-stuff, I finally figured it out. In order to re-deploy the same Cloud Function, I needed to specify all arguments that defined my Cloud Function. Only the required ones were not enough. To re-deploy;

gcloud functions deploy <MY-FUNCTION-NAME> --source=https://source.developers.google.com/projects/<MY-PROJECT>/repos/<MY-REPO>/moveable-aliases/<MY-BRANCH>/paths/<FOLDER-PATH> --trigger-http --runtime=<RUNTIME> --allow-unauthenticated --entry-point=<ENTRY-POINT> --region=<REGION> --memory=<MEMORY>

Sign up to request clarification or add additional context in comments.

1 Comment

I wonder if anyone's made a utility that essentially does this automatically
0

You need to pass all arguments you used in the first deploy again so it'll conclude that's the same function. Give a look here to more into creating CI pipelines to functions with cloud build.

Comments

0

I may be wrong but I believe to use gcloud functions deploy to update an existing Cloud Function, ensure same project, and function name and region are specified, and are the same as the existing function you want to update.

Here's a sample index.js

exports.helloWorld = (req, res) => {
res.send('Hello, World! deployOne');};

I deployed it;

gcloud functions deploy testFunctionTwo --runtime nodejs16 --trigger-http --entry-point helloWorld --region us-central1 --gen2

Then made some changes to both index.js and the command;

exports.helloWorld = (req, res) => {
res.send('Hello, World! deployTwo');};

gcloud functions deploy testFunctionTwo --trigger-http --entry-point helloWorld --region us-central1 --gen2 --memory 280MB

I set a memory parameter, changed the string inside the index.js file, and did not specify the runtime parameter.

It updated the function as expected.

Take a look here at the docs for the REST API for Cloud Functions; method projects.locations.functions.create

"It takes the form projects/{project}/locations/{location}"

What I'm getting at is it looks like a Cloud Function's uniqueness is project, location (region), and the name of the function.

If the Cloud Function being deployed satisfies all three to an existing function, it should update the existing function.

When I changed the region parameter, to europe-west1 for example, a new, separate function was deployed in that region.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.