From 6a16e2c73c72acbc4006c02908f96a9da7e041cf Mon Sep 17 00:00:00 2001 From: Shingo Nakayama <116932109+centmount1@users.noreply.github.com> Date: Wed, 21 Jan 2026 12:51:26 +0900 Subject: [PATCH 01/11] Update app.py --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index 01013b6..3b46106 100644 --- a/app.py +++ b/app.py @@ -31,7 +31,7 @@ def hello() -> str: # https://cloud.google.com/run/docs/logging#correlate-logs logger.info("Child logger with trace Id.") - return "Hello, World!" + return "Hello, Cloud Run!" def shutdown_handler(signal_int: int, frame: FrameType) -> None: From d0a426220dc26c3b2f4fe366e1d7f7939da8b1cc Mon Sep 17 00:00:00 2001 From: Shingo Nakayama <116932109+centmount1@users.noreply.github.com> Date: Wed, 28 Jan 2026 11:33:09 +0900 Subject: [PATCH 02/11] Change return message to 'Hello, TV-Asahi!' --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index 3b46106..5f6d977 100644 --- a/app.py +++ b/app.py @@ -31,7 +31,7 @@ def hello() -> str: # https://cloud.google.com/run/docs/logging#correlate-logs logger.info("Child logger with trace Id.") - return "Hello, Cloud Run!" + return "Hello, TV-Asahi!" def shutdown_handler(signal_int: int, frame: FrameType) -> None: From 967605e01f98e1303256f8f46a3f67d7003b0559 Mon Sep 17 00:00:00 2001 From: Shingo Nakayama <116932109+centmount1@users.noreply.github.com> Date: Fri, 13 Feb 2026 15:15:42 +0900 Subject: [PATCH 03/11] Add GitHub Actions workflow for Cloud Run deployment --- .github/workflows/deploy.yml | 62 ++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..105eed8 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,62 @@ +name: Deploy to Cloud Run Direct WIF + +on: + push: + branches: + - main + +env: + PROJECT_ID: # 自身のプロジェクトIDに書き換え + GAR_LOCATION: asia-northeast1 + REPOSITORY: #自身のarのレジストリ + SERVICE: #自身のcloud runのサービス名 + REGION: asia-northeast1 + +jobs: + deploy: + # OIDCトークン発行のために必須 + permissions: + contents: 'read' + id-token: 'write' + + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + # Direct Workload Identity Federationによる認証 + - name: Google Auth + id: auth + uses: 'google-github-actions/auth@v2' + with: + token_format: 'access_token' + workload_identity_provider: '${{ secrets.WIF_PROVIDER }}' + + # gcloudコマンドのセットアップ (ここを追加) + - name: Set up Cloud SDK + uses: 'google-github-actions/setup-gcloud@v2' + with: + version: '>= 363.0.0' + + # Cloud Build にビルドとプッシュを依頼 (ここを変更) + # ローカル(GitHub Actions)のソースをGCPにアップロードし、Cloud Build上でビルド&プッシュします + - name: Build and Push with Cloud Build + run: |- + gcloud builds submit \ + --quiet \ + --tag "${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }}" \ + . + + # Cloud Runへのデプロイ (ここは変更なし) + # Cloud Buildによってプッシュされた上記のimageタグを指定します + - name: Deploy to Cloud Run + id: deploy + uses: 'google-github-actions/deploy-cloudrun@v2' + with: + service: '${{ env.SERVICE }}' + region: '${{ env.REGION }}' + image: '${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }}' + flags: '--allow-unauthenticated' + + - name: Show Output + run: echo ${{ steps.deploy.outputs.url }} From a1afe12ab859f8c900ff1503c211c822fcbdc3b3 Mon Sep 17 00:00:00 2001 From: Shingo Nakayama <116932109+centmount1@users.noreply.github.com> Date: Fri, 13 Feb 2026 15:21:24 +0900 Subject: [PATCH 04/11] Update project ID, repository, and service names --- .github/workflows/deploy.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 105eed8..1885f6d 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -6,10 +6,10 @@ on: - main env: - PROJECT_ID: # 自身のプロジェクトIDに書き換え + PROJECT_ID: ex-ai-training-program # 自身のプロジェクトIDに書き換え GAR_LOCATION: asia-northeast1 - REPOSITORY: #自身のarのレジストリ - SERVICE: #自身のcloud runのサービス名 + REPOSITORY: nakayama-20260213 # 自身のarのレジストリ + SERVICE: nakayama-20260213 # 自身のcloud runのサービス名 REGION: asia-northeast1 jobs: From 1e5080ef39bc6e6d34a435cd179ebbd764b837cf Mon Sep 17 00:00:00 2001 From: Shingo Nakayama <116932109+centmount1@users.noreply.github.com> Date: Fri, 13 Feb 2026 15:31:31 +0900 Subject: [PATCH 05/11] Update deploy.yml --- .github/workflows/deploy.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 1885f6d..e98100e 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -31,6 +31,7 @@ jobs: with: token_format: 'access_token' workload_identity_provider: '${{ secrets.WIF_PROVIDER }}' + project_id: '${{ env.PROJECT_ID }}' # gcloudコマンドのセットアップ (ここを追加) - name: Set up Cloud SDK From b9c8b7782f9330ec3c95e8669d33656ef4f2a59d Mon Sep 17 00:00:00 2001 From: Shingo Nakayama <116932109+centmount1@users.noreply.github.com> Date: Fri, 13 Feb 2026 15:53:42 +0900 Subject: [PATCH 06/11] Refactor deploy.yml for project configuration Updated environment variables and configuration for deployment. --- .github/workflows/deploy.yml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index e98100e..e5771d7 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -6,10 +6,10 @@ on: - main env: - PROJECT_ID: ex-ai-training-program # 自身のプロジェクトIDに書き換え + PROJECT_ID: ex-ai-training-program  # 自身のプロジェクトIDに書き換え GAR_LOCATION: asia-northeast1 - REPOSITORY: nakayama-20260213 # 自身のarのレジストリ - SERVICE: nakayama-20260213 # 自身のcloud runのサービス名 + REPOSITORY: nakayama-20260213 #自身のarのレジストリ + SERVICE: nakayama-20260213 #自身のcloud runのサービス名 REGION: asia-northeast1 jobs: @@ -29,15 +29,15 @@ jobs: id: auth uses: 'google-github-actions/auth@v2' with: - token_format: 'access_token' - workload_identity_provider: '${{ secrets.WIF_PROVIDER }}' project_id: '${{ env.PROJECT_ID }}' + workload_identity_provider: '${{ secrets.WIF_PROVIDER }}' # gcloudコマンドのセットアップ (ここを追加) - name: Set up Cloud SDK uses: 'google-github-actions/setup-gcloud@v2' with: version: '>= 363.0.0' + project_id: '${{ env.PROJECT_ID }}' # Cloud Build にビルドとプッシュを依頼 (ここを変更) # ローカル(GitHub Actions)のソースをGCPにアップロードし、Cloud Build上でビルド&プッシュします @@ -46,6 +46,7 @@ jobs: gcloud builds submit \ --quiet \ --tag "${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }}" \ + --project '${{ env.PROJECT_ID }}\ . # Cloud Runへのデプロイ (ここは変更なし) From 04cbbac25644d7d16828dfffae6b6753770b3f0c Mon Sep 17 00:00:00 2001 From: Shingo Nakayama <116932109+centmount1@users.noreply.github.com> Date: Fri, 13 Feb 2026 15:58:09 +0900 Subject: [PATCH 07/11] Refactor comments and fix formatting in deploy.yml Updated comments and fixed string formatting in deploy.yml. --- .github/workflows/deploy.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index e5771d7..3d95802 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -40,15 +40,15 @@ jobs: project_id: '${{ env.PROJECT_ID }}' # Cloud Build にビルドとプッシュを依頼 (ここを変更) - # ローカル(GitHub Actions)のソースをGCPにアップロードし、Cloud Build上でビルド&プッシュします + # Cloud Build にビルドとプッシュを依頼 - name: Build and Push with Cloud Build run: |- gcloud builds submit \ --quiet \ --tag "${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }}" \ - --project '${{ env.PROJECT_ID }}\ + --project "${{ env.PROJECT_ID }}" \ . - + # Cloud Runへのデプロイ (ここは変更なし) # Cloud Buildによってプッシュされた上記のimageタグを指定します - name: Deploy to Cloud Run From b304bf36a5567a49e5381a36d6cf62db5a7eab51 Mon Sep 17 00:00:00 2001 From: Shingo Nakayama <116932109+centmount1@users.noreply.github.com> Date: Fri, 13 Feb 2026 16:02:04 +0900 Subject: [PATCH 08/11] Update deploy.yml --- .github/workflows/deploy.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 3d95802..e131ada 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -6,9 +6,9 @@ on: - main env: - PROJECT_ID: ex-ai-training-program  # 自身のプロジェクトIDに書き換え + PROJECT_ID: ex-ai-training-program #自身のプロジェクトIDに書き換え GAR_LOCATION: asia-northeast1 - REPOSITORY: nakayama-20260213 #自身のarのレジストリ + REPOSITORY: nakayama-20260213 #自身のarのレジストリ SERVICE: nakayama-20260213 #自身のcloud runのサービス名 REGION: asia-northeast1 From a6ef48809a76eb2a84826467c319b078684cedf2 Mon Sep 17 00:00:00 2001 From: Shingo Nakayama <116932109+centmount1@users.noreply.github.com> Date: Fri, 13 Feb 2026 16:29:28 +0900 Subject: [PATCH 09/11] Create preview.yml --- .github/workflows/preview.yml | 183 ++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 .github/workflows/preview.yml diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml new file mode 100644 index 0000000..f10ba69 --- /dev/null +++ b/.github/workflows/preview.yml @@ -0,0 +1,183 @@ +name: Deploy Preview to Cloud Run + + + # 【課題1】トリガーの設定 + # PRが作られたとき,閉じられた時などにワークフローが動くように、イベントタイプを追加してください + +on: + pull_request: + branches: + - main + types: [opened, synchronize, closed] + +env: + # ※ 演習環境に合わせて値を変更してください + PROJECT_ID: ex-ai-training-program + GAR_LOCATION: us-central1 + REPOSITORY: nakayama-20260213 + SERVICE_BASE: python-microservice + REGION: us-central1 + +jobs: + # =================================================== + # Job 1: プレビュー環境のデプロイ (Open/Update時) + # =================================================== + deploy-preview: + # PRがClosedの時はこのジョブをスキップしても良い + runs-on: ubuntu-latest + permissions: + contents: 'read' + id-token: 'write' + pull-requests: 'write' + + steps: + - name: Checkout + uses: actions/checkout@v4 + + + # 【課題2】サービス名の生成 + # 以下の要件で環境変数 SERVICE_NAME を設定するコマンドを記述してください + # - 形式: [プレフィックス]-pr-[PR番号] + # - ヒント: PR番号は github.event.number で取得できます + # - ヒント: GITHUB_ENV への書き込みが必要です + - name: Set Service Name + run: echo "SERVICE_NAME=${{ env.SERVICE_BASE }}-pr-${{ github.event.number }}" >> $GITHUB_ENV + + # Google Cloud 認証 + - name: Google Auth + id: auth + uses: 'google-github-actions/auth@v2' + with: + workload_identity_provider: '${{ secrets.WIF_PROVIDER }}' + project_id: '${{ env.PROJECT_ID }}' + + # gcloud コマンドのセットアップ + - name: Set up Cloud SDK + uses: 'google-github-actions/setup-gcloud@v2' + with: + project_id: '${{ env.PROJECT_ID }}' + + # 【課題3】Cloud Build でのビルド & Push + # Cloud Build を使ってコンテナをビルドし、Artifact Registry に Push するコマンドを記述してください + # - コマンド: gcloud builds submit + # - タグ: ${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE_NAME }}:${{ github.sha }} + # - ビルドパス: カレントディレクトリ (.) + - name: Build and Push via Cloud Build + run: | + gcloud builds submit . \ + --tag ${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE_NAME }}:${{ github.sha }} + + # 【課題4】Cloud Run へのデプロイ + # google-github-actions/deploy-cloudrun@v2 アクションの設定を完成させてください + # 必要な設定: service, region, image, tag(PR番号), flags(--allow-unauthenticated) + - name: Deploy to Cloud Run + id: deploy + uses: 'google-github-actions/deploy-cloudrun@v2' + with: + service: ${{ env.SERVICE_NAME }} + region: ${{ env.REGION }} + image: ${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE_NAME }}:${{ github.sha }} + flags: '--allow-unauthenticated' + + + # デプロイ完了通知 (ここはそのまま利用) + - name: Comment on PR + uses: actions/github-script@v7 + with: + script: | + const url = '${{ steps.deploy.outputs.url }}'; + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: `🚀 **Preview Environment Deployed!**\n\nApp is running at: ${url}\n\n(Latest commit: ${context.sha})` + }) + + # =================================================== + # Job 2: プレビュー環境の削除 (Close/Merge時) + # =================================================== + cleanup-preview: + # 【課題5】実行条件の設定 + # PRが「閉じられた (closed)」時のみ、このジョブが実行される条件式を記述してください + if: github.event.action == 'closed' + runs-on: ubuntu-latest + permissions: + contents: 'read' + id-token: 'write' + pull-requests: 'write' + + steps: + # Job 1と同様にサービス名を定義 (ここは記入済み) + - name: Set Service Name + run: echo "SERVICE_NAME=${{ env.SERVICE_BASE }}-pr-${{ github.event.number }}" >> $GITHUB_ENV + + - name: Google Auth + uses: 'google-github-actions/auth@v2' + with: + workload_identity_provider: '${{ secrets.WIF_PROVIDER }}' + + - name: Set up Cloud SDK + uses: 'google-github-actions/setup-gcloud@v2' + + + + # 【課題6】Cloud Run サービスの削除 + # デプロイされたプレビュー環境を削除する gcloud コマンドを記述してください + # - コマンド: gcloud run services delete + # - 対象: 環境変数 SERVICE_NAME + # - リージョン: 環境変数 REGION + # - 確認プロンプト: スキップするフラグ (--quiet) を必ずつけること + - name: Delete Cloud Run Service + run: | + gcloud run services delete ${{ env.SERVICE_NAME }} \ + --region ${{ env.REGION }} \ + --quiet + + - name: Comment on PR + uses: actions/github-script@v7 + with: + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: `🗑️ **Preview Environment Deleted.**\n\nThe Cloud Run service has been cleaned up.` + }) + + + # WIF認証 + - name: Google Auth + uses: 'google-github-actions/auth@v2' + with: + workload_identity_provider: '${{ secrets.WIF_PROVIDER }}' + + # 【課題5】削除コマンドの記述 + # - コマンド: gcloud run services delete + # - 対象: 環境変数 SERVICE_NAME + # - リージョン: 環境変数 REGION + # - 確認プロンプト: スキップ (--quiet) + - name: Delete Cloud Run Service + run: | + echo "Deleting service: ${{ env.SERVICE_NAME }}" + ______ ______ ______ ${{ env.SERVICE_NAME }} \ + --region ${{ env.REGION }} \ + --quiet + + - name: Comment on PR + + + uses: actions/github-script@v7 + with: + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: `🗑️ **Preview Environment Deleted.**\n\nThe Cloud Run service has been cleaned up.` + }) + + + + + + From 2ccb170febf38ba9674cc8829351505ccae5726d Mon Sep 17 00:00:00 2001 From: Shingo Nakayama <116932109+centmount1@users.noreply.github.com> Date: Fri, 13 Feb 2026 16:32:56 +0900 Subject: [PATCH 10/11] Update preview.yml From 46ec9c78329a5132a7bb72da0af826b01a1177c6 Mon Sep 17 00:00:00 2001 From: Shingo Nakayama <116932109+centmount1@users.noreply.github.com> Date: Fri, 13 Feb 2026 16:37:56 +0900 Subject: [PATCH 11/11] Add test.txt with initial content --- test/test.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 test/test.txt diff --git a/test/test.txt b/test/test.txt new file mode 100644 index 0000000..9daeafb --- /dev/null +++ b/test/test.txt @@ -0,0 +1 @@ +test