8

The documentation for Google Cloud Platform describes a number of ways of transferring files to Google Compute Engine instances. But it does not mention how to use Git to do this. Also, while I have Google Cloud code repositories available under "Development" in the Web console, it's not clear to he how to associate these with Compute instances.

How do I associate a Git repository with a Google Compute Engine instance and git push from my local machine to that the instance repo?

orome
  • 45,163
  • 57
  • 202
  • 418
  • That's an interesting idea but I have never seen it done in practice. People typically use CM tools such as Ansible, Chef, and Puppet. For simple cases I like Fabric, which is easy to learn, allows you to program your code pushes and host management tasks in Python, and works over SSH. – jarmod May 14 '16 at 22:13

3 Answers3

16

Since an SSH service is running on GCE Linux VMs by default, simply follow these steps:

  1. On your local computer run gcloud auth login to authorize gcloud to access Google Cloud Platform.
  2. Run gcloud compute config-ssh to populate SSH config files with Host entries from each instance.
  3. Test SSHing to your VM instance by running ssh NAME.ZONE.PROJECT

    Example: ssh example-instance.us-central1-a.MY-PROJECT

  4. Set up an empty repository on your VM:

    $ mkdir project.git
    $ cd project.git
    $ git init --bare

  5. On your local computer add the remote repository:

git remote add origin NAME.ZONE.PROJECT:/<PATH>/project.git

Now you should be able to push your project to the Git repository on your VM.

orome
  • 45,163
  • 57
  • 202
  • 418
Kamran
  • 3,397
  • 26
  • 40
  • Can't I just create a repo on the instance and push with SSH? – orome May 28 '16 at 23:40
  • I updated my answer with steps required on GCE VM to create a Git repo server. – Kamran May 30 '16 at 00:11
  • That looks right, but I don't see the relevance of the first part now. It's just a matter of pushing the the repo over SSH. – orome May 30 '16 at 00:18
  • If you can elaborate further on your question and use case, I may be able to provide more information. – Kamran Jun 02 '16 at 01:42
  • Just what you've described. It's just that the first part of the answer has nothing to do with it. – orome Jun 02 '16 at 12:56
0

I usually use gcloud to clone the repo

gcloud source repos clone <<repo>> --projec=<<project_id>>

after you can use git from command line, don't know how works integration with tools, but from command line works really good (at least on MacOs and Linux)

Edit: Looking after you edit the question, I will use a cloud build job to do it, even if could be more complicate that it should. Just trigger the job that does the copy on push on a directory and after use one of the method for transfer the file.

sciack
  • 83
  • 6
-2

You have to define remote for this action. (using git remote add/set-url)


remote add

# Add new remote
git remote add godaddy1 $user1@foo.com:~/root.git 

remote set-url

# replace the current remote if want to replace instead of adding a new one
git remote set-url godaddy $user1@foo.com:~/root.git

List all remotes

# List all remotes
git remote -v
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • But how do I specify my Google Compute Instance, or associate my repo with the instance? – orome May 14 '16 at 17:54
  • You need to find out the url of your google git repo and set it up. it can be found under your google account – CodeWizard May 14 '16 at 17:56
  • Here is a link : https://cloud.google.com/source-repositories/docs/adding-repositories-as-remotes – CodeWizard May 14 '16 at 17:56
  • How do I associate the git Google git repo with a Google Compute Instance? Starting from "create instance", I don't see any repo associated. – orome May 14 '16 at 18:03
  • yes you can. follow the instructions on how to do it. – CodeWizard May 14 '16 at 18:04
  • Sorry, I just see a way to specify a project with ``. So I have repos on the Google cloud, but what's missing is: how do associate a repo with an instance? – orome May 14 '16 at 18:11
  • 1
    My question must not be clear. I've got all that. I've got a Google cloud repo. I've got a local repo that uses the Google cloud repo as a remote. That's just basic git. The question has nothing to do with that. The question is how to I associate the repo with a VM instance? (E.g., how to a control what's on my instance by pushing with Git?) – orome May 14 '16 at 18:36
  • So if you are just trying to put files on your instance, why do you need GIT ? Surely, just copy them with scp?? – jr593 Aug 20 '18 at 16:23