fly-pr-review-apps/entrypoint.sh

67 lines
2.1 KiB
Bash
Raw Normal View History

2019-09-19 20:50:53 +00:00
#!/bin/sh -l
2022-01-28 04:52:23 +00:00
set -ex
2022-01-29 16:52:27 +00:00
PR_NUMBER=$(jq -r .number /github/workflow/event.json)
2022-01-29 18:42:29 +00:00
if [ -z "$PR_NUMBER" ]; then
echo "This action only supports pull_request actions."
exit 1
fi
2022-09-02 18:05:14 +00:00
USER=$(jq -r .pull_request.user.login /github/workflow/event.json)
2022-09-02 14:34:37 +00:00
REPO_NAME=$(jq -r .repository.name /github/workflow/event.json)
2022-09-02 18:05:14 +00:00
PR_TITLE=$(jq -r .pull_request.head.ref /github/workflow/event.json)
2022-01-28 04:52:23 +00:00
EVENT_TYPE=$(jq -r .action /github/workflow/event.json)
2022-09-02 14:34:37 +00:00
2022-09-02 18:05:14 +00:00
# Default the Fly app name to pr-{repo}-{title}-{number}-{user}
app="${INPUT_NAME:-pr-$REPO_NAME-$PR_TITLE-$PR_NUMBER-$USER}"
2022-09-12 18:41:53 +00:00
app=$(echo $app | tr '[:upper:]' '[:lower:]')
2022-01-28 04:52:23 +00:00
region="${INPUT_REGION:-${FLY_REGION:-iad}}"
org="${INPUT_ORG:-${FLY_ORG:-personal}}"
2022-01-28 23:33:38 +00:00
image="$INPUT_IMAGE"
2022-01-28 03:26:45 +00:00
2022-01-29 18:42:29 +00:00
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
2022-01-29 16:52:27 +00:00
2022-01-29 18:42:29 +00:00
# PR was closed - remove the Fly app if one exists and exit.
if [ "$EVENT_TYPE" = "closed" ]; then
2022-09-02 18:05:14 +00:00
echo "PR was closed - removing the Fly app."
2022-01-29 18:42:29 +00:00
flyctl apps destroy "$app" -y || true
exit 0
fi
2022-01-28 03:26:45 +00:00
2022-09-02 18:05:14 +00:00
# Create the app
2022-01-29 18:42:29 +00:00
if ! flyctl status --app "$app"; then
2022-09-02 18:05:14 +00:00
echo "Creating the Fly app."
flyctl apps create --name "$app" --org "$org"
flyctl scale memory 1024 --app "$app"
2022-01-28 23:33:38 +00:00
fi
2022-01-28 04:52:23 +00:00
2022-01-29 18:42:29 +00:00
# Attach postgres cluster to the app if specified.
if [ -n "$INPUT_POSTGRES" ]; then
2022-09-02 18:05:14 +00:00
echo "Attaching postgres cluster."
flyctl postgres attach "$INPUT_POSTGRES" --app "$app" --database-name "$app"-db || true
fi
2022-09-12 18:29:47 +00:00
# Set up secrets
2022-09-02 18:05:14 +00:00
if [ -n "$INPUT_SECRETS" ]; then
echo "Setting secrets."
echo $INPUT_SECRETS | tr " " "\n" | flyctl secrets import --app "$app"
fi
2022-09-12 18:29:47 +00:00
# Deploy the app
2022-09-02 18:05:14 +00:00
if [ "$INPUT_UPDATE" != "false" ]; then
echo "Deploying the app."
flyctl deploy --config "$INPUT_CONFIG" --app "$app" --region "$region" --strategy immediate --image "$image" --remote-only
2022-01-28 23:33:38 +00:00
fi
2022-01-28 04:52:23 +00:00
2022-01-29 18:42:29 +00:00
# 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"