Lin Du
Lin Du

Reputation: 102257

How to deploy multiple functions using gcloud command line?

I want to deploy multiple cloud functions. Here is my index.js:

const { batchMultipleMessage } = require('./gcf-1');
const { batchMultipleMessage2 } = require('./gcf-2');

module.exports = {
  batchMultipleMessage,
  batchMultipleMessage2
};

How can I use gcloud beta functions deploy xxx to deploy these two functions at one time.

Upvotes: 4

Views: 3377

Answers (3)

Stanjhae
Stanjhae

Reputation: 284

Another way to deploy if you don't/can't use a script but prefer to keep it in the package.json.

The only downside is you need to repeat npm run deploy:function functionX for as many functions as you have.

  "scripts": {
    "deploy:function": "gcloud functions deploy $1 --trigger-http",
    "deploy:functions": "npm run deploy:function function1 && npm run deploy:function function2",
    "deploy": "npm run deploy:functions",
  },

Upvotes: 0

naranjja
naranjja

Reputation: 85

If anyone is looking for a better/cleaner/parallel solution, this is what I do:

# deploy.sh

# store deployment command into a string with character % where function name should be
deploy="gcloud functions deploy % --trigger-http"

# find all functions in index.js (looking at exports.<function_name>) using sed
# then pipe the function names to xargs
# then instruct that % should be replaced by each function name
# then open 20 processes where each one runs one deployment command
sed -n 's/exports\.\([a-zA-Z0-9\-_#]*\).*/\1/p' index.js | xargs -I % -P 20 sh -c "$deploy;"

You can also change the number of processes passed on the -P flag. I chose 20 arbitrarily.

This was super easy and saves a lot of time. Hopefully it will help someone!

Upvotes: 0

Lin Du
Lin Du

Reputation: 102257

Option 1:

For now, I write a deploy.sh to deploy these two cloud functions at one time.

TOPIC=batch-multiple-messages
FUNCTION_NAME_1=batchMultipleMessage
FUNCTION_NAME_2=batchMultipleMessage2

echo "start to deploy cloud functions\n"
gcloud beta functions deploy ${FUNCTION_NAME_1} --trigger-resource ${TOPIC} --trigger-event google.pubsub.topic.publish
gcloud beta functions deploy ${FUNCTION_NAME_2} --trigger-resource ${TOPIC} --trigger-event google.pubsub.topic.publish

It works, but if gcloud command line support deploy multiple cloud functions, that will be best way.

Option 2:

https://serverless.com/

Upvotes: 3

Related Questions