-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathentrypoint.sh
More file actions
executable file
·124 lines (112 loc) · 3.9 KB
/
entrypoint.sh
File metadata and controls
executable file
·124 lines (112 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/ash -l
# shellcheck shell=dash
set -e
if [ -n "$INPUT_PATH" ]; then
# Allow user to change directories in which to run Fly commands.
cd "$INPUT_PATH" || exit
fi
# Support both pull_request (.number) and issue_comment (.issue.number) events
PR_NUMBER=$(jq -r '.number // .issue.number' /github/workflow/event.json)
if [ -z "$PR_NUMBER" ] || [ "$PR_NUMBER" = "null" ]; then
echo "Could not determine PR number. This action supports pull_request and issue_comment events."
exit 1
fi
GITHUB_REPOSITORY_NAME=${GITHUB_REPOSITORY#"$GITHUB_REPOSITORY_OWNER"/}
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-$GITHUB_REPOSITORY_OWNER-$GITHUB_REPOSITORY_NAME}"
# Change underscores to hyphens.
# shellcheck disable=SC3060
app="${app//_/-}"
# Change to lower case
app=$(echo "$app" | tr '[:upper:]' '[:lower:]')
region="${INPUT_REGION:-${FLY_REGION:-iad}}"
org="${INPUT_ORG:-${FLY_ORG:-personal}}"
image="$INPUT_IMAGE"
config="${INPUT_CONFIG:-fly.toml}"
if ! printf '%s' "$app" | grep -q "$PR_NUMBER"; then
echo "For safety, this action requires the app's name to contain the PR number."
exit 1
fi
# PR was closed - remove the Fly app if one exists and exit.
if [ "$EVENT_TYPE" = "closed" ]; then
flyctl apps destroy "$app" -y || true
exit 0
fi
# Deploy the Fly app, creating it first if needed.
if ! flyctl status --app "$app"; then
# Backup the original config file since 'flyctl launch' messes up the [build.args] section
cp "$config" "$config.bak"
set -- flyctl launch --no-deploy --copy-config --name "$app" --image "$image" --region "$region" --org "$org"
if [ -n "$INPUT_BUILD_ARGS" ]; then
for ARG in $INPUT_BUILD_ARGS; do
set -- "$@" --build-arg "$ARG"
done
fi
if [ -n "$INPUT_BUILD_SECRETS" ]; then
for ARG in $INPUT_BUILD_SECRETS; do
set -- "$@" --build-secret "$ARG"
done
fi
if [ -n "$INPUT_LAUNCH_OPTIONS" ]; then
# shellcheck disable=SC2086
set -- "$@" $INPUT_LAUNCH_OPTIONS
fi
"$@"
# Restore the original config file
cp "$config.bak" "$config"
fi
if [ -n "$INPUT_SECRETS" ]; then
printf '%s\n' "$INPUT_SECRETS" | tr " " "\n" | flyctl secrets import --app "$app"
fi
# Attach postgres cluster to the app if specified.
if [ -n "$INPUT_POSTGRES" ]; then
flyctl postgres attach "$INPUT_POSTGRES" --app "$app" || true
fi
# Trigger the deploy of the new version.
echo "Contents of config $config file: " && cat "$config"
if [ -n "$INPUT_VMSIZE" ]; then
set -- flyctl deploy --config "$config" --app "$app" --regions "$region" --image "$image" --strategy immediate --ha="$INPUT_HA" --vm-size "$INPUT_VMSIZE"
if [ -n "$INPUT_BUILD_ARGS" ]; then
for ARG in $INPUT_BUILD_ARGS; do
set -- "$@" --build-arg "$ARG"
done
fi
if [ -n "$INPUT_BUILD_SECRETS" ]; then
for ARG in $INPUT_BUILD_SECRETS; do
set -- "$@" --build-secret "$ARG"
done
fi
if [ -n "$INPUT_DEPLOY_OPTIONS" ]; then
# shellcheck disable=SC2086
set -- "$@" $INPUT_DEPLOY_OPTIONS
fi
"$@"
else
set -- flyctl deploy --config "$config" --app "$app" --regions "$region" --image "$image" --strategy immediate --ha="$INPUT_HA" --vm-cpu-kind "$INPUT_CPUKIND" --vm-cpus "$INPUT_CPU" --vm-memory "$INPUT_MEMORY"
if [ -n "$INPUT_BUILD_ARGS" ]; then
for ARG in $INPUT_BUILD_ARGS; do
set -- "$@" --build-arg "$ARG"
done
fi
if [ -n "$INPUT_BUILD_SECRETS" ]; then
for ARG in $INPUT_BUILD_SECRETS; do
set -- "$@" --build-secret "$ARG"
done
fi
if [ -n "$INPUT_DEPLOY_OPTIONS" ]; then
# shellcheck disable=SC2086
set -- "$@" $INPUT_DEPLOY_OPTIONS
fi
"$@"
fi
# Make some info available to the GitHub workflow.
flyctl status --app "$app" --json >status.json
hostname=$(jq -r .Hostname status.json)
appid=$(jq -r .ID status.json)
{
echo "hostname=$hostname"
echo "url=https://$hostname"
echo "id=$appid"
echo "name=$app"
} >>"$GITHUB_OUTPUT"