gcloud CLI メモ

最近 gcp に入門して、gcloud CLIを使い始めたので、今後のためにもよく使いそうなコマンドをメモしていく。

アカウントの切り替え
gcloud config set account [ACCOUNT]
[ACCOUNT] はアカウントの email アドレスを入れる。
また、そのアカウントで再度ログインすることでも、activate されたアカウントが切り替わる。
gcloud auth login
アカウントの一覧表示
gcloud auth list

Service account を使用した認証
service account を利用したログインも可能。詳細はこちら↓

Set up Application Default Credentials
Obtains user access credentials via a web flow and puts them in the well-known location for Application Default Credentials (ADC).
GCP 関連の client-sdk を利用する際に必要となる Application Default Credentials を設定
ログインした Google Account の credential を $HOME/.config/gcloud/application_default_credentials.json
に書き出す。
gcloud auth application-default login

OAuth access token を生成
gcloud auth print-access-token

project の切り替え
gcloud config set
コマンドで project の property の値をセットしてあげればOK。
gcloud config set project your-project-name
gcloud config set
で変更されるのは、activate された configuration の property。複数の configuuration を管理している場合は、先に対象の configuration に切り替える (configuration を activate させる) 必要があるので注意。( gcloud init
で初期化した段階では、"default" という名前の configuration が作成され、この configuration が activate された状態 )
flag を使用した project の指定
gcloud CLI を実行する際に、--project
で project を指定することも可能。( config で指定した project より --project
で指定した project が優先される)

現在の project 名の値を取得
gcloud config get-value project

configuration の確認
configuration を確認する際は、gcloud config list
を使用する。
gcloud config list
gcloud config list
は activate された configuration の設定値を表示。

configuration を使用した project の切り替え
project の切り替えをする際に、逐一 gcloud config set
で一つ一つの property を変更するのは大変なので、"configuration" を作成し project 単位の決まった設定値を定めておくと便利。project の切り替えをする際は、その project の configuration に切り替えるだけで良くなる。
configuration の作成
以下コマンドで configuration が作成できる。
gcloud config configurations create YOUR_CONFIGURATION_NAME
configuration は空の状態で作成され(disable_usage_reporting = True
のみ設定されている)、作成された configuration が activate される。
こんな感じ↓
$ gcloud config configurations create my-config
Created [my-config].
Activated [my-config].
$ gcloud config list
[core]
disable_usage_reporting = True
Your active configuration is: [my-config]
property を設定
gcloud config set
コマンドを利用して property を set していく。
以下は project を設定する例。
$ gcloud config set project my-project
Updated property [core/project].
$ gcloud config list
[core]
disable_usage_reporting = True
project = my-project
Your active configuration is: [my-config]
configuration の確認
対象の configuration が active の場合は、gcloud config list
で確認できるが、
$ gcloud config list
[core]
disable_usage_reporting = True
project = my-project
Your active configuration is: [my-config]
activate されていない configuration を確認したい場合は、gcloud config configurations describe
を利用する。
gcloud config configurations describe YOUR_CONFIGURATION_NAME
activate されていない "my-config" を確認する例↓
$ gcloud config configurations describe my-config
is_active: false
name: my-config
properties:
core:
project: my-project
configuration の切り替え
gcloud config configurations activate
を使用することで、configuration を切り替えることができる。
gcloud config configurations activate YOUR_CONFIGURATION_NAME
環境変数 CLOUDSDK_ACTIVE_CONFIG_NAME
に configuration name を指定することでも切り替えることが可能。
export CLOUDSDK_ACTIVE_CONFIG_NAME=YOUR_CONFIGURATION_NAME
configuration の削除
configuration を削除したい場合は、gcloud config configurations delete
を使用すればOK
gcloud config configurations delete YOUR_CONFIGURATION_NAME
$ gcloud config configurations list
NAME IS_ACTIVE ACCOUNT PROJECT COMPUTE_DEFAULT_ZONE COMPUTE_DEFAULT_REGION
default True sample@example.com default-project
my-config False my-project
$ gcloud config configurations delete my-config
The following configurations will be deleted:
- my-config
Do you want to continue (Y/n)? y
Deleted [my-config].
$ gcloud config configurations list
NAME IS_ACTIVE ACCOUNT PROJECT COMPUTE_DEFAULT_ZONE COMPUTE_DEFAULT_REGION
botlogy-dev False
default True sample@example.com default-project
尚、activate 中の configuration は削除できないので注意。
configurations の一覧を表示
$ gcloud config configurations list

directory 単位で自動で configuration を切り替える
To change the active configuration for all commands in your current terminal, you can set the environment variable CLOUDSDK_ACTIVE_CONFIG_NAME to the name of the configuration you'd like to use.
gcloud CLI では環境変数を使用した ccnfiguration の切り替えも可能で、 具体的には、 CLOUDSDK_ACTIVE_CONFIG_NAME
env var に activate したい configuration 名を指定することで、その configuration に切り替えることができる。
これを direnv と組み合わせることで、directory 切り替え時に自動的に configuration を切り替えることが可能になる。
Install & Setup direnv
homebrew を使用して install し、zsh に setup する例↓
brew install direnv
~/.zshrc
に以下を追加
eval "$(direnv hook zsh)"
.envrc を設置
対象の directory に .envrc
を作成し、CLOUDSDK_ACTIVE_CONFIG_NAME
env var にその directory に移動した際に有効化させたい configuration の名前をセットする。
export CLOUDSDK_ACTIVE_CONFIG_NAME=my-configuration

Cloud Run の default region を設定
gcloud config set run/region asia-northeast1

Artifact Registry に repository を作成
gcloud artifacts repositories create quickstart-docker-repo --repository-format=docker \
--location=us-central1 --description="Docker repository"
list コマンドで確認
gcloud artifacts repositories list

Cloud Build で Container Image を build
Dockerfile が存在する directory で以下コマンドを叩く。
gcloud builds submit --tag IMAGE_URL

Container Image を Cloud Run へ deploy
gcloud を利用
gcloud run deploy SERVICE --image IMAGE_URL
YAML を使用
-
service.yaml
ファイルを作成する。
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: SERVICE
spec:
template:
spec:
containers:
- image: IMAGE
- 以下コマンドを叩く
gcloud run services replace service.yaml

user に紐づいた role の一覧を取得
$ gcloud projects get-iam-policy ${PROJECT_ID} --flatten="bindings[].members" --filter=bindings.members:${USER_EMAIL} --format="value(bindings.role)"
${PROJECT_ID}
: 対象の projectId
${USER_EMAIL}
: 対象の user の email

member に role を付与
Adds a policy binding to the IAM policy of a project, given a project ID and
the binding. One binding consists of a member, a role, and an optional
condition.
$ gcloud projects add-iam-policy-binding ${PROJECT_ID} --member=user:me@example.com --role=${ROLE}