Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
mattes committed Sep 22, 2020
1 parent 3d6d415 commit 3a5d88a
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
33 changes: 32 additions & 1 deletion README.md
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
```

69 changes: 69 additions & 0 deletions action.yml
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

0 comments on commit 3a5d88a

Please sign in to comment.