forked from fileformat/bitrise-step-appcenter-app-release
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step.sh
182 lines (160 loc) · 4.92 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
#!/bin/bash
#
# upload an app from Bitrise to AppCenter
#
# API details: https://docs.microsoft.com/en-us/appcenter/distribution/uploading
#
set -o errexit
set -o pipefail
set -o nounset
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
}
echo_info "Starting AppCenter app upload at $(date -u +%Y-%m-%dT%H:%M:%SZ)"
validate_required_input "appcenter_api_token" ${appcenter_api_token:-}
validate_required_input "appcenter_name" ${appcenter_name:-}
validate_required_input "appcenter_org" ${appcenter_org:-}
validate_required_input "artifact_path" ${artifact_path:-}
if [ ! -f "${artifact_path}" ]; then
echo_fail "[!] File ${artifact_path} does not exist"
fi
if [ "${appcenter_api_token}" == "TestApiToken" ]
then
echo_done "Running in test mode: all the parameters look good!"
exit 0
fi
echo_info "Getting a release upload url for ${appcenter_org}/${appcenter_name}"
TMPFILE=$(mktemp)
STATUSCODE=$(curl \
-X POST \
--header 'Content-Type: application/json' \
--header "accept: application/json" \
--header "X-API-Token: ${appcenter_api_token}" \
--silent --show-error \
--output /dev/stderr --write-out "%{http_code}" \
"https://api.appcenter.ms/v0.1/apps/${appcenter_org}/${appcenter_name}/release_uploads" \
2> "${TMPFILE}")
if [ "${STATUSCODE}" -ne "201" ]
then
echo_fail "API call failed with ${STATUSCODE}: $(cat ${TMPFILE})"
fi
UPLOAD_URL=$(cat "${TMPFILE}" | jq .upload_url --raw-output)
UPLOAD_ID=$(cat "${TMPFILE}" | jq .upload_id --raw-output)
echo_details "result is ${STATUSCODE}: $(cat ${TMPFILE})"
rm "${TMPFILE}"
echo_info "Uploading ${artifact_path} to ${UPLOAD_URL}"
TMPFILE=$(mktemp)
STATUSCODE=$(curl \
-F "ipa=@${artifact_path}" \
--silent --show-error \
--output /dev/stderr --write-out "%{http_code}" \
"${UPLOAD_URL}" \
2> "${TMPFILE}")
if [ "${STATUSCODE}" -gt "299" ]
then
echo_fail "API call failed with ${STATUSCODE}: $(cat ${TMPFILE})"
fi
echo_details "result is ${STATUSCODE}: $(cat ${TMPFILE})"
rm "${TMPFILE}"
echo_info "Committing the release"
TMPFILE=$(mktemp)
STATUSCODE=$(curl \
-X PATCH \
--header "accept: application/json" \
--header "X-API-Token: ${appcenter_api_token}" \
--header "Content-Type: application/json" \
--silent --show-error \
--output /dev/stderr --write-out "%{http_code}" \
-d '{ "status": "committed"}' \
"https://api.appcenter.ms/v0.1/apps/${appcenter_org}/${appcenter_name}/release_uploads/${UPLOAD_ID}" \
2> "${TMPFILE}")
if [ "${STATUSCODE}" -ne "200" ]
then
echo_fail "API call failed with ${STATUSCODE}: $(cat ${TMPFILE})"
fi
RELEASE_ID=$(cat "${TMPFILE}" | jq .release_id --raw-output)
echo_details "result is ${STATUSCODE}: $(cat ${TMPFILE})"
rm "${TMPFILE}"
IFS=', ' read -r -a DISTRIBUTION_GROUPS <<< ${distribution_groups:-}
if [ ${#DISTRIBUTION_GROUPS[@]} -eq 0 ]
then
echo_info "Retrieving distribution groups for ${appcenter_name}"
TMPFILE=$(mktemp)
STATUSCODE=$(curl \
-X GET \
-H "accept: application/json" \
-H "X-API-Token: ${appcenter_api_token}" \
--silent --show-error \
--output /dev/stderr --write-out "%{http_code}" \
"https://api.appcenter.ms/v0.1/apps/${appcenter_org}/${appcenter_name}/distribution_groups" \
2> ${TMPFILE})
if [ "${STATUSCODE}" -ne "200" ]
then
echo_fail "API call failed with ${STATUSCODE}: $(cat ${TMPFILE})"
fi
echo_details "result is ${STATUSCODE}: $(cat ${TMPFILE})"
SAVEIFS="${IFS}"
IFS=$'\n'
DISTRIBUTION_GROUPS=( $(cat "${TMPFILE}" | jq '.[].name' --raw-output) )
IFS="${SAVEIFS}"
rm "${TMPFILE}"
fi
echo_details "distribution groups are ${DISTRIBUTION_GROUPS[*]}"
for DISTRIBUTION_GROUP in "${DISTRIBUTION_GROUPS[@]}"
do
echo_info "Adding to distribution group ${DISTRIBUTION_GROUP}"
TMPFILE=$(mktemp)
STATUSCODE=$(curl -X PATCH \
--header "Content-Type: application/json" \
--header "Accept: application/json" \
--header "X-API-Token: ${appcenter_api_token}" \
--silent --show-error \
--output /dev/stderr --write-out "%{http_code}" \
-d "{ \"destination_name\": \"${DISTRIBUTION_GROUP}\", \"release_notes\": \"${release_notes:-}\", \"notify_testers\": ${notify_testers:-true}}" \
"https://api.appcenter.ms/v0.1/apps/${appcenter_org}/${appcenter_name}/releases/${RELEASE_ID}" \
2> "${TMPFILE}")
if [ "${STATUSCODE}" -ne "200" ]
then
echo_fail "API call failed with ${STATUSCODE}: $(cat ${TMPFILE})"
fi
echo_details "result is ${STATUSCODE}: $(cat ${TMPFILE})"
rm "${TMPFILE}"
done
echo_done "Completed AppCenter app upload at $(date -u +%Y-%m-%dT%H:%M:%SZ)"