在 Google Cloud Build 期间在 Google Cloud SQL 上运行 node.js 数据库迁移 [英] Run node.js database migrations on Google Cloud SQL during Google Cloud Build

查看:19
本文介绍了在 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.

推荐答案

如果使用 CloudSQLNode.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: gcr.io/cloud-builders/yarn
    waitFor: ["-"]

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

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

timeout: "1200s"

您将启动 yarn install 并下载 CloudSQL 代理 并行.完成这两个步骤后,运行启动代理,等待 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 实例连接名称 这里.SQL 连接设置中将使用相同的名称,例如host=/cloudsql/example:us-central1:pg13.

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/example:us-central1:pg13.

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

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 数据库迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆