-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpublish.bash
executable file
·295 lines (245 loc) · 7.89 KB
/
publish.bash
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
#!/usr/bin/env bash
set -euo pipefail
if test -z "$APPLE_DEVELOPER_ID" ||
test -z "$APPLE_DEVELOPER_TEAM" ||
test -z "$APPLE_DEVELOPER_PASSWORD" ||
test -z "$CERTIFICATE_BASE64" ||
test -z "$CERTIFICATE_PASSWORD" ; then
echo "env not set"
exit 1
fi
function log() { echo "$@" 1>&2 ; }
function main() {
git fetch --tags --force
rm -rf dist ; mkdir dist
bdir="$(mktemp -d)"
pdir="$(mktemp -d)"
# shellcheck disable=2154
trap 'code=$? ; echo "FAILURE: $code" ; rm -rf $bdir $pdir ; exit $code' EXIT
version_name="$(git describe --tags --abbrev=0 HEAD)"
log "-- Building '$version_name'"
setup_macos_keychain
build freebsd arm64
build freebsd amd64
build linux arm64
build linux amd64
build darwin arm64
build darwin amd64
make_macos_universal_binary
sign_macos run_darwin_amd64
sign_macos run_darwin_arm64
sign_macos run_darwin_universal
lipod_submission_id="$(notarize_macos run_darwin_universal)"
amd64_submission_id="$(notarize_macos run_darwin_amd64)"
arm64_submission_id="$(notarize_macos run_darwin_arm64)"
wait_for_notarization "$lipod_submission_id"
wait_for_notarization "$amd64_submission_id"
wait_for_notarization "$arm64_submission_id"
create_package freebsd arm64
create_package freebsd amd64
create_package linux arm64
create_package linux amd64
create_package darwin arm64
create_package darwin amd64
create_package darwin universal
create_release
}
# setup_macos_keychain adds $CERTIFICATE_BASE64 to the macOS keychain.
function setup_macos_keychain() {
log "-- Setting up macos keychain"
cert_path="$(mktemp -d)/cert.p12"
keychain_path="$(mktemp -d)/keys.keychain-db"
keychain_password="$(openssl rand -base64 10)"
# decode cert to file
echo -n "$CERTIFICATE_BASE64" | base64 --decode -o "$cert_path"
# create temporary keychain
security create-keychain -p "$keychain_password" "$keychain_path"
security set-keychain-settings -lut 21600 "$keychain_path"
security unlock-keychain -p "$keychain_password" "$keychain_path"
# import certificate to keychain
security import "$cert_path" \
-P "$CERTIFICATE_PASSWORD" \
-A \
-t cert \
-f pkcs12 \
-k "$keychain_path"
security set-key-partition-list \
-S apple-tool:,apple: \
-k "$keychain_password" \
"$keychain_path"
security list-keychain \
-d user \
-s "$keychain_path"
}
# build, given GOOS and GOARCH values, builds a binary file and places it
# in $bdir.
function build() {
goos="$1"; goarch="$2"
if test -z "$goos" || test -z "$goarch" ; then
log "build requires 2 arguments"
return 1
fi
log "-- Building $goos ($goarch)"
CGO_ENABLED=0 GOOS=$goos GOARCH=$goarch \
go build \
-ldflags="-s -w -X 'github.com/amonks/run.Version=${version_name}'" \
-o "${bdir}/run_${goos}_${goarch}" \
./cmd/run
}
## make_macos_universal_binary combines $bdir/run_darwin_amd64 and
#$bdir/run_darwin_arm64 into a "fat" universal binary called
#$bdir/run_darwin_universal.
function make_macos_universal_binary() {
log "-- Making universal macos binary"
lipo \
-output "${bdir}/run_darwin_universal" \
-create "${bdir}/run_darwin_amd64" "${bdir}/run_darwin_arm64"
}
## sign_macos codesigns the given macOS binary.
function sign_macos() {
binary=$1
if test -z "$binary" ; then
log "sign_macos requires 1 argument"
return 1
fi
log "-- Signing $binary"
codesign \
--keychain buildagent \
-s 'Developer ID Application: Andrew Monks (89WR6ARSCL)' \
--timestamp \
--options runtime \
"${bdir}/${binary}"
return 0
}
# notarize_macos, given a binary file, sends it to Apple for notarization. It
# returns immediately after the submission is submitted, it **does not** wait
# for the submission to complete. It returns the Submission ID, which can be
# used to wait for the submission or check its status.
#
# When a binary is run for the first time on macOS, the operating system
# sends its hash to Apple's server to check whether it appears in their
# notarization database. If it is not, the binary won't run.
function notarize_macos() {
binary="$1"
if test -z "$binary" ; then
log "notarize_macos requires 1 argument"
return 1
fi
log "-- Asking Apple to notarize $binary; this could take some time"
/usr/bin/ditto -c -k \
--keepParent \
"${bdir}/${binary}" \
"${bdir}/${binary}.zip"
xcrun notarytool submit \
--output-format json \
--apple-id "$APPLE_DEVELOPER_ID" \
--team-id "$APPLE_DEVELOPER_TEAM" \
--password "$APPLE_DEVELOPER_PASSWORD" \
"${bdir}/${binary}.zip" | \
sed 's/.*"id":"\([-A-z0-9]\{36\}\)".*/\1/'
}
function wait_for_notarization() {
submission_id="$1"
if test -z "$submission_id" ; then
log "wait_for_notarization requires 1 argument"
return 1
fi
log "-- Waiting for notarization submission '$submission_id'"
xcrun notarytool wait "$submission_id" \
--apple-id "$APPLE_DEVELOPER_ID" \
--team-id "$APPLE_DEVELOPER_TEAM" \
--password "$APPLE_DEVELOPER_PASSWORD"
}
# create_pacakge, given GOOS and GOARCH values, produces a tar.gz archive
# in ./dist. The archive contains the relevant platform's binary, plus
# support files. It then appends the archive's checksum to
# ./dist/checksum.txt.
#
# The relevant binary must already exist in $pdir when create_package is
# called.
function create_package() {
os="$1"; arch="$2"
if test -z "$os" || test -z "$arch" ; then
log "create_package requires 2 arguments"
return 1
fi
package_name="$(package_name "$os" "$arch")"
log "-- Creating package $package_name"
binary="${bdir}/run_${os}_${arch}"
mkdir -p "${pdir}/${package_name}"
cp -r docs "${pdir}/${package_name}/"
cp CONTRIBUTORS.md "${pdir}/${package_name}/"
cp CREDITS.txt "${pdir}/${package_name}/"
cp LICENSE.md "${pdir}/${package_name}/"
cp README.md "${pdir}/${package_name}/"
cp "$binary" "${pdir}/${package_name}/run"
distdir="$(pwd)/dist"
pushd "${pdir}/${package_name}"
tar \
--create \
--gzip \
--file "${distdir}/${package_name}.tar.gz" \
. \
&> /dev/null
popd
pushd dist ; shasum "${package_name}.tar.gz" >> checksums.txt ; popd
}
# package_name, given GOOS and GOARCH values, produces the same string
# that this command would on the target platform:
# echo "run_$(uname -s)_$(uname -m)"
function package_name() {
os="$1"; arch="$2"
if test -z "$os" || test -z "$arch" ; then
log "package_name requires 2 arguments"
return 1
fi
os_name="" ; arch_name=""
case $os in
darwin) os_name="Darwin" ;;
freebsd) os_name="FreeBSD" ;;
linux) os_name="Linux" ;;
*) log "unsupported os $os" ; exit 1 ;;
esac
case $arch in
arm64) arch_name="arm64" ;;
amd64) arch_name="amd64" ;;
universal) arch_name="universal" ;;
*) log "unsupported arch $arch" ; exit 1 ;;
esac
if test "$os" = "darwin" && test "$arch" = "amd64" ; then
arch_name="x86_64"
fi
echo "run_${os_name}_${arch_name}"
}
# create_release_notes produces markdown including a changelog (heuristic:
# since the last git tag) and instructions for determining the right
# binary for a given platform.
function create_release_notes() {
last_release="$(git describe --tags --abbrev=0 HEAD~)"
echo "## Changelog"
git log "$last_release..HEAD" --oneline --decorate=no | sort | awk '{ print "- " $0 }'
echo ""
if git log "$last_release..HEAD" | grep -q BREAKING ; then
echo "## Breaking Changes"
git log "$last_release..HEAD" | \
grep BREAKING | \
sed 's/^[[:space:]]*BREAKING CHANGE: //g' | \
awk '{ print "- " $0 }'
echo ""
fi
echo "> [!TIP]"
echo "> You can find the correct asset for your system with the following command:"
# shellcheck disable=2016
echo '> `echo "run_$(uname -s)_$(uname -m).tar.gz"`'
}
# create_release uses the Github API to create the release and upload the
# release artifacts.
function create_release() {
log "-- Uploading release to Github"
create_release_notes | gh release create \
--repo="${GITHUB_REPOSITORY:-amonks/run}" \
--notes-file=- \
"$version_name" \
dist/*
}
main