0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GitHub ActionsとTerraformとWorkload Identityを使ってGoogle Cloudのリソースを自動デプロイしよう!

Last updated at Posted at 2025-09-15

概要

GitHub ActionsとTerraformとWorkload Identityを使ってコードベースでインフラ周りのデプロイを自動化できるので実装方法について解説します

前提

  • 必要なAPIを有効化済み

Workload Identityとは?

Google Cloud の外部で実行されているアプリケーションは、サービス アカウント キーを使用して Google Cloud リソースにアクセスできます。ただし、サービス アカウント キーは強力な認証情報であり、正しく管理しなければセキュリティ上のリスクとなります。Workload Identity 連携により、サービス アカウント キーに関連するメンテナンスとセキュリティの負担が軽減されます。
Workload Identity 連携を使用すると、Identity and Access Management(IAM)を使用して、外部 ID に IAM ロールを付与し、Google Cloud リソースへの直接アクセスを許可できます。サービス アカウントの権限借用によってアクセス権を付与することもできます。

公式ドキュメントに記載の通り、サービスアカウントキーではなく、IAMロールを使ったGoogle Cloudリソースへのアクセス権を付与する仕組みです
IAMロールを使ったアクセスになるため、サービスアカウントキーのメンテナンスやセキュリティに関する負担が軽減されます

GitHub ActionsからGoogle Cloudリソースを操作するためのサービスアカウントとIAMの作成

  • サービスアカウント
  • IAM

を以下のように作成します
今回は検証用のため、ロールはOwner権限にしています

Workload Identity Federationの作成

IDプールを作成します
今回は名前をIDを以下の通りにします

Screenshot 2025-09-15 at 13.48.31.png

下記の公式ドキュメントに従ってプロバイダの設定を行います

Screenshot 2025-09-15 at 13.49.51.png

オーディエンスはデフォルトのものを使用します
Screenshot 2025-09-15 at 13.50.10.png

今回は属性マッピングではassertion.repositoryを記載します
属性条件は以下の通りにします
組織名とリポジトリ名はご自身が使用するものに置き換えて設定しましょう

assertion.repository == "GitHub組織名/リポジトリ名"

Screenshot 2025-09-15 at 13.51.31.png

以下のように作成できれば成功です
その後、アクセスを許可を押します
Screenshot 2025-09-15 at 13.58.56.png

サービスアカウントの権限借用を使用して権限アクセス権を付与します
subjectには"GitHub組織名/リポジトリ名"を入力します
Screenshot 2025-09-15 at 14.03.16.png

実装

以下のようにmainブランチへのpushをトリガーにTerraformを使って自動デプロイを行うワークフローを作成します
Google Cloud認証時は作成した

  • workload_identity_provider
    • projects/{プロジェクト番号}/locations/global/workloadIdentityPools/github-actions-cicd/providers/github
  • service_account

を指定します

.github/workflows/deploy.yml
name: Deploy

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read
    steps:
      - name: Checkout code
        uses: actions/checkout@v5
      - name: Authenticate to Google Cloud with Workload Identity
        uses: google-github-actions/auth@v3
        with:
          workload_identity_provider: ${{ secrets.WORKLOAD_IDENTITY_PROVIER }}
          service_account: ${{ secrets.SERVICE_ACCOUNT }}
          create_credentials_file: true
      - name: Setup Terraform
        uses: hashicorp/setup-terraform@v3
        with:
          terraform_version: ">1.11.0"
      - name: Terraform Init
        run: terraform init
      - name: Terraform Validate
        run: terraform validate -no-color
      - name: Terraform Plan
        run: terraform plan -no-color
      - name: Terraform Apply
        run: terraform apply -no-color -auto-approve

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?