d2/ci/release/build.sh

195 lines
4.8 KiB
Bash
Raw Normal View History

2022-11-11 09:17:06 +00:00
#!/bin/sh
set -eu
cd -- "$(dirname "$0")/../.."
2022-11-12 19:53:54 +00:00
. ./ci/sub/lib.sh
2022-11-11 09:17:06 +00:00
2022-11-12 21:38:54 +00:00
help() {
cat <<EOF
2022-11-14 06:59:09 +00:00
usage: $0 [--rebuild] [--local] [--dry-run] [--run=regex] [--host-only]
2022-11-12 21:38:54 +00:00
2022-11-14 10:13:37 +00:00
$0 builds D2 release archives into ./ci/release/build/<version>/d2-<VERSION>-<OS>-<ARCH>.tar.gz
2022-11-12 22:19:09 +00:00
The version is detected via git describe which will use the git tag for the current
commit if available.
2022-11-12 21:38:54 +00:00
Flags:
2022-11-14 06:41:58 +00:00
--rebuild
By default build.sh will avoid rebuilding finished assets if they already exist but if you
changed something and need to force rebuild, use this flag.
--local
By default build.sh uses \$TSTRUCT_MACOS_AMD64_BUILDER, \$TSTRUCT_MACOS_ARM64_BUILDER,
\$TSTRUCT_LINUX_AMD64_BUILDER and \$TSTRUCT_LINUX_ARM64_BUILDER to build the release
archives. It's required for now due to the following issue:
https://github.com/terrastruct/d2/issues/31 With --local, build.sh will cross compile
locally. warning: This is only for testing purposes, do not use in production!
--host-only
Use to build the release archive for the host OS-ARCH only. All logging is done to stderr
so in a script you can read from stdout to get the path to the release archive.
--run=regex
Use to run only the OS-ARCH jobs that match the given regex. e.g. --run=linux only runs
the linux jobs. --run=linux-amd64 only runs the linux-amd64 job.
2022-11-14 10:13:37 +00:00
--version vX.X.X
Use to overwrite the version detected from git.
2022-11-12 21:38:54 +00:00
EOF
}
2022-11-11 09:17:06 +00:00
main() {
2022-11-12 22:19:09 +00:00
while :; do
flag_parse "$@"
case "$FLAG" in
h|help)
help
return 0
;;
rebuild)
2022-11-14 05:58:55 +00:00
flag_noarg && shift "$FLAGSHIFT"
2022-11-12 22:19:09 +00:00
REBUILD=1
;;
local)
2022-11-14 05:58:55 +00:00
flag_noarg && shift "$FLAGSHIFT"
2022-11-12 22:19:09 +00:00
LOCAL=1
;;
2022-11-14 06:59:09 +00:00
dry-run)
2022-11-14 05:58:55 +00:00
flag_noarg && shift "$FLAGSHIFT"
2022-11-14 06:59:09 +00:00
DRY_RUN=1
2022-11-12 22:19:09 +00:00
;;
2022-11-14 00:57:47 +00:00
run)
2022-11-14 05:58:55 +00:00
flag_reqarg && shift "$FLAGSHIFT"
JOBFILTER="$FLAGARG"
;;
2022-11-14 06:41:58 +00:00
host-only)
flag_noarg && shift "$FLAGSHIFT"
HOST_ONLY=1
2022-11-14 10:13:37 +00:00
LOCAL=1
;;
version)
flag_nonemptyarg && shift "$FLAGSHIFT"
VERSION=$FLAGARG
2022-11-14 06:41:58 +00:00
;;
2022-11-12 22:19:09 +00:00
'')
shift "$FLAGSHIFT"
break
;;
*)
flag_errusage "unrecognized flag $FLAGRAW"
;;
esac
done
2022-11-11 09:17:06 +00:00
2022-11-12 21:38:54 +00:00
if [ $# -gt 0 ]; then
flag_errusage "no arguments are accepted"
fi
2022-11-14 10:13:37 +00:00
VERSION=${VERSION:-$(git_describe_ref)}
BUILD_DIR=ci/release/build/$VERSION
2022-11-14 06:41:58 +00:00
if [ -n "${HOST_ONLY-}" ]; then
runjob $(os)-$(arch) "OS=$(os) ARCH=$(arch) build" &
waitjobs
return 0
fi
2022-11-12 21:38:54 +00:00
runjob linux-amd64 'OS=linux ARCH=amd64 build' &
runjob linux-arm64 'OS=linux ARCH=arm64 build' &
runjob macos-amd64 'OS=macos ARCH=amd64 build' &
runjob macos-arm64 'OS=macos ARCH=arm64 build' &
2022-11-12 22:19:09 +00:00
waitjobs
2022-11-11 09:17:06 +00:00
}
2022-11-13 03:10:45 +00:00
build() {
2022-11-14 10:13:37 +00:00
HW_BUILD_DIR="$BUILD_DIR/$OS-$ARCH/d2-$VERSION"
ARCHIVE="$BUILD_DIR/d2-$VERSION-$OS-$ARCH.tar.gz"
2022-11-13 03:10:45 +00:00
if [ -e "$ARCHIVE" -a -z "${REBUILD-}" ]; then
log "skipping as already built at $ARCHIVE"
return 0
fi
2022-11-13 03:42:39 +00:00
if [ -n "${LOCAL-}" ]; then
build_local
return 0
fi
2022-11-13 03:10:45 +00:00
case $OS in
2022-11-14 14:57:01 +00:00
macos)
case $ARCH in
amd64)
RHOST=$TSTRUCT_MACOS_AMD64_BUILDER build_rhost_macos
;;
arm64)
RHOST=$TSTRUCT_MACOS_ARM64_BUILDER build_rhost_macos
;;
*)
warn "no builder for OS=$OS ARCH=$ARCH, building locally..."
build_local
;;
esac
;;
2022-11-13 03:10:45 +00:00
linux)
case $ARCH in
amd64)
2022-11-14 14:57:01 +00:00
RHOST=$TSTRUCT_LINUX_AMD64_BUILDER build_rhost_linux
2022-11-13 03:10:45 +00:00
;;
arm64)
2022-11-14 14:57:01 +00:00
RHOST=$TSTRUCT_LINUX_ARM64_BUILDER build_rhost_linux
2022-11-13 03:10:45 +00:00
;;
*)
2022-11-14 14:57:01 +00:00
warn "no builder for OS=$OS ARCH=$ARCH, building locally..."
2022-11-13 03:10:45 +00:00
build_local
;;
esac
;;
*)
2022-11-14 06:41:58 +00:00
warn "no builder for OS=$OS, building locally..."
2022-11-13 03:42:39 +00:00
build_local
2022-11-13 03:10:45 +00:00
;;
esac
}
build_local() {
2022-11-14 06:59:09 +00:00
export DRY_RUN \
2022-11-13 03:10:45 +00:00
HW_BUILD_DIR \
VERSION \
OS \
ARCH \
ARCHIVE
sh_c ./ci/release/_build.sh
}
2022-11-14 14:57:01 +00:00
build_rhost_macos() {
sh_c ssh "$RHOST" mkdir -p src
sh_c rsync --archive --human-readable --delete ./ "$RHOST:src/d2/"
sh_c ssh -tttt "$RHOST" "DRY_RUN=${DRY_RUN-} \
HW_BUILD_DIR=$HW_BUILD_DIR \
VERSION=$VERSION \
OS=$OS \
ARCH=$ARCH \
ARCHIVE=$ARCHIVE \
TERM=$TERM \
2022-11-14 19:56:40 +00:00
PATH=\"/usr/local/bin:/usr/local/sbin:/opt/homebrew/bin:/opt/homebrew/sbin\${PATH+:\$PATH}\" \
2022-11-14 14:57:01 +00:00
./src/d2/ci/release/_build.sh"
2022-11-14 19:56:40 +00:00
sh_c mkdir -p $HW_BUILD_DIR
2022-11-14 14:57:01 +00:00
sh_c rsync --archive --human-readable "$RHOST:src/d2/$ARCHIVE" "$ARCHIVE"
}
build_rhost_linux() {
2022-11-13 03:10:45 +00:00
sh_c ssh "$RHOST" mkdir -p src
sh_c rsync --archive --human-readable --delete ./ "$RHOST:src/d2/"
2022-11-14 06:59:09 +00:00
sh_c ssh -tttt "$RHOST" "DRY_RUN=${DRY_RUN-} \
2022-11-13 03:10:45 +00:00
HW_BUILD_DIR=$HW_BUILD_DIR \
VERSION=$VERSION \
OS=$OS \
ARCH=$ARCH \
ARCHIVE=$ARCHIVE \
2022-11-14 05:58:55 +00:00
TERM=$TERM \
2022-11-13 03:10:45 +00:00
./src/d2/ci/release/build_docker.sh"
2022-11-14 19:56:40 +00:00
sh_c mkdir -p $HW_BUILD_DIR
2022-11-13 03:10:45 +00:00
sh_c rsync --archive --human-readable "$RHOST:src/d2/$ARCHIVE" "$ARCHIVE"
}
2022-11-11 09:17:06 +00:00
main "$@"