いろいろ作ってると、GCPのプロジェクトが増えてきて、切り替えとかが面倒になる。
gcloud init
を打つと、設定ファイル(configuration)が出てきて、切り替えと再認証(Switch to and re-initialize existing configuration)は出来るけど、切り替えだけの方法がわからなくて嵌ったのでメモ。
すでに使っている環境でgcloudを打つと下記のように出てくる。
% gcloud init Welcome! This command will take you through the configuration of gcloud. Settings from your current configuration [gae] are: [compute] region = asia-northeast1 zone = asia-northeast1-a [core] account = yu_yamazaki@example.com disable_usage_reporting = False project = oceanus-example Your active configuration is: [gae] Pick configuration to use: [1] Re-initialize this configuration [gae] with new settings [2] Create a new configuration [3] Switch to and re-initialize existing configuration: [default] [4] Switch to and re-initialize existing configuration: [sample]
新しく設定ファイルを作りたいときは[2] Create a new configurationを選択して、言われたとおりにいろいろやる。
今回はすでにある設定sampleに切り替えたいので、下記を打つと既存のものにできる。
% gcloud config configurations activate sample
コマンドが長過ぎる。
google compute engine - How to change the active configuration profile in gcloud? - Stack Overflow
面倒なのでシェルのエイリアスに追加する。私の場合はzshなので
% vim ~/.zshrc #追加 alias gchange='gcloud config configurations activate' #読み込み % source ~/.zshrc
で、
gchange 設定名
で切り替えできるようになった。
最初gswitchにしたけど、長いしつづりが直感的じゃないので、gchangeにした。
現在どんな設定があるかとか、削除の方法とかは下記で出てくる
% gcloud config configurations
ERROR: (gcloud.config.configurations) too few arguments
Usage: gcloud config configurations [optional flags]
command may be activate | create | delete | describe | listFor detailed information on this command and its flags, run:
gcloud config configurations --help