Skip to content

Commit

Permalink
App create and database drop
Browse files Browse the repository at this point in the history
  • Loading branch information
JargeZ committed Sep 4, 2024
1 parent 4d6566e commit f47aaa9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ If you have an existing `fly.toml` in your repo, this action will copy it with a
| `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_clean_on_close` | Drop database and role on Pull Request close event (default false) |
| `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. |
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ 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_clean_on_close:
description: Drop database and role on Pull Request close event (default false)
default: false
secrets:
description: Secrets to be set on the app. Separate multiple secrets with a space
vmsize:
Expand Down
34 changes: 27 additions & 7 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ config="${INPUT_CONFIG:-fly.toml}"
build_args=""
build_secrets=""
runtime_environment=""
database_name="${app/-/_}_pg"
database_role="${app/-/_}_pg"

if ! echo "$app" | grep "$PR_NUMBER"; then
if [ "$INPUT_ALLOW_UNSAFE_NAME" != "true" ]; then
Expand All @@ -39,6 +41,15 @@ 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 [ "$INPUT_POSTGRES_CLEAN_ON_CLOSE" == "true" && -n "$INPUT_POSTGRES" ]; then
flyctl postgres connect --app "$INPUT_POSTGRES" <<EOF || true
drop database ${database_name} with (force );
drop role ${database_role};
\q
EOF
fi

exit 0
fi

Expand All @@ -63,20 +74,29 @@ if [ -n "$INPUT_ENVIRONMENT" ]; then
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
cp "$config" "$config.bak"
flyctl launch --no-deploy --copy-config --name "$app" --image "$image" --region "$region" --org "$org" ${build_args} ${build_secrets} ${runtime_environment}
# Restore the original config file
cp "$config.bak" "$config"
if [ ! flyctl status --app "$app" ]; then
if [ -n "$INPUT_CONFIG" ]; then
# Config specified explicitly, create empty app
flyctl app create --name "$app" --org "$org"
else
# Default behavior: launch the app inplace from default config
# TODO: https://github.com/superfly/fly-pr-review-apps/issues/49
# Probably dont need to use launch for preview at all or adjust to launch only (handle redis/postgres deletion on destroy)

# Backup the original config file since 'flyctl launch' messes up the [build.args] section
cp "$config" "$config.bak"
flyctl launch --no-deploy --copy-config --name "$app" --image "$image" --region "$region" --org "$org" ${build_args} ${build_secrets} ${runtime_environment}
# Restore the original config file
cp "$config.bak" "$config"
fi
fi
if [ -n "$INPUT_SECRETS" ]; then
echo $INPUT_SECRETS | tr " " "\n" | flyctl secrets import --app "$app"
fi

# Attach postgres cluster to the app if specified.
if [ -n "$INPUT_POSTGRES" ]; then
flyctl postgres attach "$INPUT_POSTGRES" --app "$app" || true
flyctl postgres attach "$INPUT_POSTGRES" --app "$app" --database-name "$database_name" --database-user "$database_role" || true
fi

# Use remote builders
Expand Down

0 comments on commit f47aaa9

Please sign in to comment.