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を使ってbuildしたDockerfileをGoogle CloudのArtifact Registryへpushしよう!

Posted at

概要

開発環境検証時に最新のソースコードをCloud RunやGKEに反映させる際、commitなどをトリガーにArtifact Registryへpushできたら便利なので今回はGitHub Actionsを使ってbuildとArtifact Registryへのpushまで自動化させる方法について解説します
GitHub Actions内でArtifact Registryへpushする際はWorkload Indentityを使用します

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

Artifact Registryの作成

以下のようにArtifact Registryを作成することもできます

registry.tf
resource "google_artifact_registry_repository" "app_artifact_repository" {
  cleanup_policy_dry_run = false
  description            = null
  format                 = "DOCKER"
  location               = var.region
  mode                   = "STANDARD_REPOSITORY"
  project                = var.project
  repository_id          = "app"
}

ワークフローの作成

以下のようにワークフローを作成します
Google Cloud認証時は作成した

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

を指定します
その後、Artifact RegistryへpushできるようGoogle Cloud SDKに認証とArtifact Registryへの認証を行います
その後、docker buildとpushを行います

build.yaml
name: Build

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read
    steps:
      - name: Checkout code
        uses: actions/checkout@v5
      # https://github.com/google-github-actions/auth
      - name: Authenticate to Google Cloud with Workload Identity
        uses: google-github-actions/auth@v3
        with:
          workload_identity_provider: ${{ secrets.WORKLOAD_IDENTITY_PROVIDER }}
          service_account: ${{ secrets.GITHUB_ACTIONS_SA }}
          create_credentials_file: true
      # https://github.com/google-github-actions/setup-gcloud
      - name: Set up Google Cloud SDK
        uses: google-github-actions/setup-gcloud@v2
      - name: Configure Docker for Artifact Registry
        run: gcloud auth configure-docker us-central1-docker.pkg.dev
      - name: Docker Build
        run: |
          docker build -t us-central1-docker.pkg.dev/${{ secrets.GOOGLE_CLOUD_PROJECT }}/app/cloud-run-service-app:latest .
      - name: Push to Google Container Registry
        run: |
          docker push us-central1-docker.pkg.dev/${{ secrets.GOOGLE_CLOUD_PROJECT }}/app/cloud-run-service-app:latest

Artifact Registryの認証からpushまでの一連の流れは公式ドキュメント通りです

実際に実行してみよう!

以下のようにDockerfileがpushされたら成功です

Screenshot 2025-09-15 at 14.56.11.png

Screenshot 2025-09-15 at 14.56.56.png

参考

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?