Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Google Cloud Source Repositories with service account

Is it possible to access a Google Cloud Source Repository in an automated way, i.e. from a GCE instance using a service account?

The only authentication method I am seeing in the docs is to use the gcloud auth login command, which will authenticate my personal user to access the repo, not the machine I am running commands from.

like image 339
Benjamin Smith Avatar asked Aug 18 '15 13:08

Benjamin Smith


3 Answers

On GCE vms running

gcloud source repos clone default ~/my_repo

should work automatically without extra step of authentication, as it will use VMs service account.

If you running on some other machine you can download from https://console.cloud.google.com service account .json key file and activate it with

gcloud auth activate-service-account --key-file KEY_FILE

and then run the above clone command.

like image 175
cherba Avatar answered Sep 19 '22 08:09

cherba


If you want to clone with git rather than running through gcloud, you can run:

git config --global credential.helper gcloud.sh

...and then this will work:

git clone https://source.developers.google.com/p/$PROJECT/r/$REPO
like image 42
Maximilian Avatar answered Sep 19 '22 08:09

Maximilian


In case somebody like me was trying to do this as part of Dockerfile, after struggling for a while I've only managed to get it to work like this:

RUN gcloud auth activate-service-account --key-file KEY_FILE ; \
    gcloud source repos clone default ~/my_repo

As you can see, having it to be part of the same RUN command was the key, otherwise it kept failing with

ERROR: (gcloud.source.repos.clone) You do not currently have an active account selected.
like image 31
Taras Avatar answered Sep 17 '22 08:09

Taras