Skip to content

Instantly share code, notes, and snippets.

@alexdmoss
Last active February 3, 2021 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexdmoss/8ac9eea1f55063e2c99f8b7bbe9564cd to your computer and use it in GitHub Desktop.
Save alexdmoss/8ac9eea1f55063e2c99f8b7bbe9564cd to your computer and use it in GitHub Desktop.
Bash to switch between GCP projects/accounts
#!/usr/bin/env bash
set -euoE pipefail
if [[ -z "${1:-}" ]]; then
echo "You did not specify a config or project"
exit 1
fi
# special pre-defined project/account combinations I use a lot to save typing
special="N"
case "${1}" in
"dev")
project="<<work-k8s-sandbox-project>>";
account="<<work-email-address>>";
special="Y";;
"prod")
project="<<work-k8s-production-project>>";
account="<<work-email-address>>";
special="Y";;
"sites")
project="<<my-personal-gcp-project>>";
account="<<my-personal-email>>";
special="Y";;
esac
if [[ ${special} == "N" ]]; then
project="${1}"
if [[ -z "${2:-}" ]]; then
echo "You must specify an account when not using one of the pre-defined configs"
exit 2
else
account="${2}"
fi
fi
# account first to avoid warning
gcloud config set account "${account}"
gcloud config set project "${project}"
# [optional] to confirm it happened
gcloud config list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment