Last active
February 3, 2021 08:14
-
-
Save alexdmoss/8ac9eea1f55063e2c99f8b7bbe9564cd to your computer and use it in GitHub Desktop.
Bash to switch between GCP projects/accounts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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