Reputation: 1023
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?
Upvotes: 1
Views: 2993
Reputation: 110
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.
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 1023
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>
Upvotes: 2