handle secrets and db startup

This commit is contained in:
Don Cote 2022-09-02 14:05:14 -04:00
parent c5462a4e38
commit 4520dabe79
3 changed files with 29 additions and 30 deletions

View File

@ -16,6 +16,7 @@ If you have an existing `fly.toml` in your repo, this action will copy it with a
| `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. |
| `update` | Whether or not to update this Fly app when the PR is updated. Default `true`. |
| `config` | Optional path to a custom Fly toml config. Config path should be relative to `path` parameter, if specified.
## Required Secrets

View File

@ -23,3 +23,8 @@ inputs:
update:
description: Whether new commits to the PR should re-deploy the Fly app
default: true
secrets:
description: Secrets to be set on the app. Separate multiple secrets with a space.
config:
description: Optional path to a custom Fly toml config. Config path should be relative to path parameter, if specified.

View File

@ -2,10 +2,8 @@
set -ex
if [ -n "$INPUT_PATH" ]; then
# Allow user to change directories in which to run Fly commands.
cd "$INPUT_PATH" || exit
fi
pwd
ls -la
PR_NUMBER=$(jq -r .number /github/workflow/event.json)
if [ -z "$PR_NUMBER" ]; then
@ -13,32 +11,17 @@ if [ -z "$PR_NUMBER" ]; then
exit 1
fi
KEYS=$(jq 'keys' /github/workflow/event.json)
SENDER=$(jq -r .sender.name /github/workflow/event.json)
USER=$(jq -r .pull_request.user.login /github/workflow/event.json)
REPO_NAME=$(jq -r .repository.name /github/workflow/event.json)
PR_TITLE=$(jq -r .pull_request.head.ref /github/workflow/event.json)
EVENT_TYPE=$(jq -r .action /github/workflow/event.json)
FILES=$(ls -la /github/workflow)
echo "FILES: $FILES"
echo "KEYS: $KEYS"
echo "PR_NUMBER: $PR_NUMBER"
echo "REPO_OWNER: $REPO_OWNER"
echo "REPO_NAME: $SENDER"
echo "EVENT_TYPE: $EVENT_TYPE"
EVENT_JSON=$(jq -r . /github/workflow/event.json)
# Default the Fly app name to pr-{number}-{repo_owner}-{repo_name}
app="${INPUT_NAME:-pr-$PR_NUMBER-$REPO_OWNER-$REPO_NAME}"
# Default the Fly app name to pr-{repo}-{title}-{number}-{user}
app="${INPUT_NAME:-pr-$REPO_NAME-$PR_TITLE-$PR_NUMBER-$USER}"
region="${INPUT_REGION:-${FLY_REGION:-iad}}"
org="${INPUT_ORG:-${FLY_ORG:-personal}}"
image="$INPUT_IMAGE"
echo "$app"
echo "$region"
echo "$org"
echo "$image"
if ! echo "$app" | grep "$PR_NUMBER"; then
echo "For safety, this action requires the app's name to contain the PR number."
exit 1
@ -46,22 +29,32 @@ fi
# PR was closed - remove the Fly app if one exists and exit.
if [ "$EVENT_TYPE" = "closed" ]; then
echo "PR was closed - removing the Fly app."
flyctl apps destroy "$app" -y || true
exit 0
fi
exit 0
# Deploy the Fly app, creating it first if needed.
# Create the app
if ! flyctl status --app "$app"; then
flyctl launch --now --copy-config --name "$app" --image "$image" --region "$region" --org "$org"
elif [ "$INPUT_UPDATE" != "false" ]; then
flyctl deploy --app "$app" --region "$region" --image "$image" --region "$region" --strategy immediate
echo "Creating the Fly app."
flyctl apps create --name "$app" --org "$org"
flyctl scale memory 1024 --app "$app
fi
# Attach postgres cluster to the app if specified.
if [ -n "$INPUT_POSTGRES" ]; then
flyctl postgres attach --postgres-app "$INPUT_POSTGRES" || true
echo "Attaching postgres cluster."
flyctl postgres attach "$INPUT_POSTGRES" --app "$app" --database-name "$app"-db || true
fi
if [ -n "$INPUT_SECRETS" ]; then
echo "Setting secrets."
echo $INPUT_SECRETS | tr " " "\n" | flyctl secrets import --app "$app"
fi
if [ "$INPUT_UPDATE" != "false" ]; then
echo "Deploying the app."
flyctl deploy --config "$INPUT_CONFIG" --app "$app" --region "$region" --strategy immediate --image "$image" --remote-only
fi
# Make some info available to the GitHub workflow.