From d8d1202b5e78eeda501c32505aafe47a29aa2d43 Mon Sep 17 00:00:00 2001 From: Don Cote Date: Fri, 2 Sep 2022 14:05:14 -0400 Subject: [PATCH] handle secrets and db startup --- README.md | 1 + action.yml | 5 +++++ entrypoint.sh | 52 +++++++++++++++++++++------------------------------ 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index d4d3d05..15c2358 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/action.yml b/action.yml index de2c36d..7cd369f 100644 --- a/action.yml +++ b/action.yml @@ -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. + \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh index cda39f2..fdddf97 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,43 +2,23 @@ set -ex -if [ -n "$INPUT_PATH" ]; then - # Allow user to change directories in which to run Fly commands. - cd "$INPUT_PATH" || exit -fi - PR_NUMBER=$(jq -r .number /github/workflow/event.json) if [ -z "$PR_NUMBER" ]; then echo "This action only supports pull_request actions." 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 +26,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.