ノート: GitHubホストランナーは、現在GitHub Enterprise Serverでサポートされていません。 GitHubパブリックロードマップで、計画されている将来のサポートに関する詳しい情� �を見ることができます。
はじめに
This guide explains how to use GitHub Actions to build a containerized application, push it to Google Container Registry (GCR), and deploy it to Google Kubernetes Engine (GKE) when there is a push to the main branch.
GKEはGoogle CloudによるマネージドなKubernetesクラスタサービスで、コンテナ化されたワークロードをクラウドもしくはユーザ自身のデータセンターでホストできます。 詳しい情� �についてはGoogle Kubernetes Engineを参照してく� さい。
必要な環境
ワークフローの作成に進む前に、Kubernetesプロジェクトについて以下のステップを完了しておく必要があります。 このガイドは、プロジェクトのルートにDockerfileとKubernetes Deployment設定ファイルがすでに存在することを前提としています。 例としてはgoogle-github-actionsを参照してく� さい。
GKEクラスタの作成
GKEクラスタを作成するには、まずgcloud CLIで認証を受けなければなりません。 このステップに関する詳しい情� �については、以下の記事を参照してく� さい。
例:
$ gcloud container clusters create $GKE_CLUSTER \
--project=$GKE_PROJECT \
--zone=$GKE_ZONEAPIの有効化
Kubernetes Engine及びContainer Registry APIを有効化してく� さい。 例:
$ gcloud services enable \
containerregistry.googleapis.com \
container.googleapis.comサービスアカウントの設定と資� �情� �の保存
この手� �は、GKEインテグレーション用のサービスアカウントの作成方法を示します。 It explains how to create the account, add roles to it, retrieve its keys, and store them as a base64-encoded encrypted repository secret named GKE_SA_KEY.
-
新しいサービスアカウントを作成してく� さい。
$ gcloud iam service-accounts create $SA_NAME -
作成したサービスアカウントのメールアドレスを取得してく� さい。
$ gcloud iam service-accounts list -
サービスアカウントにロールを追� してく� さい。 ノート: 要件に合わせて、より制約の強いロールを適用してく� さい。
$ gcloud projects add-iam-policy-binding $GKE_PROJECT \ --member=serviceAccount:$SA_EMAIL \ --role=roles/container.admin $ gcloud projects add-iam-policy-binding $GKE_PROJECT \ --member=serviceAccount:$SA_EMAIL \ --role=roles/storage.admin $ gcloud projects add-iam-policy-binding $GKE_PROJECT \ --member=serviceAccount:$SA_EMAIL \ --role=roles/container.clusterViewer -
サービスアカウントのJSONキーファイルをダウンロードしてく� さい。
$ gcloud iam service-accounts keys create key.json --iam-account=$SA_EMAIL -
Store the service account key as a secret named
GKE_SA_KEY:$ export GKE_SA_KEY=$(cat key.json | base64)For more information about how to store a secret, see "Encrypted secrets."
Storing your project name
Store the name of your project as a secret named GKE_PROJECT. For more information about how to store a secret, see "Encrypted secrets."
(オプション)kustomizeの設定
Kustomizeは、YAML仕様を管理するために使われるオプションのツールです。 After creating a kustomization file, the workflow below can be used to dynamically set fields of the image and pipe in the result to kubectl. 詳しい情� �については、「kustomize の使い方」を参照してく� さい。
(Optional) Configure a deployment environment
Environments are used to describe a general deployment target like production, staging, or development. When a GitHub Actions workflow deploys to an environment, the environment is displayed on the main page of the repository. You can use environments to require approval for a job to proceed, restrict which branches can trigger a workflow, or limit access to secrets. For more information about creating environments, see "Using environments for deployment."
ワークフローの作成
必要な環境を整えたら、ワークフローの作成に進むことができます。
以下のワークフロー例は、コンテナイメージを作成して GCR にプッシュする方法を示しています。 次に、Kubernetes ツール(kubectl や kustomize など)を使用して、イメージをクラスタデプロイメントにプルします。
Under the env key, change the value of GKE_CLUSTER to the name of your cluster, GKE_ZONE to your cluster zone, DEPLOYMENT_NAME to the name of your deployment, and IMAGE to the name of your image.
If you configured a deployment environment, change the value of environment to be the name of your environment. If you did not configure an environment, delete the environment key.
# このワークフローはGitHubによって認定されていないアクションを使用します。
# それらはサードパーティによって提供され、
# 別個の利用規約、プライバシーポリシー、
# サポートドキュメンテーションが適用されます。
name: Build and Deploy to GKE
on:
push:
branches:
- main
env:
PROJECT_ID: ${{ secrets.GKE_PROJECT }}
GKE_CLUSTER: cluster-1 # Add your cluster name here.
GKE_ZONE: us-central1-c # Add your cluster zone here.
DEPLOYMENT_NAME: gke-test # Add your deployment name here.
IMAGE: static-site
jobs:
setup-build-publish-deploy:
name: Setup, Build, Publish, and Deploy
runs-on: ubuntu-latest
environment: production
steps:
- name: Checkout
uses: actions/checkout@v2
# Setup gcloud CLI
- uses: google-github-actions/setup-gcloud@94337306dda8180d967a56932ceb4ddcf01edae7
with:
service_account_key: ${{ secrets.GKE_SA_KEY }}
project_id: ${{ secrets.GKE_PROJECT }}
# Configure Docker to use the gcloud command-line tool as a credential
# helper for authentication
- run: |-
gcloud --quiet auth configure-docker
# Get the GKE credentials so we can deploy to the cluster
- uses: google-github-actions/get-gke-credentials@fb08709ba27618c31c09e014e1d8364b02e5042e
with:
cluster_name: ${{ env.GKE_CLUSTER }}
location: ${{ env.GKE_ZONE }}
credentials: ${{ secrets.GKE_SA_KEY }}
# Build the Docker image
- name: Build
run: |-
docker build \
--tag "gcr.io/$PROJECT_ID/$IMAGE:$GITHUB_SHA" \
--build-arg GITHUB_SHA="$GITHUB_SHA" \
--build-arg GITHUB_REF="$GITHUB_REF" \
.
# Docker イメージを Google Container Registry にプッシュする
- name: Publish
run: |-
docker push "gcr.io/$PROJECT_ID/$IMAGE:$GITHUB_SHA"
# kustomize を設定する
- name: Set up Kustomize
run: |-
curl -sfLo kustomize https://github.com/kubernetes-sigs/kustomize/releases/download/v3.1.0/kustomize_3.1.0_linux_amd64
chmod u+x ./kustomize
# Docker イメージを GKE クラスタにデプロイする
- name: Deploy
run: |-
./kustomize edit set image gcr.io/PROJECT_ID/IMAGE:TAG=gcr.io/$PROJECT_ID/$IMAGE:$GITHUB_SHA
./kustomize build . | kubectl apply -f -
kubectl rollout status deployment/$DEPLOYMENT_NAME
kubectl get services -o wide追� リソース
これらの例で使用されているツールの詳細については、次のドキュメントを参照してく� さい。
- For the full starter workflow, see the "Build and Deploy to GKE" workflow.
- その他のスターターワークフローと付随するコードについては、Google のGitHub Actionsワークフローの例を参照してく� さい。
- Kubernetes YAML のカスタマイズエンジンは、「Kustomize」を参照してく� さい。
- Google Kubernetes Engine のドキュメントにある「コンテナ化された Web アプリケーションのデプロイ」を参照してく� さい。