在我的代码中,我尝试从与Cloud Build相关的服务帐户收集应用程序默认凭据:
from google.auth import default
credentials, project_id = default()
这在我的本地空间中运行良好,因为我已经适当地设置了环境变量 GOOGLE_APPLICATION_CREDENTIALS
。但是,在Cloud Build内执行此行(通过构建配置中的测试步骤)时,会出现以下错误:
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials.
Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application.
For more information, please see https://cloud.google.com/docs/authentication/getting-started
这令我感到困惑,因为根据文档所述:
默认情况下,Cloud Build使用一个特殊的服务帐号代表您执行构建。这个服务帐号称为Cloud Build服务帐号,并且在启用Google Cloud项目中的Cloud Build API时会自动创建。 点击此处阅读 如果环境变量GOOGLE_APPLICATION_CREDENTIALS未设置,则ADC将使用附加到运行代码的资源的服务帐号。 点击此处阅读 那么为什么默认调用无法访问Cloud Build服务帐号凭据呢?