Skip to content

Instantly share code, notes, and snippets.

@utamori
Last active August 21, 2020 04:35
Show Gist options
  • Select an option

  • Save utamori/3fa0832bdeae6cabb626a57c3643f81e to your computer and use it in GitHub Desktop.

Select an option

Save utamori/3fa0832bdeae6cabb626a57c3643f81e to your computer and use it in GitHub Desktop.

Google Kubernetes Engine - GitHub Actions

GitHub Actions を使用して、既存の Google Kubernetes Engine クラスタに静的 Web サイトをデプロイするワークフローの例です。

このコードはあくまでも一例です。設定に合わせて値を変更したり更新したりする必要があるでしょう。

Workflow 詳細

masterブランチのpushに対して、このworkflowは以下を行います。

  1. 提供された資格情報(credentials)を使用して、クラウドSDKをダウンロードして設定します。

  2. コンテナイメージをビルド、タグ付けし、Google Container Registryにプッシュします。

  3. Kubernetes デプロイメントを使用して、イメージをクラスタにプッシュします。

    • GKE の配置では、ポッドを更新するために一意のタグが必要になることに注意してください。定数タグ latest やブランチ名 master を使用すると、ワークフローが成功してもクラスタが更新されないことがあります。

Setup

  1. Container Registry AOI と Kubernetes Engine APIを有効にします。

  2. クラスタを選びます。

  3. Create or reuse a GitHub repository for the example workflow:

    1. Create a repository.

    2. Move into the repository directory:

      $ cd <repo>
      
    3. Copy the example into the repository:

      $ cp -r <path_to>/github-actions/example-workflows/gke/ .
      
  4. Create a Google Cloud service accountを用意します

  5. 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ロールを作成する必要があります。

  6. Create a JSON service account key サービスアカウントキーを用意します

  7. secretsを設定します

    • GKE_PROJECT: GCP project ID

    • GKE_SA_KEY: サービスアカウントキーJSONファイルの中身

  8. .github/workflows/gke.ymlの値を更新します

    • GKE_CLUSTER - クラスタのインスタンス名

    • GCE_ZONE - クラスタのあるZONE

    • IMAGE - お好みのDockerイメージ名

    クラスタの名前は、コマンドを使用して見つけることができます。

    $ gcloud container clusters list
    

    利用中のゾーンは以下のように確認します。

    $ gcloud container clusters describe <CLUSTER_NAME>
    

workflowを実行する

  1. Add and commit your changes:

    $ git add .
    $ git commit -m "Set up GitHub workflow"
    
  2. Push to the master branch:

    $ git push -u origin master
    
  3. View the GitHub Actions Workflow by selecting the Actions tab at the top of your repository on GitHub. Then click on the Build and Deploy to GKE element to see the details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment