以自动化方式验证 gcloud 服务帐户

Authenticate gcloud service account in automated fashion

总体objective:

我们希望能够使用我们的 Circle CI 作业中的 Docker 容器将工件上传到 Google 云中的存储桶。理想情况下,我们可以使用 gsutil cp <artifact> <bucket>

来完成此操作

问题:

我很难以自动方式验证 gcloud 服务帐户,而且我在任何地方都找不到这方面的文档。我读过的几乎每篇文章都遵循以下两种变体之一:

  1. 配置 gsutil

    $ gsutil config -a
    

    但是,这会提示一系列需要与 shell 互动的问题:

    Backing up existing config file "/Users/<user>/.boto" to "/Users/<user>/.boto.bak"...
    This command will create a boto config file at
    /Users/<user>/.boto containing your credentials, based on your
    responses to the following questions.
    What is your google access key ID?
    
  2. 使用gcloud登录:

    $ gcloud auth login
    Your browser has been opened to visit:
    
        https://accounts.google.com/o/oauth2/auth?....
    

由于这里的最终结果是 Docker 容器将验证服务帐户以执行 gsutil cp,交互式登录不是很有利...

我在 this example 中设置了凭据 JSON,但是设置环境变量 GOOGLE_APPLICATION_CREDENTIALS 仍然 提示 Are you sure? [Y/n]无法自动绕过的消息。有什么想法吗?

从服务帐户 Json 文件配置 Google Cloud SDK CLI 的命令:

gcloud auth activate-service-account test@development-123456.iam.gserviceaccount.com --key-file=service_account.json

将电子邮件地址 test@... 替换为您的服务帐户电子邮件地址。 将 Json 文件名 service_account.json 替换为您用于服务帐户凭据的文件名。

服务帐户电子邮件地址在服务帐户 json 文件中作为 client_email

如果您想了解更多信息,我写了很多关于 Google 凭据和 OAuth 的文章:

Google Cloud – Setting up Gcloud with Service Account Credentials