Skip to content

Instantly share code, notes, and snippets.

@alexdmoss

alexdmoss/switch

Last active Feb 3, 2021
Embed
What would you like to do?
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