This Cloud Build configuration deploys the FastAPI application to Google Cloud Run.
- Google Cloud project with billing enabled
- Cloud Build API enabled
- Cloud Run API enabled
- Artifact Registry API enabled
gcloud services enable cloudbuild.googleapis.com
gcloud services enable run.googleapis.com
gcloud services enable artifactregistry.googleapis.comgcloud artifacts repositories create cloud-run-source-deploy \
--repository-format=docker \
--location=us-central1 \
--description="Docker repository for Cloud Run deployments"echo -n "postgresql://user:password@host:5432/dbname" | \
gcloud secrets create HACKATON_DATABASE_URL --data-file=-Grant Cloud Run access to the secret:
PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format="value(projectNumber)")
gcloud secrets add-iam-policy-binding HACKATON_DATABASE_URL \
--member="serviceAccount:${PROJECT_NUMBER}-compute@developer.gserviceaccount.com" \
--role="roles/secretmanager.secretAccessor"-
Connect your repository to Cloud Build:
- Go to Cloud Build > Triggers
- Click "Create Trigger"
- Connect your repository (GitHub, Bitbucket, etc.)
- Set the configuration file path to
cloudbuild.yaml - Set the substitution variables:
_SERVICE_NAME: self-improving-engine-api_DEPLOY_REGION: us-central1_AR_HOSTNAME: us-central1-docker.pkg.dev
- Save and enable the trigger
-
Push to your repository:
git push origin main
gcloud builds submit --config=cloudbuild.yaml \
--substitutions=_SERVICE_NAME=self-improving-engine-api,_DEPLOY_REGION=us-central1,_AR_HOSTNAME=us-central1-docker.pkg.devThe current configuration uses:
- Memory: 512Mi
- CPU: 1
- Timeout: 300 seconds
- Concurrency: 80
- Min Instances: 0 (scales to zero)
- Max Instances: 10
To adjust these values, modify the args section in the Deploy step.
The build config sets:
DEBUG=False- Production modeLOG_LEVEL=INFO- Logging level
The DATABASE_URL environment variable is populated from the HACKATON_DATABASE_URL secret in Secret Manager. To update it:
echo -n "new-database-url" | gcloud secrets versions add HACKATON_DATABASE_URL --data-file=-View logs:
gcloud run services logs read self-improving-engine-api --region us-central1View service details:
gcloud run services describe self-improving-engine-api --region us-central1-
Check build logs:
gcloud builds list --limit=5 gcloud builds log <BUILD_ID>
-
Test Docker build locally:
docker build -t test-image .
-
Check service logs:
gcloud run services logs read self-improving-engine-api --region us-central1 --limit=50 -
Verify secrets are accessible:
gcloud secrets versions access latest --secret=HACKATON_DATABASE_URL
- Ensure Cloud SQL instance is running
- Verify connection string format
- Check network connectivity from Cloud Run to database
- Min instances = 0: Service scales to zero when idle
- Memory: Start with 512Mi, increase if needed
- CPU: Scale based on actual usage
- Timeout: Set based on expected request duration
- Secrets are managed through Secret Manager
- Non-root user in container
- HTTPS enforced by Cloud Run
- IAM-based access control