forked from mattes/gce-cloudsql-proxy-action
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,32 @@ | ||
# gce-cloudsql-proxy-action | ||
# Google Cloud SQL Proxy | ||
|
||
Github action which will start a [Google Cloud SQL Proxy](https://cloud.google.com/sql/docs/postgres/sql-proxy) as Docker container. | ||
|
||
## Prerequisites | ||
|
||
Set up the following resources manually in the Cloud Console | ||
or use a tool like [Terraform](https://www.terraform.io). | ||
|
||
* Running Cloud SQL instance with a public IP address | ||
* Create Service Account with Role `Cloud SQL Client` and export a new JSON key. | ||
|
||
|
||
## Github Action Inputs | ||
|
||
| Variable | Description | | ||
|----------------------------------|-----------------------------------------------------------------------------| | ||
| `creds` | ***Required*** Service Account JSON Key (not base64 encoded) | | ||
| `instance` | ***Required*** Cloud SQL connection name | | ||
| `port` | Listen on port, default 5432 | | ||
| `proxy_version` | Cloud SQL Proxy version, default 1.18.0 | | ||
|
||
|
||
## Example Usage | ||
|
||
``` | ||
uses: mattes/gce-cloudsql-proxy-action@v1 | ||
with: | ||
creds: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }} | ||
instance: my-project:us-central1:instance-1 | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
name: "Google Cloud SQL Proxy" | ||
description: "Start Google Cloud SQL Proxy" | ||
|
||
branding: | ||
icon: "database" | ||
color: "red" | ||
|
||
inputs: | ||
creds: | ||
description: "Contents of a Service Account JSON Key" | ||
required: true | ||
instance: | ||
description: "CloudSQL instance" | ||
required: true | ||
port: | ||
description: "Listen on port" | ||
required: false | ||
default: 5432 | ||
proxy_version: | ||
description: "CloudSQL Proxy Version" | ||
required: false | ||
default: 1.18.0 | ||
|
||
runs: | ||
using: "composite" | ||
steps: | ||
- name: Start Google Cloud SQL Proxy | ||
shell: bash | ||
run: | | ||
# fail if pg_isready is not installed | ||
if ! command -v pg_isready &>/dev/null; then | ||
echo "pg_isready is not installed" | ||
exit 5 | ||
fi | ||
# write google application credentials to a temporary file to be used inside the container | ||
mkdir -p /tmp/gce-cloudsql-proxy | ||
echo '${{ inputs.creds }}' > /tmp/gce-cloudsql-proxy/key.json | ||
# start container | ||
docker run -d --net host --name gce-cloudsql-proxy --restart on-failure \ | ||
-v /tmp/gce-cloudsql-proxy:/tmp/gce-cloudsql-proxy \ | ||
gcr.io/cloudsql-docker/gce-proxy:${{ inputs.proxy_version }} \ | ||
/cloud_sql_proxy \ | ||
-credential_file /tmp/gce-cloudsql-proxy/key.json \ | ||
-dir /tmp \ | ||
-instances=${{ inputs.instance }}=tcp:127.0.0.1:${{ inputs.port }} | ||
# wait until connections are accepted | ||
sleep 3 | ||
isready=0 | ||
for i in {1..10}; do | ||
echo "Wait for connections to be ready ... $i/10" | ||
(pg_isready --quiet --timeout 3 --host localhost --port ${{ inputs.port }} || exit $?) && true # escape bash's pipefail | ||
isready=$? | ||
if [[ $isready -eq 0 ]]; then | ||
break | ||
fi | ||
sleep 2 | ||
done | ||
# print container logs | ||
docker logs gce-cloudsql-proxy | ||
# exit with error code if we couldn't connect | ||
if [[ $isready -ne 0 ]]; then | ||
exit $isready | ||
fi | ||