From 4dde3374e8d2ee6a81bddc0798ef9ea368fbb264 Mon Sep 17 00:00:00 2001 From: peak-nexus <141696770+peak-nexus@users.noreply.github.com> Date: Wed, 21 Jan 2026 12:51:01 +0900 Subject: [PATCH 01/13] Update app.py --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index 01013b6..fa8b0d2 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, World!!!!!!!!!!!!!!!!!!!" def shutdown_handler(signal_int: int, frame: FrameType) -> None: From 7a64b271a2eb542ecb95d06a76f61484160e832a Mon Sep 17 00:00:00 2001 From: peak-nexus <141696770+peak-nexus@users.noreply.github.com> Date: Wed, 28 Jan 2026 11:35:04 +0900 Subject: [PATCH 02/13] Update app.py --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index fa8b0d2..efe5173 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, World dayo" def shutdown_handler(signal_int: int, frame: FrameType) -> None: From 5a6b9730de45a46e0189781df9b41a0d2a69fab6 Mon Sep 17 00:00:00 2001 From: peak-nexus <141696770+peak-nexus@users.noreply.github.com> Date: Fri, 13 Feb 2026 15:26:37 +0900 Subject: [PATCH 03/13] Create deploy.yml --- .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..b0dca7c --- /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:ex-ai-training # 自身のプロジェクトIDに書き換え + GAR_LOCATION: asia-northeast1 + REPOSITORY: cloud-run-source-deploy-ty-20260213 #自身のarのレジストリ + SERVICE: python-microservice-ty-20260213 #自身の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 6b04604d3ee73ea8ff472c399e10b033264bbe63 Mon Sep 17 00:00:00 2001 From: peak-nexus <141696770+peak-nexus@users.noreply.github.com> Date: Fri, 13 Feb 2026 15:29:47 +0900 Subject: [PATCH 04/13] Update deploy.yml --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index b0dca7c..bf5a635 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -6,7 +6,7 @@ on: - main env: - PROJECT_ID:ex-ai-training # 自身のプロジェクトIDに書き換え + PROJECT_ID:ex-ai-training-program # 自身のプロジェクトIDに書き換え GAR_LOCATION: asia-northeast1 REPOSITORY: cloud-run-source-deploy-ty-20260213 #自身のarのレジストリ SERVICE: python-microservice-ty-20260213 #自身のcloud runのサービス名 From bebb63402bc6ec5b4f714ce75209ca8938c8ca8d Mon Sep 17 00:00:00 2001 From: peak-nexus <141696770+peak-nexus@users.noreply.github.com> Date: Fri, 13 Feb 2026 15:31:55 +0900 Subject: [PATCH 05/13] Update deploy.yml --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index bf5a635..52153cc 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -6,7 +6,7 @@ on: - main env: - PROJECT_ID:ex-ai-training-program # 自身のプロジェクトIDに書き換え + PROJECT_ID: ex-ai-training-program # 自身のプロジェクトIDに書き換え GAR_LOCATION: asia-northeast1 REPOSITORY: cloud-run-source-deploy-ty-20260213 #自身のarのレジストリ SERVICE: python-microservice-ty-20260213 #自身のcloud runのサービス名 From 70375e7752b31f298b8a717cbf5a6ca74bd6f976 Mon Sep 17 00:00:00 2001 From: peak-nexus <141696770+peak-nexus@users.noreply.github.com> Date: Fri, 13 Feb 2026 15:34:32 +0900 Subject: [PATCH 06/13] Update deploy.yml --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 52153cc..254ea15 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -29,7 +29,7 @@ jobs: id: auth uses: 'google-github-actions/auth@v2' with: - token_format: 'access_token' + project_id: '${{ env.PROJECT_ID }} workload_identity_provider: '${{ secrets.WIF_PROVIDER }}' # gcloudコマンドのセットアップ (ここを追加) From 22287d96de795191d538acb8ca2ce609bccb2953 Mon Sep 17 00:00:00 2001 From: peak-nexus <141696770+peak-nexus@users.noreply.github.com> Date: Fri, 13 Feb 2026 15:35:44 +0900 Subject: [PATCH 07/13] Update deploy.yml --- .github/workflows/deploy.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 254ea15..397df2c 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -37,6 +37,7 @@ jobs: uses: 'google-github-actions/setup-gcloud@v2' with: version: '>= 363.0.0' + project_id: '${{ env.PROJECT_ID }} # Cloud Build にビルドとプッシュを依頼 (ここを変更) # ローカル(GitHub Actions)のソースをGCPにアップロードし、Cloud Build上でビルド&プッシュします @@ -45,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_id: '${{ env.PROJECT_ID }}\ . # Cloud Runへのデプロイ (ここは変更なし) From b833ea7d717f40a4dc9cbfc83912d226220efbcb Mon Sep 17 00:00:00 2001 From: peak-nexus <141696770+peak-nexus@users.noreply.github.com> Date: Fri, 13 Feb 2026 15:39:46 +0900 Subject: [PATCH 08/13] Update deploy.yml --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 397df2c..8f7f35a 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -46,7 +46,7 @@ jobs: gcloud builds submit \ --quiet \ --tag "${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }}" \ - --project_id: '${{ env.PROJECT_ID }}\ + --project_id: '${{ env.PROJECT_ID }}\ . # Cloud Runへのデプロイ (ここは変更なし) From 7e0582bf51f6c946f31733ed3b94d307c0a7d520 Mon Sep 17 00:00:00 2001 From: peak-nexus <141696770+peak-nexus@users.noreply.github.com> Date: Fri, 13 Feb 2026 15:41:40 +0900 Subject: [PATCH 09/13] Update deploy.yml --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 8f7f35a..b5d9547 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -29,7 +29,7 @@ jobs: id: auth uses: 'google-github-actions/auth@v2' with: - project_id: '${{ env.PROJECT_ID }} + project_id: '${{ env.PROJECT_ID }}' workload_identity_provider: '${{ secrets.WIF_PROVIDER }}' # gcloudコマンドのセットアップ (ここを追加) From fb12595604f20be71a41097cd4a88009feb2efdd Mon Sep 17 00:00:00 2001 From: peak-nexus <141696770+peak-nexus@users.noreply.github.com> Date: Fri, 13 Feb 2026 15:43:13 +0900 Subject: [PATCH 10/13] Update deploy.yml --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index b5d9547..d09bde7 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -37,7 +37,7 @@ jobs: uses: 'google-github-actions/setup-gcloud@v2' with: version: '>= 363.0.0' - project_id: '${{ env.PROJECT_ID }} + project_id: '${{ env.PROJECT_ID }}' # Cloud Build にビルドとプッシュを依頼 (ここを変更) # ローカル(GitHub Actions)のソースをGCPにアップロードし、Cloud Build上でビルド&プッシュします From 5158325ac4a569c53d4b7509b69f0ebbec031c06 Mon Sep 17 00:00:00 2001 From: peak-nexus <141696770+peak-nexus@users.noreply.github.com> Date: Fri, 13 Feb 2026 15:49:16 +0900 Subject: [PATCH 11/13] Update deploy.yml --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index d09bde7..e1a1d42 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -46,7 +46,7 @@ jobs: gcloud builds submit \ --quiet \ --tag "${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }}" \ - --project_id: '${{ env.PROJECT_ID }}\ + --project: '${{ env.PROJECT_ID }}'\ . # Cloud Runへのデプロイ (ここは変更なし) From 65b109b7fefca25679c9d819b0a1183180cc8d26 Mon Sep 17 00:00:00 2001 From: peak-nexus <141696770+peak-nexus@users.noreply.github.com> Date: Fri, 13 Feb 2026 15:51:09 +0900 Subject: [PATCH 12/13] Update deploy.yml --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index e1a1d42..f47f010 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -46,7 +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 }}'\ + --project '${{ env.PROJECT_ID }}'\ . # Cloud Runへのデプロイ (ここは変更なし) From e870666dc7a84ba998497ace6c3c4dbb7288fd26 Mon Sep 17 00:00:00 2001 From: peak-nexus <141696770+peak-nexus@users.noreply.github.com> Date: Fri, 13 Feb 2026 16:39:12 +0900 Subject: [PATCH 13/13] Create preview.yml --- .github/workflows/preview.yml | 180 ++++++++++++++++++++++++++++++++++ 1 file changed, 180 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..faa26ed --- /dev/null +++ b/.github/workflows/preview.yml @@ -0,0 +1,180 @@ +name: Deploy Preview to Cloud Run + + + # 【課題1】トリガーの設定 + # PRが作られたとき,閉じられた時などにワークフローが動くように、イベントタイプを追加してください + +on: + pull_request: + branches: + - main + types: [opened, closed,] + + +env: + # ※ 演習環境に合わせて値を変更してください + PROJECT_ID: ex-ai-training-program # 自身のプロジェクトIDに書き換え + GAR_LOCATION: asia-northeast1 + REPOSITORY: cloud-run-source-deploy-ty-20260213 #自身のarのレジストリ + SERVICE: python-microservice-ty-20260213 #自身のcloud runのサービス名 + REGION: asia-northeast1 + +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}}-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 \ + --quiet \ + --tag "${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }}" \ + --project '${{ env.PROJECT_ID }}'\ + . + + # 【課題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 }}' + region: '${{ env.REGION }}' + image: '${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }}' + tag: "${{github.event.number}}" + 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 }}-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 "$SERVICE_NAME" --region "$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 }}" + gcloud run services delete ${{ env.SERVICE }} \ + --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.` + }) +