From ec2f75da952532c9c42d34e64f55fac64e7865a7 Mon Sep 17 00:00:00 2001 From: Jason Pollentier Date: Thu, 25 Jan 2024 09:31:08 -0600 Subject: [PATCH] WIP: add a way to create the postgres cluster if it doesn't exist --- README.md | 3 ++- action.yml | 2 ++ entrypoint.sh | 11 +++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 04f6d4e..67e884d 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,8 @@ If you have an existing `fly.toml` in your repo, this action will copy it with a | `region` | Which Fly region to run the app in. Alternatively, set the env `FLY_REGION`. Defaults to `iad`. | | `org` | Which Fly organization to launch the app under. Alternatively, set the env `FLY_ORG`. Defaults to `personal`. | | `path` | Path to run the `flyctl` commands from. Useful if you have an existing `fly.toml` in a subdirectory. | -| `postgres` | Optional name of an existing Postgres cluster to `flyctl postgres attach` to. | +| `postgres` | Optional name of a new or existing Postgres cluster to `flyctl postgres attach` to. | +| `postgres_standalone` | If "true", the Postgres cluster specified by `postgres` will be created as necessary and deleted during teardown. | | `update` | Whether or not to update this Fly app when the PR is updated. Default `true`. | | `secrets` | Secrets to be set on the app. Separate multiple secrets with a space | | `vmsize` | Set app VM to a named size, eg. shared-cpu-1x, dedicated-cpu-1x, dedicated-cpu-2x etc. Takes precedence over cpu, cpu kind, and memory inputs. | diff --git a/action.yml b/action.yml index 0748d75..76a688c 100644 --- a/action.yml +++ b/action.yml @@ -22,6 +22,8 @@ inputs: description: path to a directory containing a fly.toml to clone postgres: description: Optionally attach the app to a pre-existing Postgres cluster on Fly + postgres_standalone: + description: If true, create the postgres cluster specified by the 'postgres' input, and destroy it on teardown secrets: description: Secrets to be set on the app. Separate multiple secrets with a space vmsize: diff --git a/entrypoint.sh b/entrypoint.sh index 1ec9f5c..8a00127 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -33,9 +33,20 @@ fi # PR was closed - remove the Fly app if one exists and exit. if [ "$EVENT_TYPE" = "closed" ]; then flyctl apps destroy "$app" -y || true + if [ -n "$INPUT_POSTGRES" && "$INPUT_POSTGRES_STANDALONE" = "true" ]; then + flyctl apps destroy "$INPUT_POSTGRES" -y || true + fi exit 0 fi +# Create postgres app if necessary +# TODO: do we want the postgres app specs to be configurable? +if [ -n "$INPUT_POSTGRES" && "$INPUT_POSTGRES_STANDALONE" = "true" ]; then + if ! flyctl status --app "$INPUT_POSTGRES"; then + flyctl postgres create --name "$INPUT_POSTGRES" --region "$region" --org "$org" --vm-size shared-cpu-1x --volume-size 1 --initial-cluster-size 1 || true + fi +fi + # Deploy the Fly app, creating it first if needed. if ! flyctl status --app "$app"; then # Backup the original config file since 'flyctl launch' messes up the [build.args] section