0

I investigated this question on StackOverflow but unfortunately to me nothing helps.

I have cloudbuild.yaml file

steps:
  - name: 'node:14.16.0'
    entrypoint: 'yarn'
    id: yarn-install
    args: ['install']
    waitFor: ["-"]

  - name: gcr.io/cloud-builders/yarn
    id: proxy-install
    entrypoint: sh
    args:
      - "-c"
      - "wget https://storage.googleapis.com/cloudsql-proxy/v1.23.0/cloud_sql_proxy.linux.amd64 -O /workspace/cloud_sql_proxy && chmod +x /workspace/cloud_sql_proxy"
    waitFor: ["-"]

  - id: migrate
    name: gcr.io/cloud-builders/yarn
    env:
      - NODE_ENV=$_NODE_ENV
      - DB_NAME=$_DB_NAME
      - DB_USER=$_DB_USER
      - DB_PASSWORD=MY_FAKE_PASSWORD
      - CLOUD_SQL_CONNECTION_NAME=$_CLOUD_SQL_CONNECTION_NAME
    entrypoint: sh
    args:
      - "-c"
      - "(./workspace/cloud_sql_proxy -dir=/workspace -instances=$_CLOUD_SQL_CONNECTION_NAME & sleep 2) && yarn run knex migrate:latest"
    timeout: "1200s"
    waitFor: ["yarn-install", "proxy-install"]

I want to connect to my Cloud SQL database to apply schema migration by using yarn run knex migrate:latest.

But it fails on the migrate step

Logs from Cloud Build

info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
    at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1145:16)
Error: connect ENOENT /cloudsql/project:us-east1:project-posgresql1/.s.PGSQL.5432
Using environment: production
Working directory changed to /workspace/src/infrastructure/knex
Requiring external module ts-node/register
$ ./node_modules/knex/bin/cli.js --knexfile=./src/infrastructure/knex/knex.config.ts migrate:latest --env production migrate:latest
yarn run v1.22.5
sh: 1: ./workspace/cloud_sql_proxy: not found
Already have image (with digest): gcr.io/cloud-builders/yarn

I don't know how to debug it correctly... Could you help me to find the root cause of the problem?

P.S.

...@cloudbuild.gserviceaccount.com has the following roles

  • Cloud Build Service Account
  • Cloud SQL Client
  • Service Account User
  • Cloud Run Admin
  • Secret Manager Secret Accessor

P.S.S knex is a JavaScript query builder for SQL-like databases

Roman Mahotskyi
  • 4,576
  • 5
  • 35
  • 68
  • Could you add some context in what are you trying to do? I don't know if I quite get it. It is a Cloud SQL proxy error? Have you tried accessing the database normally with cloud sql proxy? – Daniel Gomez Jun 11 '21 at 14:15
  • @DanielGomez I am a bit novice to Google Cloud Platform. All I do is reading guides and trying mimic their code. Don't know too much where this error comes from – Roman Mahotskyi Jun 11 '21 at 14:26

1 Answers1

0

By design, you need to run the cloud-sql prowy in the same step as the proxy install step. However, I never tested with the waitFor["-"], it might be a workaround. But this solution should work for you

  - name: gcr.io/cloud-builders/yarn
    id: proxy-install
    entrypoint: sh
    env:
      - NODE_ENV=$_NODE_ENV
      - DB_NAME=$_DB_NAME
      - DB_USER=$_DB_USER
      - DB_PASSWORD=MY_FAKE_PASSWORD
      - CLOUD_SQL_CONNECTION_NAME=$_CLOUD_SQL_CONNECTION_NAME
    args:
      - "-c"
      - |
         wget https://storage.googleapis.com/cloudsql-proxy/v1.23.0/cloud_sql_proxy.linux.amd64 -O /workspace/cloud_sql_proxy && chmod +x /workspace/cloud_sql_proxy
         (./workspace/cloud_sql_proxy -dir=/workspace -instances=$_CLOUD_SQL_CONNECTION_NAME & sleep 2) && yarn run knex migrate:latest
    timeout: "1200s"

Have a try and let me know

guillaume blaquiere
  • 66,369
  • 2
  • 47
  • 76