You can store the Region values as environment configs using firebase functions:config:set:
firebase functions:config:set env.region="us-central1"
After running functions:config:set, you must redeploy functions to make the new configuration available.
Then get the environment variable inside your function by using this code:
exports.myFunction = functions
.region(functions.config.env.region)
.https.onRequest((req, res) => {
res.send("Hello");
});
With this, you no longer need to change region inside your code. But you still have to change configs and redeploy functions on every project if you want to change regions.
An additional solution is to use Cloud Build to automate everything. I haven't fully tested it yet, but here's what I can come up with.
First, follow the instructions on how to use the Firebase builder tool. You need this community provided image to run Firebase CLI commands on Cloud Build. Once finished, make sure that you have the following API's enabled on your projects:
- Cloud Resource Manager API
- Firebase Management API
and on your Cloud Build settings, Firebase Admin is enabled.
Then try this cloudbuild.yaml file. Cloud Build will use the tool from your project and to your other projects:
steps:
# Setup First Project
- name: gcr.io/project-id1/firebase
id: 'Use Project 1'
args: ['use', 'project-id1']
- name: gcr.io/project-id1/firebase
id: 'Set Firebase Environment Config'
args: ['functions:config:set', 'env.region=us-central1']
- name: gcr.io/project-id1/firebase
id: 'Deploy function1'
args: ['deploy', '--project=project-id1', '--only=functions']
# Setup Second Project
- name: gcr.io/project-id1/firebase
id: 'Use Project 2'
args: ['use', 'project-id2']
- name: gcr.io/project-id1/firebase
id: 'Set Firebase Environment Config'
args: ['functions:config:set', 'env.region=eu-west3']
- name: gcr.io/project-id1/firebase
id: 'Deploy function2'
args: ['deploy', '--project=project-id2', '--only=functions']
# And so on...
Note: Change project-id with your actual Project ID.