.githooks/pre-push
53 lines · 2049 bytes · executable
1#!/bin/sh
2# Build the macOS binaries when a version tag is pushed.
3#
4# gitbay's runner is Linux and has no platform targeting, so the darwin tarballs
5# cannot be built in CI. They are built here instead, on the Mac doing the push,
6# and this is the better place for the gate: a build failure aborts the push, so
7# a tag whose Mac binaries do not compile is never published. GitHub Actions
8# found that out only after the tag was already public.
9#
10# Uploading has to wait: `release asset add` needs the release, and the release is
11# created by the CI job this push triggers. So the upload is handed to a detached
12# helper that waits for the release to appear. Its log, and the tarballs, stay in
13# dist/macos/<tag>/ — rerun .githooks/upload-macos <tag> if it fails.
14#
15# Wired up with: git config core.hooksPath .githooks
16set -eu
17
18root=$(git rev-parse --show-toplevel)
19targets="aarch64-apple-darwin x86_64-apple-darwin"
20
21# stdin: <local ref> <local sha> <remote ref> <remote sha>, one line per ref.
22while read -r _local_ref _local_sha remote_ref _remote_sha; do
23 case "$remote_ref" in
24 refs/tags/v*) ;;
25 *) continue ;;
26 esac
27 tag=${remote_ref#refs/tags/}
28
29 version=$(cargo pkgid --manifest-path "$root/Cargo.toml" | sed 's/.*[#@]//')
30 if [ "$tag" != "v$version" ]; then
31 echo "pre-push: tag $tag does not match Cargo.toml version $version" >&2
32 exit 1
33 fi
34
35 dist="$root/dist/macos/$tag"
36 rm -rf "$dist"
37 mkdir -p "$dist"
38
39 for target in $targets; do
40 echo "pre-push: building $target"
41 (cd "$root" && cargo build --release --locked --target "$target")
42 staging="orgo-$tag-$target"
43 rm -rf "$root/$staging"
44 mkdir "$root/$staging"
45 cp "$root/target/$target/release/orgo" "$root/README.md" "$root/LICENSE" "$root/$staging/"
46 (cd "$root" && tar czf "$dist/$staging.tar.gz" "$staging")
47 rm -rf "$root/$staging"
48 (cd "$dist" && shasum -a 256 "$staging.tar.gz" >"$staging.tar.gz.sha256")
49 done
50
51 echo "pre-push: built $tag for $targets; upload waits for the release"
52 nohup "$root/.githooks/upload-macos" "$tag" >>"$dist/upload.log" 2>&1 &
53done