GitHub Actions を使用して、既存の Google Kubernetes Engine クラスタに静的 Web サイトをデプロイするワークフローの例です。
このコードはあくまでも一例です。設定に合わせて値を変更したり更新したりする必要があるでしょう。
masterブランチのpushに対して、このworkflowは以下を行います。
-
提供された資格情報(credentials)を使用して、クラウドSDKをダウンロードして設定します。
-
コンテナイメージをビルド、タグ付けし、Google Container Registryにプッシュします。
-
Kubernetes デプロイメントを使用して、イメージをクラスタにプッシュします。
- GKE の配置では、ポッドを更新するために一意のタグが必要になることに注意してください。定数タグ
latestやブランチ名masterを使用すると、ワークフローが成功してもクラスタが更新されないことがあります。
- GKE の配置では、ポッドを更新するために一意のタグが必要になることに注意してください。定数タグ
-
Container Registry AOI と Kubernetes Engine APIを有効にします。
-
クラスタを選びます。
-
Create or reuse a GitHub repository for the example workflow:
-
Move into the repository directory:
$ cd <repo> -
Copy the example into the repository:
$ cp -r <path_to>/github-actions/example-workflows/gke/ .
-
Add the the following Cloud IAM roles to your service account:
-
Kubernetes Engine Developer- allows deploying to GKE -
Storage Admin- allows publishing to Container Registry注意: これらの許可は、クイックスタート用にに過度に広範なものとなっています。彼らは、Least Privledgeの原則の周りのベストプラクティスを表していません。適切にアクセスを制限するには、最も制限的なパーミッションを持つカスタムIAMロールを作成する必要があります。
-
-
Create a JSON service account key サービスアカウントキーを用意します
-
secretsを設定します
-
GKE_PROJECT: GCP project ID -
GKE_SA_KEY: サービスアカウントキーJSONファイルの中身
-
-
.github/workflows/gke.ymlの値を更新します-
GKE_CLUSTER- クラスタのインスタンス名 -
GCE_ZONE- クラスタのあるZONE -
IMAGE- お好みのDockerイメージ名
クラスタの名前は、コマンドを使用して見つけることができます。
$ gcloud container clusters list利用中のゾーンは以下のように確認します。
$ gcloud container clusters describe <CLUSTER_NAME> -
-
Add and commit your changes:
$ git add . $ git commit -m "Set up GitHub workflow" -
Push to the
masterbranch:$ git push -u origin master -
View the GitHub Actions Workflow by selecting the
Actionstab at the top of your repository on GitHub. Then click on theBuild and Deploy to GKEelement to see the details.