forked from mattes/gce-cloudsql-proxy-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
86 lines (74 loc) · 2.74 KB
/
action.yml
File metadata and controls
86 lines (74 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
name: "Google Cloud SQL Proxy"
description: "Start Google CloudSQL Proxy"
branding:
icon: "database"
color: "purple"
inputs:
creds:
description: "Contents of a Service Account JSON Key"
required: true
instances:
description: "CloudSQL instances (comma-separated)"
required: true
ports:
description: "Listen ports (comma-separated)"
required: false
default: "5432"
proxy_version:
description: "CloudSQL Proxy Version"
required: false
default: "1.21.0"
runs:
using: "composite"
steps:
- name: Start Google Cloud SQL Proxy
id: proxy
shell: bash
run: |
instances=(${{ inputs.instances }})
ports=(${{ inputs.ports }})
proxy_version="${{ inputs.proxy_version }}"
# Split comma-separated inputs into arrays
IFS=',' read -ra instances <<< "$instances"
IFS=',' read -ra ports <<< "$ports"
# Check if the number of instances and ports match
if [[ ${#instances[@]} -ne ${#ports[@]} ]]; then
echo "Error: Number of instances and ports should match."
exit 1
fi
# Start Google Cloud SQL Proxy for each instance
for i in "${!instances[@]}"; do
instance="${instances[$i]}"
port="${ports[$i]}"
# write google application credentials to a temporary file to be used inside the container
mkdir -p $HOME/gce-cloudsql-proxy/$i
echo '${{ inputs.creds }}' > $HOME/gce-cloudsql-proxy/$i/key.json
# start container
docker run -d --net host --name gce-cloudsql-proxy-$i --restart on-failure \
-v $HOME/gce-cloudsql-proxy/$i:/tmp/gce-cloudsql-proxy \
gcr.io/cloudsql-docker/gce-proxy:$proxy_version \
/cloud_sql_proxy \
-credential_file /tmp/gce-cloudsql-proxy/key.json \
-dir /tmp \
-instances=$instance=tcp:127.0.0.1:$port
# wait until connections are accepted
sleep 3
isready=0
for j in {1..10}; do
echo "Wait for connections to be ready for instance $instance on port $port ... $j/10"
(${{ github.action_path }}/wait-for-it.sh --quiet --timeout=3 --host=127.0.0.1 --port=$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-$i
# exit with error code if we couldn't connect
if [[ $isready -ne 0 ]]; then
exit $isready
fi
done
# Set the output to be used by subsequent steps
echo "::set-output name=proxy_ids::$(docker ps -aq --filter 'name=gce-cloudsql-proxy-'*)"