forked from guness/bitrise-step-firebase-app-distribution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step.sh
executable file
·260 lines (215 loc) · 6.96 KB
/
step.sh
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/bin/bash
set -e
THIS_SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
#=======================================
# Functions
#=======================================
RESTORE='\033[0m'
RED='\033[00;31m'
YELLOW='\033[00;33m'
BLUE='\033[00;34m'
GREEN='\033[00;32m'
function color_echo {
color=$1
msg=$2
echo -e "${color}${msg}${RESTORE}"
}
function echo_fail {
msg=$1
echo
color_echo "${RED}" "${msg}"
exit 1
}
function echo_warn {
msg=$1
color_echo "${YELLOW}" "${msg}"
}
function echo_info {
msg=$1
echo
color_echo "${BLUE}" "${msg}"
}
function echo_details {
msg=$1
echo " ${msg}"
}
function echo_done {
msg=$1
color_echo "${GREEN}" " ${msg}"
}
function validate_required_input {
key=$1
value=$2
if [ -z "${value}" ] ; then
echo_fail "Missing required input: ${key}"
fi
}
function escape {
token=$1
quoted=$(echo "${token}" | sed -e 's/\"/\\"/g' )
echo "${quoted}"
}
function validate_required_input_with_options {
key=$1
value=$2
options=$3
validate_required_input "${key}" "${value}"
found="0"
for option in "${options[@]}" ; do
if [ "${option}" == "${value}" ] ; then
found="1"
fi
done
if [ "${found}" == "0" ] ; then
echo_fail "Invalid input: (${key}) value: (${value}), valid options: ($( IFS=$", "; echo "${options[*]}" ))"
fi
}
#=======================================
# Additional functions
#=======================================
function truncate_release_notes {
notes=$1
max_length=$2
original_length=${#notes}
if (( $original_length > $max_length )); then
end_message="..."
cut_limit=$(($max_length-${#end_message}))
echo "${notes:0:$cut_limit}${end_message}"
else
echo "${notes}"
fi
}
#=======================================
# Main
#=======================================
#
# Validate parameters
echo_info "Configs:"
echo_details "* firebase_token: $firebase_token"
echo_details "* service_credentials_file: $service_credentials_file"
echo_details "* app_path: $app_path"
echo_details "* app: $app"
echo_details "* release_notes: $release_notes"
echo_details "* release_notes_length: $release_notes_length"
echo_details "* release_notes_file: $release_notes_file"
echo_details "* testers: $testers"
echo_details "* groups: $groups"
echo_details "* flags: $flags"
echo_details "* is_debug: $is_debug"
echo_details "* upgrade_firebase_tools: $upgrade_firebase_tools"
echo
# Export Service Credentials File
if [ -n "${service_credentials_file}" ] ; then
export GOOGLE_APPLICATION_CREDENTIALS="${service_credentials_file}"
fi
if [ -z "${app_path}" ] ; then
echo_fail "App path for APK, AAB or IPA is not defined"
fi
case "${app_path}" in
\|\|*)
echo_warn "App path starts with || . Manually fixing path: ${app_path}"
app_path="${app_path:2}"
;;
*\|\|)
echo_warn "App path ends with || . Manually fixing path: ${app_path}"
app_path="${app_path%??}"
;;
\|*\|)
echo_warn "App path starts and ends with | . Manually fixing path: ${app_path}"
app_path="${app_path:1}"
app_path="${app_path%?}"
;;
*\|*)
echo_fail "App path contains | . You need to make sure only one build path is set: ${app_path}"
;;
*)
echo_info "App path contains a file, great!! 👍"
;;
esac
if [ ! -f "${app_path}" ] ; then
echo_fail "App path defined but the file does not exist at path: ${app_path}"
fi
if [ -n "${FIREBASE_TOKEN}" ] && [ -z "${FIREBASE_TOKEN}" ] ; then
echo_warn "FIREBASE_TOKEN is defined but empty. This may cause a problem with the binary."
fi
if [ -z "${firebase_token}" ] ; then
if [ -z "${service_credentials_file}" ]; then
echo_fail "No authentication input was defined, please fill one of Firebase Token or Service Credentials Field."
elif [ ! -f "${service_credentials_file}" ]; then
if [[ $service_credentials_file == http* ]]; then
echo_info "Service Credentials File is a remote url, downloading it ..."
curl $service_credentials_file --output credentials.json
service_credentials_file=$(pwd)/credentials.json
export GOOGLE_APPLICATION_CREDENTIALS="${service_credentials_file}"
echo_info "Downloaded Service Credentials File to path: ${service_credentials_file}"
else
echo_fail "Service Credentials File defined but does not exist at path: ${service_credentials_file}"
fi
fi
fi
if [ -n "${FIREBASE_TOKEN}" ] && [ -n "${service_credentials_file}" ]; then
echo_warn "Both authentication methods are defined: Firebase Token (via FIREBASE_TOKEN environment variable) and Service Credentials Field, one is enough."
fi
if [ -n "${firebase_token}" ] && [ -n "${service_credentials_file}" ]; then
echo_warn "Both authentication inputs are defined: Firebase Token and Service Credentials Field, one is enough."
fi
if [ -z "${app}" ] ; then
echo_fail "Firebase App ID is not defined"
fi
if [ -n "${release_notes_length}" ] && [ "${release_notes_length}" -gt 0 ] ; then
echo_info "Release notes length is defined: ${release_notes_length}. Truncating release notes ..."
release_notes=$(truncate_release_notes "${release_notes}" "${release_notes_length}")
fi
if [ ! -z "${release_notes_file}" ] && [ ! -f "${release_notes_file}" ] ; then
echo_warn "Path for Release Notes specified, but file does not exist at path: ${release_notes_file}"
fi
# Install Firebase
if [ "${upgrade_firebase_tools}" = true ] ; then
curl -sL firebase.tools | upgrade=true bash
else
curl -sL firebase.tools | bash
fi
# Deploy
echo_info "Deploying build to Firebase"
submit_cmd="firebase appdistribution:distribute \"${app_path}\""
submit_cmd="$submit_cmd --app \"${app}\""
## Optional params
if [ -n "${firebase_token}" ] ; then
submit_cmd="$submit_cmd --token \"${firebase_token}\""
fi
if [ -n "${release_notes}" ] ; then
submit_cmd="$submit_cmd --release-notes \"$(escape "$release_notes")\""
fi
if [ -n "${release_notes_file}" ] && [ -f "${release_notes_file}" ] ; then
submit_cmd="$submit_cmd --release-notes-file \"${release_notes_file}\""
fi
if [ -n "${testers}" ] ; then
submit_cmd="$submit_cmd --testers \"${testers}\""
fi
if [ -n "${groups}" ] ; then
submit_cmd="$submit_cmd --groups \"${groups}\""
fi
if [ -n "${flags}" ] ; then
submit_cmd="$submit_cmd \"${flags}\""
fi
if [ "${is_debug}" = true ] ; then
submit_cmd="$submit_cmd --debug"
fi
echo_details "$submit_cmd"
echo
max_retries=3
retry_delay=5
for ((i=1; i<=max_retries; i++)); do
if eval "${submit_cmd}"; then
echo_done "Success"
exit 0
else
if [ $i -lt $max_retries ]; then
echo_warn "Attempt $i failed. Retrying in $retry_delay seconds..."
sleep $retry_delay
else
echo_fail "All $max_retries attempts failed. Exiting."
fi
fi
done
echo_fail "Failed to deploy build to Firebase after $max_retries attempts."