GitHub ActionsでCloud Runに自動デプロイする最小構成
概要
GitHub ActionsからGoogle Cloud RunにWebアプリケーションを自動デプロイする、実際に動作する最小構成をまとめました。
複雑な設定は排除し、確実に動作する必要最小限の構成に特化しています。
システム構成フロー
Local EnvironmentのDeveloperがGitHub Platformの**Repository (main branch)**にコードをプッシュすると、GitHub Actions CI/CD Runnerが自動実行されます。
GitHub Actions CI/CD Runnerは、Google Cloud PlatformのArtifact Registry Docker Imagesにイメージを保存し、Cloud Run Web Serviceにデプロイします。
Cloud Run Web Serviceは必要に応じてArtifact Registry Docker ImagesからPull Imageします。
各コンポーネントの役割
コンポーネント | 役割 | 実行内容 |
---|---|---|
🖥️ Local Environment - Developer | 開発環境 | コード作成・ローカルテスト |
📚 GitHub Platform - Repository (main branch) | ソース管理 | コード保存・バージョン管理 |
⚙️ GitHub Actions CI/CD Runner | CI/CD実行 | Docker Build・Push・Deploy の全自動化 |
📦 Google Cloud Platform - Artifact Registry Docker Images | イメージ保存 | Dockerイメージの安全な保管・配信 |
🚀 Google Cloud Platform - Cloud Run Web Service | 本番サービス | Webアプリケーションの実行・公開 |
GitHub Actions CI/CD Runnerの具体的な処理内容
- Push Code: Local EnvironmentのDeveloperがGitHub Platformの**Repository (main branch)**にコードをプッシュ
- Trigger: Repository (main branch)更新でGitHub Actions CI/CD Runnerワークフローが自動実行
- Build & Push: GitHub Actions CI/CD RunnerがDockerイメージをビルドしてGoogle Cloud PlatformのArtifact Registry Docker Imagesにプッシュ
- Deploy: GitHub Actions CI/CD RunnerがGoogle Cloud PlatformのCloud Run Web Serviceを新しいイメージでデプロイ
- Pull Image: Cloud Run Web ServiceがArtifact Registry Docker Imagesから必要なイメージを取得
重要ポイント:
- GitHub Actions CI/CD RunnerがDockerビルド、イメージプッシュ、デプロイの全工程を実行
- Cloud Build不使用:GitHub Actions CI/CD Runner内でdockerコマンドとgcloudコマンドを直接実行
-
単一ワークフロー:
.github/workflows/deploy.yml
ですべての処理を自動化
前提条件
- GCPプロジェクトが作成済み
- GitHubリポジトリが作成済み
- Dockerfileが準備済み
1. GCP側の準備
1.1 必要なAPIの有効化
# Cloud Run API
gcloud services enable run.googleapis.com
# Artifact Registry API(コンテナレジストリ)
gcloud services enable artifactregistry.googleapis.com
# Cloud Build API
gcloud services enable cloudbuild.googleapis.com
理由: GitHub ActionsからGCPサービスにアクセスするため
1.2 Artifact Registry Docker Images リポジトリ作成
# Dockerイメージ保存用のリポジトリを作成
gcloud artifacts repositories create docker-repo \
--repository-format=docker \
--location=asia-northeast1 \
--description="Docker images for Cloud Run"
理由: Artifact Registry Docker ImagesでDockerイメージを管理するため
1.3 サービスアカウント作成・権限設定
# GitHub Actions CI/CD Runner用サービスアカウント作成
gcloud iam service-accounts create github-actions-deploy \
--display-name="GitHub Actions Deploy" \
--description="Service account for GitHub Actions CI/CD"
# 必要な権限を付与
# Cloud Run管理権限
gcloud projects add-iam-policy-binding PROJECT_ID \
--member="serviceAccount:github-actions-deploy@PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/run.admin"
# Artifact Registry書き込み権限
gcloud projects add-iam-policy-binding PROJECT_ID \
--member="serviceAccount:github-actions-deploy@PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/artifactregistry.writer"
# サービスアカウント使用権限
gcloud projects add-iam-policy-binding PROJECT_ID \
--member="serviceAccount:github-actions-deploy@PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/iam.serviceAccountUser"
理由: GitHub Actions CI/CD RunnerがCloud Run Web ServiceデプロイとArtifact Registry Docker Images管理を行うため
1.4 サービスアカウントキー生成
# JSONキーファイル生成
gcloud iam service-accounts keys create github-actions-key.json \
--iam-account=github-actions-deploy@PROJECT_ID.iam.gserviceaccount.com
理由: GitHub Actions CI/CD RunnerでGCP認証を行うため
2. GitHub側の設定
2.1 Secrets設定
GitHub PlatformのRepository (main branch)の Settings > Secrets and variables > Actions で以下を設定:
-
GCP_SA_KEY
: 上記で生成したJSONキーファイルの内容 -
GCP_PROJECT_ID
: GCPプロジェクトID
理由: 機密情報を安全にGitHub Actions CI/CD Runnerで使用するため
3. GitHub Actions CI/CD Runnerワークフロー設定
.github/workflows/deploy.yml
を作成:
name: Deploy to Cloud Run
# mainブランチプッシュ時に自動実行
on:
push:
branches: [main]
workflow_dispatch: # 手動実行も可能
# 環境変数
env:
PROJECT_ID: your-project-id
REGION: asia-northeast1
SERVICE_NAME: my-app
REGISTRY: asia-northeast1-docker.pkg.dev
REPOSITORY: docker-repo
jobs:
deploy:
runs-on: ubuntu-latest
steps:
# ソースコード取得
- name: Checkout code
uses: actions/checkout@v4
# GCP認証
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v1
with:
credentials_json: ${{ secrets.GCP_SA_KEY }}
# Google Cloud CLI設定
- name: Set up Google Cloud CLI
uses: google-github-actions/setup-gcloud@v1
with:
project_id: ${{ secrets.GCP_PROJECT_ID }}
# Docker認証
- name: Configure Docker for Artifact Registry
run: gcloud auth configure-docker asia-northeast1-docker.pkg.dev
# Dockerイメージビルド
- name: Build Docker image
run: |
docker build -t $REGISTRY/$PROJECT_ID/$REPOSITORY/$SERVICE_NAME:$GITHUB_SHA .
docker tag $REGISTRY/$PROJECT_ID/$REPOSITORY/$SERVICE_NAME:$GITHUB_SHA $REGISTRY/$PROJECT_ID/$REPOSITORY/$SERVICE_NAME:latest
# イメージプッシュ
- name: Push Docker image
run: |
docker push $REGISTRY/$PROJECT_ID/$REPOSITORY/$SERVICE_NAME:$GITHUB_SHA
docker push $REGISTRY/$PROJECT_ID/$REPOSITORY/$SERVICE_NAME:latest
# Cloud Runデプロイ
- name: Deploy to Cloud Run
run: |
gcloud run deploy $SERVICE_NAME \
--image $REGISTRY/$PROJECT_ID/$REPOSITORY/$SERVICE_NAME:latest \
--region $REGION \
--platform managed \
--allow-unauthenticated \
--port 8080 \
--memory 512Mi \
--cpu 1 \
--min-instances 0 \
--max-instances 10
# デプロイURL表示
- name: Show service URL
run: gcloud run services describe $SERVICE_NAME --region $REGION --format 'value(status.url)'
4. 動作確認
4.1 Repository (main branch)にプッシュ
git add .
git commit -m "Setup GitHub Actions"
git push origin main
4.2 GitHub Actions CI/CD Runnerの実行確認
- GitHub Platformの**Repository (main branch)**の「Actions」タブで実行状況を確認
- 成功すると最後にCloud Run Web ServiceのURLが表示される
4.3 Cloud Run Web Service確認
# デプロイされたサービス一覧確認
gcloud run services list --region=asia-northeast1
5. ポイント・注意事項
成功のポイント
- Artifact Registry Docker Images使用: 新しいコンテナレジストリ(Container Registryは廃止予定)
- 最小権限: 必要最小限のIAMロールのみ付与
- Secrets管理: 機密情報をGitHub Platformの Secretsで安全管理
よくある問題
- 認証エラー: サービスアカウント権限不足 → IAMロール再確認
-
イメージプッシュ失敗: Docker認証設定ミス →
gcloud auth configure-docker
再実行 -
デプロイ失敗: ポート設定ミス → Dockerfileの
EXPOSE
とCloud Run Web Serviceの--port
を一致
まとめ
この設定により、
GitHub PlatformのRepository (main branch)にコードをプッシュするだけで、GitHub Actions CI/CD Runnerが自動的にGoogle Cloud PlatformのCloud Run Web Service**にデプロイする環境が構築できます。
Discussion