" gcloud auth activate-service-account"和" gcloud source repos clone"错误

时间:2018-01-23 00:32:07

标签: git docker google-cloud-platform service-accounts google-cloud-sdk

我想一起使用以下工具:

  • Docker
  • gcloud SDK
  • gcloud服务帐户
  • gcloud Source Repo(作为项目依赖项的一部分)

我采取的步骤:

1)登录Google云端控制台: https://console.cloud.google.com/iam-admin/serviceaccounts/project?project=project-name

2)创建一个服务帐户,现在给予所有者'权限,并将json密钥下载为account-name.json

enter image description here

3)添加Dockerfile:

FROM google/cloud-sdk
COPY account-name.json /tmp/account-name.json
RUN gcloud auth activate-service-account --key-file /tmp/account-name.json
RUN rm /tmp/account-name.json
RUN gcloud config set project project-name
RUN gcloud config set account account-name@project-name.iam.gserviceaccount.com
RUN gcloud source repos clone default --project=project-name

4)构建并运行docker:

docker build  -t service-account-repo .
docker run -ti service-account-repo

我得到的错误是:

  

错误:(gcloud.source.repos.clone)您目前没有活动状态   帐户已选中。请运行:

     

$ gcloud auth login

     

获取新凭据,或者您已使用   不同的帐户:

     

$ gcloud config set account ACCOUNT

     

选择要使用的已经过身份验证的帐户。

无论我尝试哪些命令,包括设置帐户和auth登录,通过docker exec它都不起作用。

如果我在本地运行相同的命令,它可以工作,因为我已经使用我的主gcloud帐户登录。

gcloud服务帐户是否适用于gcloud Source Repos?有人在没有正常帐户登录的情况下有一个工作示例吗?我是否需要启用其他云API才能使其正常运行?

1 个答案:

答案 0 :(得分:0)

The only gcloud command you need to run is

gcloud auth activate-service-account --key-file /tmp/account-name.json

It sets core/account property. After that

gcloud source repos clone default --project=project-name

should work (no need to set project since you specifying part of the command).

Make sure to have git installed and it must be on the path.

One thing to note that gcloud stores activated credentials in ~/.config/gcloud directory. This might not be what you want. You can change it by setting CLOUDSDK_CONFIG environment variable to point to another location, preferably not part of the docker image itself.