To see this snippet in the context of a web application, view the README on GitHub.
Note:
To see this snippet in the context of a web application, view the README on GitHub.
To see this snippet in the context of a web application, view the README on GitHub.
To see this snippet in the context of a web application, view the README on GitHub.
To see this snippet in the context of a web application, view the README on GitHub.
To see this snippet in the context of a web application, view the README on GitHub.
You can use the Cloud SQL Auth Proxy in a Cloud Build step to allow connections to your database. This configuration:
steps: -id:install-proxy name:gcr.io/cloud-builders/wget entrypoint:sh args: --c -|wget-O/workspace/cloud-sql-proxyhttps://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/2.15.2 chmod+x/workspace/cloud-sql-proxy -id:migrate waitFor:['install-proxy']name:YOUR_CONTAINER_IMAGE_NAMEentrypoint:sh env: -"DATABASE_NAME=${_DATABASE_NAME}"-"DATABASE_USER=${_DATABASE_USER}"-"DATABASE_PORT=${_DATABASE_PORT}"-"INSTANCE_CONNECTION_NAME=${_INSTANCE_CONNECTION_NAME}"secretEnv: -DATABASE_PASS args: -"-c"-|/workspace/cloud-sql-proxy${_INSTANCE_CONNECTION_NAME}--port${_DATABASE_PORT}&sleep2;# Cloud SQL Proxy is now up and running, add your own logic below to connectpythonmigrate.py# For exampleoptions: dynamic_substitutions:truesubstitutions: _DATABASE_USER:myuser _DATABASE_NAME:mydatabase _INSTANCE_CONNECTION_NAME:${PROJECT_ID}:us-central1:myinstance _DATABASE_PORT:'5432'_DATABASE_PASSWORD_KEY:database_password _AR_REPO_REGION:us-central1 _AR_REPO_NAME:my-docker-repo _IMAGE_NAME:${_AR_REPO_REGION}-docker.pkg.dev/${PROJECT_ID}/${_AR_REPO_NAME}/sample-sql-proxy availableSecrets: secretManager: -versionName:projects/$PROJECT_ID/secrets/${_DATABASE_PASSWORD_KEY}/versions/latest env:"DATABASE_PASS"
The Cloud Build code sample shows how you might run a hypothetical migrate.py
script after deploying the previous sample app to update its Cloud SQL database using the Cloud SQL Auth Proxy and Cloud Build. To run this Cloud Build code sample the setup steps required are:
sql-proxy
Dockerfile
file in the sql-proxy
folder with the following single line of code for its file contents: FROMgcr.io/gcp-runtimes/ubuntu_20_0_4
cloudbuild.yaml
file in the sql-proxy
folder.cloudbuild.yaml
file: cloudbuild.yaml
file.mydatabase
myuser
myinstance
database_password
in Secret Manager. sql-proxy
folder. cloudbuild.yaml
file using the following examples: os.getenv('DATABASE_NAME')
os.getenv('DATABASE_USER')
os.getenv('DATABASE_PASS')
os.getenv('INSTANCE_CONNECTION_NAME')
migrate.sh
) use the following examples: $DATABASE_NAME
$DATABASE_USER
$DATABASE_PASS
$INSTANCE_CONNECTION_NAME
gcloud builds submit
command to build a container with the Cloud SQL Auth Proxy, start the Cloud SQL Auth Proxy, and run the migrate.py
script: gcloudbuildssubmit--configcloudbuild.yaml
For private IP paths, your application connects directly to your instance through private pools. This method uses TCP to connect directly to the Cloud SQL instance without using the Cloud SQL Auth Proxy.
Connect using the private IP address of your Cloud SQL instance as the host and port 5432
.
To see this snippet in the context of a web application, view the README on GitHub.
To see this snippet in the context of a web application, view the README on GitHub.
Note:
To see this snippet in the context of a web application, view the README on GitHub.
To see this snippet in the context of a web application, view the README on GitHub.
To see this snippet in the context of a web application, view the README on GitHub.
To see this snippet in the context of a web application, view the README on GitHub.
To see this snippet in the context of a web application, view the README on GitHub.
You can then create a Cloud Build step to run your code directly.
The Cloud Build code sample above shows how you might run a hypothetical migrate script after deploying the sample app above to update its Cloud SQL database using Cloud Build. To run this Cloud Build code sample the setup steps required are:
sql-private-pool
Dockerfile
file in the sql-private-pool
folder with the following single line of code for its file contents: FROM gcr.io/gcp-runtimes/ubuntu_20_0_4
cloudbuild.yaml
file in the sql-private-pool
folder.cloudbuild.yaml
file: cloudbuild.yaml
file.mydatabase
myuser
databasehost
, in the form host:port
.database_password
in Secret Manager. sql-proxy
folder. cloudbuild.yaml
file using the following examples: os.getenv('DATABASE_NAME')
os.getenv('DATABASE_USER')
os.getenv('DATABASE_PASS')
os.getenv('DATABASE_HOST')
migrate.sh
) use the following examples: $DATABASE_NAME
$DATABASE_USER
$DATABASE_PASS
$DATABASE_HOST
gcloud builds submit
command to build a container with the Cloud SQL Auth Proxy, start the Cloud SQL Auth Proxy, and run the migrate.py
script: gcloud builds submit --config cloudbuild.yaml
You can use the Cloud SQL Auth Proxy when testing your application locally. See the quickstart for using the Cloud SQL Auth Proxy for detailed instructions.
You can also test using the Cloud SQL Proxy via a docker container.
By configuring Cloud Build to connect to Cloud SQL, you can run database schema migration tasks in Cloud Build using the same code you would deploy to any other serverless platform.
You can use Secret Manager to include sensitive information in your builds.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-04-17 UTC.