Hsn
Hsn

Reputation: 1238

Migrating google cloud functions from one project to another in GCP?

I have some cloud functions. I deploy these cloud function through source repository using cloudbuild.yaml and Cloud Triggers The cloudbuild.yaml is..

steps:

- name: 'python:3.7'
  entrypoint: 'bash'
  args: 
    - '-c'
    - |
        pip3 install -r requirements.txt
        pytest -rP

- name: 'gcr.io/cloud-builders/gcloud'
  args:
  - functions
  - deploy
  - Function_Name
  - --runtime=python37
  - --source=https://source.developers.google.com/projects/{}/repos/{}/moveable-aliases/master/paths/{}
  - --entry-point=main
  - --trigger-topic=TOPIC_NAME
  - --region=REGION

Now i would like to move this cloud function from this project to another project( Project A to Project B)

Now as i am not defining my project_id here. From where it is getting the project id?From service account?

How can i efficiently move this cloud function from Repository A to Repository B? as well as deploy it at Project B.

Upvotes: 5

Views: 2952

Answers (1)

guillaume blaquiere
guillaume blaquiere

Reputation: 75715

When you run your Cloud Build, some variable are set automatically, like the current project. This current projet is used by default in the environment for deploying the function.

For keeping the current behavior and adding the capability to extend it to the next projet you can do this

  • Define a substitution variable, for example _TARGET_PROJECT_ID
  • Assign by default the current project

...
- name: 'gcr.io/cloud-builders/gcloud'
  args:
  - functions
  - deploy
  - Function_Name
  - --runtime=python37
  - --source=https://source.developers.google.com/projects/{}/repos/{}/moveable-aliases/master/paths/{}
  - --entry-point=main
  - --trigger-topic=TOPIC_NAME
  - --region=REGION
  - --project=$_TARGET_PROJECT_ID
substitions:
  _TARGET_PROJECT_ID: $PROJECT_ID

Now, in your trigger (a new one?) or when you run your Cloud Build manually, specify the new project_ID if you want. If no, the current behavior (deployment in the current project) will continue.

Upvotes: 3

Related Questions