This commit is contained in:
Brent Dillingham 2022-01-29 13:42:29 -05:00
parent dc9e76ff34
commit 9b33d2c2aa

View File

@ -8,45 +8,48 @@ if [ -n "$INPUT_PATH" ]; then
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
REPO_OWNER=$(jq -r .event.base.repo.owner /github/workflow/event.json)
REPO_NAME=$(jq -r .event.base.repo.name /github/workflow/event.json)
EVENT_TYPE=$(jq -r .action /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}"
region="${INPUT_REGION:-${FLY_REGION:-iad}}"
org="${INPUT_ORG:-${FLY_ORG:-personal}}"
image="$INPUT_IMAGE"
deployed=$(fly status --app "$app" --json | jq -r .Deployed) || true
# PR was opened or reopened, or the Fly app hasn't been successfully created yet.
if [ "$deployed" != "true" ] || [ "$EVENT_TYPE" = "opened" ] || [ "$EVENT_TYPE" = "reopened" ]; then
# Create the Fly app.
flyctl launch --now --copy-config --name "$app" --image "$image" --region "$region" --org "$org"
# Attach postgres cluster to the new app if needed.
if [ -n "$INPUT_POSTGRES" ]; then
flyctl postgres attach --postgres-app "$INPUT_POSTGRES"
fi
# New commits were added to the PR, and this is an app we want to re-deploy when the PR is updated.
elif [ "$EVENT_TYPE" = "synchronize" ] && [ "$INPUT_UPDATE" != "false" ]; then
# Deploy the Fly app.
flyctl deploy --app "$app"
if ! echo "$app" | grep "$PR_NUMBER"; then
echo "For safety, this action requires the app's name to contain the PR number."
exit 1
fi
# Output the app URL to make it available to the GitHub workflow.
hostname=$(fly status --app "$app" --json | jq -r .Hostname) || true
if [ -n "$hostname" ]; then
echo "::set-output name=url::https://$hostname"
fi
# PR was closed - remove the Fly app.
# PR was closed - remove the Fly app if one exists and exit.
if [ "$EVENT_TYPE" = "closed" ]; then
flyctl apps destroy "$app" -y
flyctl apps destroy "$app" -y || true
exit 0
fi
# Deploy the Fly app, creating it first if needed.
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
fi
# Attach postgres cluster to the app if specified.
if [ -n "$INPUT_POSTGRES" ]; then
flyctl postgres attach --postgres-app "$INPUT_POSTGRES" || true
fi
# Make some info available to the GitHub workflow.
fly status --app "$app" --json >status.json
hostname=$(jq -r .Hostname status.json)
appid=$(jq -r .ID status.json)
echo "::set-output name=hostname::$hostname"
echo "::set-output name=url::https://$hostname"
echo "::set-output name=id::$appid"