Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 39 additions & 28 deletions .cloudbuild/cloudbuild-staging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,57 @@ steps:
- 'asia.gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA'
waitFor:
- Build
# Cloud Runサービスを停止してDB接続を切断
# Cloud SQLでは管理者権限がないためpg_terminate_backendが使えない
# 代わりにCloud Runサービスを停止することで接続を確実に切断する
- id: StopCloudRun
name: gcr.io/google.com/cloudsdktool/cloud-sdk
entrypoint: bash
args:
- '-c'
- |
set -euo pipefail
echo "Stopping Cloud Run service to disconnect database connections..."

# サービスが存在するか確認
if gcloud run services describe $_SERVICE_NAME --region=asia-northeast1 --quiet 2>/dev/null; then
# サービスのインスタンス数を0に設定して接続を切断
gcloud run services update $_SERVICE_NAME \
--region=asia-northeast1 \
--max-instances=0 \
--quiet

echo "Cloud Run service stopped. Waiting 30 seconds for connections to drain..."
sleep 30
else
echo "Cloud Run service does not exist yet. Skipping stop."
fi
waitFor:
- Push
- id: SqlProxy
name: 'gcr.io/cloudsql-docker/gce-proxy:1.16'
args:
- /cloud_sql_proxy
- '-dir=/cloudsql'
- '-instances=$_CLOUD_SQL_HOST'
waitFor:
- '-'
- StopCloudRun
volumes:
- name: db
path: /cloudsql
# 既存の接続を強制的に切断
- name: 'asia.gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA'
# Cloud SQL Proxyの起動を待つ
- id: WaitForProxy
name: 'asia.gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA'
args:
- bash
- '-c'
- |
set -euo pipefail

# Cloud SQL Proxyの起動を待つ(最大60秒)

echo "Waiting for Cloud SQL Proxy to be ready..."
TIMEOUT=60
ELAPSED=0

while [ $$ELAPSED -lt $$TIMEOUT ]; do
echo "Attempting database connection... ($$ELAPSED/$$TIMEOUT seconds)"
OUTPUT=$$(cat <<'TESTEOF' | bin/rails runner - 2>&1
Expand All @@ -63,34 +90,18 @@ steps:
EXITCODE=$$?
if [ $$EXITCODE -eq 0 ]; then
echo "$$OUTPUT"
echo "Database connection verified, proceeding to terminate connections..."
break
echo "Cloud SQL Proxy is ready."
exit 0
else
echo "Rails runner output: $$OUTPUT"
echo "Database not ready yet, waiting..."
fi
sleep 2
ELAPSED=$$(($$ELAPSED + 2))
done

if [ $$ELAPSED -ge $$TIMEOUT ]; then
echo "ERROR: Timeout waiting for Cloud SQL Proxy to be ready after $$TIMEOUT seconds"
exit 1
fi

# 接続を強制切断
echo "Terminating existing connections to bootcamp_staging database..."
cat <<'EOF' | bin/rails runner -
result = ActiveRecord::Base.connection.execute("
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = 'bootcamp_staging'
AND pid <> pg_backend_pid()
")
terminated_count = result.ntuples
puts "Terminated #{terminated_count} connection(s) to bootcamp_staging database"
EOF
id: TerminateConnections

echo "ERROR: Timeout waiting for Cloud SQL Proxy after $$TIMEOUT seconds"
exit 1
waitFor:
- Push
volumes:
Expand All @@ -116,7 +127,7 @@ steps:
- '--quiet'
id: DeleteDB
waitFor:
- TerminateConnections
- WaitForProxy
entrypoint: gcloud
volumes:
- name: db
Expand Down