Skip to content

Commit

Permalink
Reset db
Browse files Browse the repository at this point in the history
  • Loading branch information
JargeZ committed Oct 25, 2024
1 parent b6eb54e commit 2045310
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ inputs:
remote_only:
description: Add --remote-only flag to flyctl deploy command to use remote build. Improve build speed (default false)
default: false
reset_db:
description: Reset the database on each deploy (default false)
default: false
build_args:
description: Optional Docker --build-arg
build_secrets:
Expand Down
25 changes: 17 additions & 8 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ config="${INPUT_CONFIG:-fly.toml}"
build_args=""
build_secrets=""
runtime_environment=""
database_name="${app/-/_}_pg"
database_role="${app/-/_}_pg"
database_name="$(echo "$app" | sed 's/-/_/g')_pg"
database_role="$(echo "$app" | sed 's/-/_/g')_pg"

if ! echo "$app" | grep "$PR_NUMBER"; then
if [ "$INPUT_ALLOW_UNSAFE_NAME" != "true" ]; then
Expand All @@ -38,18 +38,21 @@ if ! echo "$app" | grep "$PR_NUMBER"; then
echo "WARNING: The app's name does not contain the PR number. it is recommended to include the PR number in the app's name."
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
drop_db() {
flyctl postgres connect --app "$INPUT_POSTGRES" <<EOF || true
drop database ${database_name} with (force );
drop role ${database_role};
\q
EOF
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
drop_db
fi
exit 0
fi

Expand Down Expand Up @@ -94,6 +97,11 @@ if [ -n "$INPUT_SECRETS" ]; then
echo $INPUT_SECRETS | tr " " "\n" | flyctl secrets import --app "$app"
fi


if [ -n "$INPUT_RESET_DB" ] && [ -n "$INPUT_POSTGRES" ]; then
drop_db || true
fi

# Attach postgres cluster to the app if specified.
if [ -n "$INPUT_POSTGRES" ]; then
pg_status=$(flyctl postgres list | grep "$INPUT_POSTGRES" | awk '{print $3}')
Expand Down Expand Up @@ -131,6 +139,7 @@ if [ -n "$INPUT_POSTGRES" ]; then
flyctl postgres attach "$INPUT_POSTGRES" --app "$app" --database-name "$database_name" --database-user "$database_role" || true
fi


# Use remote builders
if [ -n "$INPUT_REMOTE_ONLY" ]; then
remote_only="--remote-only"
Expand Down

0 comments on commit 2045310

Please sign in to comment.