如何在不更改环境中默认 gcloud 帐户的情况下在云中进行服务帐户身份验证?

How can a service account authenticate in gcloud without changing the default gcloud account in the environment?

我在脚本中使用 gcloud auth activate-service-account ... 将服务帐户登录到 gcloud。脚本完成后,gcloud 仍然登录到该服务帐户。

我希望 gcloud 保持使用我的普通用户帐户登录,这样我就可以交互式地使用它。

我如何才能临时使用服务帐户进行身份验证 - 仅在单个脚本执行中?

在脚本末尾添加 Service Account Token Creator role to the "normal user account" and take advantage of the --impersonate-service-account = SERVICE_ACCOUNT_EMAIL flag for the gcloud commands you need to run within your script to avoid using the gcloud auth activate-service-account ... within your script at all or simply add the gcloud config set account ACCOUNT command 以确保在脚本完成后设置普通用户帐户。