本文介绍了在Google Cloud Build期间在Google Cloud SQL上运行node.js数据库迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Cloud Build过程中运行用node.js编写的数据库迁移.

I would like to run database migrations written in node.js during the Cloud Build process.

当前,正在执行数据库迁移命令,但似乎Cloud Build进程无权通过具有用户名/密码的IP地址连接到Cloud SQL.

Currently, the database migration command is being executed but it seems that the Cloud Build process does not have access to connect to Cloud SQL via an IP address with username/password.

推荐答案

对于 Cloud SQL Node.js ,它看起来像这样:

In the case with Cloud SQL and Node.js it would look something like this:

steps:
  # Install Node.js dependencies
  - id: yarn-install
    name: node:dubnium
    entrypoint: yarn
    waitFor: ['-']

  # Install CLoud SQL proxy
  - id: proxy-install
    name: alpine:3
    entrypoint: sh
    args:
      - '-c'
      - 'wget -O /workspace/cloud_sql_proxy https://storage.googleapis.com/cloudsql-proxy/v1.16/cloud_sql_proxy.linux.386 && chmod +x /workspace/cloud_sql_proxy'
    waitFor: ['-']

  # Migrate database schema to the latest version
  # https://knexjs.org/#Migrations-CLI
  - id: migrate
    name: node:dubnium
    entrypoint: sh
    args:
      - '-c'
      - '(/workspace/cloud_sql_proxy -dir=/workspace -instances=<CLOUD_SQL_CONNECTION> & sleep 2) && yarn run knex migrate:latest'
    timeout: '1200s'
    waitFor: ['yarn-install', 'proxy-install']

timeout: '1200s'

您将启动yarn install并在以下位置下载 Cloud SQL代理平行线.完成这两个步骤后,您将运行启动代理,等待2秒钟,最后运行yarn run knex migrate:latest.

You would launch yarn install and download Cloud SQL Proxy in parallel. Once these two steps are complete, you run launch the proxy, wait 2 seconds and finally run yarn run knex migrate:latest.

要使其正常工作,您需要 Cloud SQL Admin API 在您的GCP项目中启用.

For this to work you would need Cloud SQL Admin API enabled in your GCP project.

<CLOUD_SQL_INSTANCE>是您的Cloud SQL实例连接名称,可以在此处找到.您的SQL连接设置中将使用相同的名称,例如host=/cloudsql/my-project/us-central1/db.

Where <CLOUD_SQL_INSTANCE> is your Cloud SQL instance connection name that can be found here. The same name will be used in your SQL connection settings, e.g. host=/cloudsql/my-project/us-central1/db.

此外,请确保Cloud Build服务帐户在数据库实例所在的GCP项目中具有"Cloud SQL Client"角色.

Also, make sure that the Cloud Build service account has "Cloud SQL Client" role in the GCP project, where the db instance is located.

这篇关于在Google Cloud Build期间在Google Cloud SQL上运行node.js数据库迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:34