简体   繁体   中英

Accessing Cloud SQL from Cloud Build

I want to configure CI/CD from Cloud Repositories that builds my CMS (Directus) when I push to main repository.

In the build-time, the project needs to access Cloud SQL . But I get this error: 在此处输入图像描述

I tried this database configuration with gcloud app deploy and it connects Cloud SQL and runs.

cloudbuild.yaml (It crashes at second step, so I didn't add other steps for simplicity):

steps:

- name: node:16
  entrypoint: npm
  args: ['install']
  dir: cms

- name: node:16
  entrypoint: npm
  args: ['run', 'start']
  dir: cms
  env:
  - 'NODE_ENV=PRODUCTION'
  - 'EXTENSIONS_PATH="./extensions"'
  - 'DB_CLIENT=pg'
  - 'DB_HOST=/cloudsql/XXX:europe-west1:XXX'
  - 'DB_PORT="5432"'
  - 'DB_DATABASE="XXXXX"'
  - 'DB_USER="postgres"'
  - 'DB_PASSWORD="XXXXXX"'
  - 'KEY="XXXXXXXX"'
  - 'SECRET="XXXXXXXXXXXX"'

Node-pg (node library) adds /.s.PGSQL.5432 at the end automatically. That's why it is not written in DB_HOST .

IAM roles: 在此处输入图像描述

How can I solve this error? I read so many answers in Stackoverflow but none of them helped me. I found this article but I didn't fully understand how to implement it in my case ( https://cloud.google.com/sql/docs/postgres/connect-build ).

Without your full Cloud Build yaml, it's hard to say for sure - but, it looks like you aren't following the steps in the documentation correctly .

Roughly what you should be doing is:

  1. Downloading the cloud_sql_proxy into your container space
  2. In a follow up step, start the cloud_sql_proxy then (in the same step) run your script, connecting to the proxy via either tcp or unix socket.

I don't see your yaml describing the proxy at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM