Skip to content

Commit fd6ec65

Browse files
chore(ci): migrate mysql/mysql + proof of concept proxy helper (#4040)
* wip: proof of concept cloud-sql mysql * chmod * add sleep to ensure proxy starts before use * use correct envvar * extra bit * debugging * instance_host is deleted by the server-unix test * proxy all tests * accepting gemini-code-assist code Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * oop * wip: tcp or unix * syntax * cause she looks like a flower * more shell logic * sh * continued proof of concept * call shutdown * format value * order? * debug: echo * hoisted by my own petard. * tmp file * -p * WIP: shared proxy setup * chmod * maybe? * ?? * bash * debug * debug * maybe * more bash * simplify usage * cleanup * actually use the new setting * remove old setting * attempt formatting * add mocha colour * at least use something * code review comments * syntax * syntax * fix usage --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 0748d2d commit fd6ec65

File tree

5 files changed

+113
-3
lines changed

5 files changed

+113
-3
lines changed

.github/config/nodejs-dev.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"auth",
101101
"batch",
102102
"cloud-language",
103+
"cloud-sql/mysql/mysql",
103104
"cloud-tasks/snippets",
104105
"cloud-tasks/tutorial-gcf/app",
105106
"cloud-tasks/tutorial-gcf/function",

.github/config/nodejs-prod.jsonc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
// TODO: fix these
7878
"ai-platform/snippets", // PERMISSION_DENIED: Permission denied: Consumer 'projects/undefined' has been suspended.
7979
"automl", // (untested) FAILED_PRECONDITION: Google Cloud AutoML Natural Language was retired on March 15, 2024. Please migrate to Vertex AI instead
80-
"cloud-sql/mysql/mysql", // (untested) Error: expected 200 "OK", got 500 "Internal Server Error"
8180
"cloud-sql/mysql/mysql2", // (untested) Error: Cannot find module './connect-connector-with-iam-authn.js'
8281
"cloud-sql/postgres/knex", // (untested) CloudSQLConnectorError: Malformed instance connection name provided: expected format of "PROJECT:REGION:INSTANCE", got undefined
8382
"cloud-sql/sqlserver/mssql", // (untested) TypeError: The "config.server" property is required and must be of type string.

.github/workflows/utils/sql-proxy.sh

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/bin/bash -ex
2+
3+
# Copyright 2025 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Shared Cloud SQL Proxy setup
18+
# Presumes the following variables in ci-setup.json:
19+
# * CLOUD_SQL_CONNECTION_NAME - the project:region:instance of a Cloud SQL instance.
20+
# * UNIX_SOCKET_DIR - a local directory to set the proxy to (default tmp/cloudsql)
21+
#
22+
# Note: in GitHub Actions environments, `/cloudsql` is not valid.
23+
# Ensure any INSTANCE_UNIX_SOCKET value is ~= $UNIX_SOCKET_DIR/$CLOUD_SQL_CONNECTION_NAME
24+
25+
usage() {
26+
cat << EOF
27+
# Usage:
28+
# CLOUD_SQL_CONNECTION_NAME=project:region:instance sql-proxy.sh [..]
29+
#
30+
# Defaults to TCP socket proxy. Set `SOCKET=unix` for Unix sockets.
31+
#
32+
# Usage in package.json:
33+
#
34+
# "proxy": "$GITHUB_WORKSPACE/.github/workflows/utils/sql-proxy.sh",
35+
# "system-test": "npm run proxy -- c8 mocha test/... ",
36+
# "system-test-unix": "SOCKET=unix npm run proxy -- c8 mocha test/... ",
37+
EOF
38+
}
39+
40+
41+
PROXY_VERSION="v2.15.1"
42+
SOCKET=${SOCKET:-tcp}
43+
44+
echop(){ # Print Echo
45+
echo "👾 $1"
46+
}
47+
48+
exit_message() { # Error Echo
49+
echo "$1"
50+
usage
51+
exit 1
52+
}
53+
54+
if [[ -z "$CLOUD_SQL_CONNECTION_NAME" ]]; then
55+
exit_message "Must provide CLOUD_SQL_CONNECTION_NAME"
56+
fi
57+
58+
if [[ $SOCKET == "unix" ]]; then
59+
UNIX_SOCKET_DIR=${UNIX_SOCKET_DIR:-"tmp/cloudsql"}
60+
61+
if [[ $UNIX_SOCKET_DIR == "/cloudsql" ]]; then
62+
exit_message "Cannot use /cloudsql in a GitHub Actions context"
63+
fi
64+
65+
mkdir -p $UNIX_SOCKET_DIR && chmod 777 $UNIX_SOCKET_DIR
66+
socket="--unix-socket $UNIX_SOCKET_DIR"
67+
fi
68+
echop "Setting up cloud-sql-proxy for $SOCKET socket connections"
69+
70+
# Download the Cloud SQL Auth Proxy (only once)
71+
if [[ ! -f cloud-sql-proxy ]]; then
72+
curl -o cloud-sql-proxy https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/${PROXY_VERSION}/cloud-sql-proxy.linux.amd64
73+
if [[ $? -ne 0 ]]; then
74+
echo "Failed to download cloud-sql-proxy"
75+
exit 1
76+
fi
77+
chmod +x cloud-sql-proxy
78+
else
79+
echo "cloud-sql-proxy already downloaded"
80+
fi
81+
82+
# Setup proxy
83+
./cloud-sql-proxy $socket $CLOUD_SQL_CONNECTION_NAME &
84+
sleep 5
85+
echop "Proxy ready for use"
86+
87+
# Run whatever command was passed to this script
88+
$@ || STATUS=$?
89+
90+
# Cleanup
91+
echop "Shutting down proxy process"
92+
pkill -f "cloud-sql-proxy" || echo "cloud-sql-proxy process not found. Was it already stopped?"
93+
94+
# Fail if the tests failed
95+
exit $STATUS

cloud-sql/mysql/mysql/ci-setup.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"env": {
3+
"INSTANCE_HOST": "127.0.0.1",
4+
"INSTANCE_CONNECTION_NAME": "nodejs-docs-samples-tests:us-central1:mysql-ci",
5+
"UNIX_SOCKET_DIR": "tmp/cloudsql",
6+
"CLOUD_SQL_CONNECTION_NAME": "$INSTANCE_CONNECTION_NAME",
7+
"INSTANCE_UNIX_SOCKET": "$UNIX_SOCKET_DIR/$INSTANCE_CONNECTION_NAME",
8+
"DB_NAME": "kokoro_ci",
9+
"DB_USER": "kokoro_ci"
10+
},
11+
"secrets": {
12+
"DB_PASS": "nodejs-docs-samples-tests/nodejs-docs-samples-sql-password"
13+
}
14+
}

cloud-sql/mysql/mysql/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
},
1414
"scripts": {
1515
"start": "node server/server.js",
16-
"system-test": "c8 mocha -p -j 2 test/server.test.js --timeout=60000 --exit",
17-
"system-test-unix": "c8 mocha -p -j 2 test/server-unix.test.js --timeout=60000 --exit",
16+
"proxy": "$GITHUB_WORKSPACE/.github/workflows/utils/sql-proxy.sh",
17+
"system-test": "npm run proxy -- c8 mocha -p -j 2 test/server.test.js --colors --timeout=60000 --exit",
18+
"system-test-unix": "SOCKET=unix npm run proxy -- c8 mocha -p -j 2 test/server-unix.test.js --colors --timeout=60000 --exit",
1819
"test": "npm run system-test && npm run system-test-unix"
1920
},
2021
"dependencies": {

0 commit comments

Comments
 (0)