Skip to content

Instantly share code, notes, and snippets.

@nightspotlight
Created July 30, 2020 18:36
Show Gist options
  • Save nightspotlight/ca3f1ff8a6ff4d659ea85c3af37596f1 to your computer and use it in GitHub Desktop.
Save nightspotlight/ca3f1ff8a6ff4d659ea85c3af37596f1 to your computer and use it in GitHub Desktop.
Switch your Google Cloud service accounts on the command line
# put this in your ~/.profile, then reload your shell
function gcloud-activate() {
local action="$1"
local account=""
local credentials=""
case "${action}" in
alpha)
account="alpha-cicd@my-project-alpha.iam.gserviceaccount.com"
credentials="${HOME}/.config/gcloud/my-project-alpha.json"
;;
beta)
account="beta-cicd@my-project-beta.iam.gserviceaccount.com"
credentials="${HOME}/.config/gcloud/my-project-beta.json"
;;
reset)
account="default.user@gmail.com"
credentials=""
;;
*) { echo "Usage: ${FUNCNAME[0]} [alpha|beta|reset]"; return 1; };;
esac
# GOOGLE_APPLICATION_CREDENTIALS is consumed by Terraform
if [ -n "${credentials}" ] && [ -r "${credentials}" ]; then
export GOOGLE_APPLICATION_CREDENTIALS="${credentials}"
gcloud auth activate-service-account --key-file="${GOOGLE_APPLICATION_CREDENTIALS}"
else
unset GOOGLE_APPLICATION_CREDENTIALS
fi
gcloud config set account "${account}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment