-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-installer.sh
More file actions
309 lines (268 loc) · 12.2 KB
/
Copy pathcreate-installer.sh
File metadata and controls
309 lines (268 loc) · 12.2 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env bash
#
# StackQL multi-arch installer for MacOS
#
# Downloads the darwin build artifacts for a given build number from the
# stackql GitHub Actions build workflow, combines them into a universal (fat)
# binary, signs it, creates a signed pkg, notarizes and staples the pkg, then
# uploads the pkg to the matching GitHub release.
#
# usage:
# bash create-installer.sh <build_number>
# eg: bash create-installer.sh 669
#
# Machine specific values (signing identities, login keychain password and the
# app specific password used for notarization) are read from vars.sh, which is
# gitignored. See vars.sh.example.
#
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
TARGET_DIR="$SCRIPTPATH/dist"
BIN_DIR="$SCRIPTPATH/bin"
DL_DIR="$SCRIPTPATH/downloads"
CONFIG_DIR="$SCRIPTPATH/config"
RES_DIR="$SCRIPTPATH/resources"
SCRIPTS_DIR="$SCRIPTPATH/scripts"
DATE=`date +%Y-%m-%d`
TIME=`date +%H:%M:%S`
LOG_PREFIX="[$DATE $TIME]"
# github source repo and build workflow (override with env vars if needed)
GH_REPO="${GH_REPO:-stackql/stackql}"
GH_WORKFLOW="${GH_WORKFLOW:-build.yml}"
GH_RUN_LOOKUP_LIMIT="${GH_RUN_LOOKUP_LIMIT:-500}"
ARTIFACT_AMD64="stackql_darwin_amd64"
ARTIFACT_ARM64="stackql_darwin_arm64"
PKG_NAME="stackql_darwin_multiarch.pkg"
PKG_FILE="${TARGET_DIR}/package/${PKG_NAME}"
NOTARY_PROFILE="stackql"
# functions
log_info() {
echo ""
echo "${LOG_PREFIX}[INFO]" "$1"
echo ""
}
log_warn() {
echo "${LOG_PREFIX}[WARN]" "$1"
}
log_error() {
echo "${LOG_PREFIX}[ERROR]" "$1"
}
die() {
log_error "$1"
exit 1
}
# download a build artifact from the workflow run as a zip file into downloads/
downloadArtifact() {
local name="$1"
local dest="${DL_DIR}/${name}.zip"
local artifact_info artifact_id artifact_expired
log_info "downloading artifact ${name} from run ${RUN_ID}..."
artifact_info=$(gh api "repos/${GH_REPO}/actions/runs/${RUN_ID}/artifacts?per_page=100" \
--jq ".artifacts[] | select(.name == \"${name}\") | \"\(.id) \(.expired)\"")
[[ -n "$artifact_info" ]] || die "artifact ${name} not found on run ${RUN_ID}"
artifact_id=$(echo "$artifact_info" | head -n 1 | awk '{print $1}')
artifact_expired=$(echo "$artifact_info" | head -n 1 | awk '{print $2}')
[[ "$artifact_expired" == "false" ]] || die "artifact ${name} (id ${artifact_id}) has expired, re-run the build workflow"
gh api "repos/${GH_REPO}/actions/artifacts/${artifact_id}/zip" > "$dest" || die "could not download artifact ${name}"
unzip -tq "$dest" >/dev/null 2>&1 || die "${dest} is not a valid zip file"
echo "downloaded ${dest} ($(du -h "$dest" | cut -f1))"
}
# notarize package using notarytool
notarizefile() {
filepath=${1:?"need a filepath"}
identifier=${2:?"need an identifier"}
# upload file using notarytool and wait for the notarization to complete
echo "## uploading $filepath for notarization"
notarization_result=$(xcrun notarytool submit "$filepath" \
--keychain-profile "$NOTARY_PROFILE" \
--wait \
--output-format json)
notarization_info=$(echo "$notarization_result" | jq -r '.id')
echo "Notarization Info: $notarization_info"
# Check if notarization failed
if [[ $notarization_info == "" ]]; then
echo "Could not upload for notarization"
echo "$notarization_result"
exit 1
fi
# Extracting the status
notarization_status=$(echo "$notarization_result" | jq -r '.status')
if [[ $notarization_status != "Accepted" ]]; then
echo "## could not notarize $filepath"
echo "$notarization_result"
exit 1
fi
echo "## notarization was successful for $filepath"
}
# staple notarization ticket to package using notarytool
staplefile() {
filepath=${1:?"need a filepath"}
echo "## stapling ticket to $filepath"
xcrun stapler staple "$filepath"
if [[ $? != 0 ]]; then
echo "## could not staple $filepath"
exit 1
fi
echo "## successfully stapled ticket to $filepath"
}
# ---------------------------------------------------------------------------
# arguments
# ---------------------------------------------------------------------------
BUILD_NUMBER="$1"
if [[ -z "$BUILD_NUMBER" ]]; then
echo "usage: bash $(basename "$0") <build_number> (eg: bash $(basename "$0") 669)"
exit 1
fi
[[ "$BUILD_NUMBER" =~ ^[0-9]+$ ]] || die "build number must be numeric, got '${BUILD_NUMBER}'"
# ---------------------------------------------------------------------------
# machine specific variables
# ---------------------------------------------------------------------------
if [[ -f "$SCRIPTPATH/vars.sh" ]]; then
source "$SCRIPTPATH/vars.sh"
fi
for v in DEV_TEAM DEV_ACCOUNT APP_SIGNATURE INST_SIGNATURE KEYCHAIN_PASSWORD APPLE_APP_PASSWORD; do
[[ -n "${!v}" ]] || die "${v} is not set, add it to ${SCRIPTPATH}/vars.sh (see vars.sh.example)"
done
app_signature="${APP_SIGNATURE}"
inst_signature="${INST_SIGNATURE}"
# ---------------------------------------------------------------------------
# preflight
# ---------------------------------------------------------------------------
for t in gh unzip lipo codesign pkgbuild xcrun security jq; do
command -v "$t" >/dev/null 2>&1 || die "required tool '${t}' not found on PATH"
done
gh auth status >/dev/null 2>&1 || die "gh is not authenticated, run 'gh auth login'"
log_info "unlocking keychain and storing notarization credentials..."
security unlock-keychain -p "$KEYCHAIN_PASSWORD" ~/Library/Keychains/login.keychain-db || die "could not unlock the login keychain"
xcrun notarytool store-credentials "$NOTARY_PROFILE" \
--apple-id "$DEV_ACCOUNT" \
--team-id "$DEV_TEAM" \
--password "$APPLE_APP_PASSWORD" || die "could not store notarization credentials"
# ---------------------------------------------------------------------------
# clean bin/, downloads/ and dist/ contents
# ---------------------------------------------------------------------------
log_info "cleaning bin/, downloads/ and dist/ contents..."
mkdir -p "$BIN_DIR" "$DL_DIR" "$TARGET_DIR/package" "$TARGET_DIR/archive"
rm -f "$BIN_DIR"/stackql
rm -f "$BIN_DIR"/stackql.gz
rm -f "$BIN_DIR"/stackql_amd64
rm -f "$BIN_DIR"/stackql_arm64
rm -f "$BIN_DIR"/stackql_darwin_arm64.zip
rm -f "$BIN_DIR"/stackql_darwin_amd64.zip
rm -f "$DL_DIR"/*.zip
rm -f "$TARGET_DIR"/archive/stackql*
rm -f "$TARGET_DIR"/package/stackql*
# ---------------------------------------------------------------------------
# step 1-2: locate the build workflow run and download the darwin artifacts
# ---------------------------------------------------------------------------
log_info "looking up build ${BUILD_NUMBER} in ${GH_REPO} (${GH_WORKFLOW})..."
RUN_INFO=$(gh run list --repo "$GH_REPO" --workflow "$GH_WORKFLOW" --limit "$GH_RUN_LOOKUP_LIMIT" \
--json number,databaseId,headBranch,headSha,conclusion \
--jq ".[] | select(.number == ${BUILD_NUMBER}) | \"\(.databaseId) \(.headBranch) \(.headSha[0:7]) \(.conclusion)\"")
[[ -n "$RUN_INFO" ]] || die "build ${BUILD_NUMBER} not found in the last ${GH_RUN_LOOKUP_LIMIT} runs of ${GH_WORKFLOW} in ${GH_REPO}"
RUN_ID=$(echo "$RUN_INFO" | head -n 1 | awk '{print $1}')
RUN_BRANCH=$(echo "$RUN_INFO" | head -n 1 | awk '{print $2}')
RUN_SHA=$(echo "$RUN_INFO" | head -n 1 | awk '{print $3}')
RUN_CONCLUSION=$(echo "$RUN_INFO" | head -n 1 | awk '{print $4}')
echo "build ${BUILD_NUMBER} = run ${RUN_ID} (branch ${RUN_BRANCH}, commit ${RUN_SHA}, conclusion ${RUN_CONCLUSION})"
if [[ "$RUN_CONCLUSION" != "success" ]]; then
log_warn "run ${RUN_ID} conclusion is '${RUN_CONCLUSION}', continuing with whatever artifacts it produced"
fi
downloadArtifact "$ARTIFACT_ARM64"
downloadArtifact "$ARTIFACT_AMD64"
# ---------------------------------------------------------------------------
# step 3: extract the binaries, combine them and get the version
# ---------------------------------------------------------------------------
cd "$BIN_DIR" || die "could not cd to ${BIN_DIR}"
log_info "unzipping ARM build..."
unzip -o "${DL_DIR}/${ARTIFACT_ARM64}.zip" -d "$BIN_DIR" || die "could not unzip ${ARTIFACT_ARM64}.zip"
log_info "renaming ARM build..."
mv stackql stackql_arm64 || die "stackql binary not found in ${ARTIFACT_ARM64}.zip"
log_info "unzipping x64 build..."
unzip -o "${DL_DIR}/${ARTIFACT_AMD64}.zip" -d "$BIN_DIR" || die "could not unzip ${ARTIFACT_AMD64}.zip"
log_info "renaming x64 build..."
mv stackql stackql_amd64 || die "stackql binary not found in ${ARTIFACT_AMD64}.zip"
ls .
# combine
log_info "combining builds..."
lipo -create -output stackql stackql_amd64 stackql_arm64 || die "lipo failed"
# get version
log_info "getting version..."
chmod +x stackql
chmod 755 stackql
VERSION_OUTPUT=$(./stackql --version 2>&1)
echo "$VERSION_OUTPUT"
VERSION=$(echo "$VERSION_OUTPUT" | sed -n -E 's/.*v([0-9]+\.[0-9]+\.[0-9]+).*/\1/p' | head -n 1)
[[ -n "$VERSION" ]] || die "could not parse a semver from the version output above"
VERSION_PATCH="${VERSION##*.}"
[[ "$VERSION_PATCH" == "$BUILD_NUMBER" ]] || die "version ${VERSION} does not match build number ${BUILD_NUMBER}, check the artifacts"
log_info "version ${VERSION}..."
# ---------------------------------------------------------------------------
# step 4-7: check the github release and whether the pkg asset already exists
# ---------------------------------------------------------------------------
RELEASE_TAG="v${VERSION}"
log_info "looking up release ${RELEASE_TAG} in ${GH_REPO}..."
RELEASE_ERR=$(gh release view "$RELEASE_TAG" --repo "$GH_REPO" --json tagName 2>&1 >/dev/null)
RELEASE_RC=$?
if [[ $RELEASE_RC -ne 0 ]]; then
if echo "$RELEASE_ERR" | grep -qi "release not found"; then
echo "Release ${VERSION} does not exist, please create it first"
exit 1
fi
die "could not look up release ${RELEASE_TAG}: ${RELEASE_ERR}"
fi
PKG_EXISTS=$(gh release view "$RELEASE_TAG" --repo "$GH_REPO" --json assets \
--jq "[.assets[].name] | index(\"${PKG_NAME}\") != null")
if [[ "$PKG_EXISTS" == "true" ]]; then
UPLOAD_PKG="false"
echo "The ${PKG_NAME} asset already exists in release ${VERSION}, the package will be saved locally for a manual update"
elif [[ "$PKG_EXISTS" == "false" ]]; then
UPLOAD_PKG="true"
echo "${PKG_NAME} is not in release ${VERSION} yet, it will be uploaded after packaging"
else
die "could not list assets for release ${RELEASE_TAG}: ${PKG_EXISTS}"
fi
# ---------------------------------------------------------------------------
# step 8: sign the binary, then create, notarize and staple the pkg
# ---------------------------------------------------------------------------
# sign binary
log_info "signing binary..."
codesign --options=runtime -s "$app_signature" --timestamp stackql || die "codesign failed"
# verify signature
log_info "verifying signature..."
codesign -dv --verbose=4 stackql
codesign --verify --verbose stackql || die "signature verification failed"
# clean bin dir
log_info "cleaning bin/ directory..."
rm -f "$BIN_DIR"/stackql_amd64
rm -f "$BIN_DIR"/stackql_arm64
cd "$SCRIPTPATH" || die "could not cd to ${SCRIPTPATH}"
# create and sign latest package
log_info "creating package..."
pkgbuild --identifier com.stackql.pkg \
--version "${VERSION}" \
--install-location /usr/local/bin \
--sign "$inst_signature" \
--timestamp \
--root "$BIN_DIR" \
"$PKG_FILE" || die "pkgbuild failed"
# notarize package
log_info "notarizing package..."
notarizefile "$PKG_FILE" "com.stackql.pkg"
# staple ticket
log_info "stapling ticket..."
staplefile "$PKG_FILE"
# create versioned package
log_info "creating versioned package..."
cp "$PKG_FILE" "${TARGET_DIR}/package/stackql_${VERSION}_darwin_multiarch.pkg"
# ---------------------------------------------------------------------------
# step 9: upload the pkg to the release (skipped if the asset already exists)
# ---------------------------------------------------------------------------
if [[ "$UPLOAD_PKG" == "true" ]]; then
log_info "uploading ${PKG_NAME} to release ${RELEASE_TAG}..."
gh release upload "$RELEASE_TAG" "$PKG_FILE" --repo "$GH_REPO" || die "upload failed, the package is saved at ${PKG_FILE}"
echo "uploaded ${PKG_NAME} to https://github.com/${GH_REPO}/releases/tag/${RELEASE_TAG}"
else
log_info "skipping upload, ${PKG_NAME} already exists in release ${RELEASE_TAG}"
echo "package saved at ${PKG_FILE} for a manual update"
fi
log_info "DONE!!!"