d2/ci/release/build.sh

253 lines
6.6 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
usage: $0 [--rebuild] [--local] [--dry-run] [--run=regex] [--host-only] [--lockfile-force]
[--install] [--uninstall]
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 \$CI_D2_LINUX_AMD64, \$CI_D2_LINUX_ARM64,
\$CI_D2_MACOS_AMD64 and \$CI_D2_MACOS_ARM64 to build the release
2022-11-14 06:41:58 +00:00
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!
2022-11-14 06:41:58 +00:00
--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.
--lockfile-force
Forcefully take ownership of remote builder lockfiles.
--install
Ensure a release using --host-only and install it.
--uninstall
Ensure a release using --host-only and uninstall it.
--push-docker
Push the built docker image. Unfortunately dockerx requires the multi-arch images be
pushed if required in the same invocation as build. dockerx cannot load multi-arch
images into the daemon for push later. It's not slow though to use --push-docker after
building the image as nearly all artifacts are cached.
Automatically set if called from release.sh
2022-11-12 21:38:54 +00:00
EOF
}
2022-11-11 09:17:06 +00:00
main() {
2022-11-16 18:48:39 +00:00
while flag_parse "$@"; do
2022-11-12 22:19:09 +00:00
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 05:58:55 +00:00
;;
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
;;
lockfile-force)
flag_noarg && shift "$FLAGSHIFT"
LOCKFILE_FORCE=1
;;
install)
flag_noarg && shift "$FLAGSHIFT"
INSTALL=1
HOST_ONLY=1
LOCAL=1
;;
uninstall)
flag_noarg && shift "$FLAGSHIFT"
UNINSTALL=1
HOST_ONLY=1
LOCAL=1
;;
push-docker)
flag_noarg && shift "$FLAGSHIFT"
PUSH_DOCKER=1
;;
2022-11-12 22:19:09 +00:00
*)
flag_errusage "unrecognized flag $FLAGRAW"
;;
esac
done
2022-11-16 18:48:39 +00:00
shift "$FLAGSHIFT"
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
sh_c mkdir -p "$BUILD_DIR"
sh_c rm -f ci/release/build/latest
sh_c ln -s "$VERSION" ci/release/build/latest
2022-11-14 06:41:58 +00:00
if [ -n "${HOST_ONLY-}" ]; then
ensure_os
ensure_arch
runjob "$OS/$ARCH" "build"
if [ -n "${INSTALL-}" ]; then
sh_c make -sC "ci/release/build/$VERSION/$OS-$ARCH/d2-$VERSION" install
elif [ -n "${UNINSTALL-}" ]; then
sh_c make -sC "ci/release/build/$VERSION/$OS-$ARCH/d2-$VERSION" uninstall
fi
2022-11-14 06:41:58 +00:00
return 0
fi
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' &
runjob windows/amd64 'OS=windows ARCH=amd64 build' &
runjob windows/arm64 'OS=windows ARCH=arm64 build' &
2022-11-12 22:19:09 +00:00
waitjobs
runjob linux/dockerimage 'build_docker_image'
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)
REMOTE_HOST=$CI_D2_MACOS_AMD64 build_remote_macos
2022-11-14 14:57:01 +00:00
;;
arm64)
REMOTE_HOST=$CI_D2_MACOS_ARM64 build_remote_macos
2022-11-14 14:57:01 +00:00
;;
*)
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)
REMOTE_HOST=$CI_D2_LINUX_AMD64 build_remote_linux
2022-11-13 03:10:45 +00:00
;;
arm64)
REMOTE_HOST=$CI_D2_LINUX_ARM64 build_remote_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
}
build_remote_macos() {(
2022-11-16 12:02:35 +00:00
sh_c lockfile_ssh "$REMOTE_HOST" .d2-build-lock
sh_c gitsync "$REMOTE_HOST" src/d2
2022-11-20 10:09:32 +00:00
sh_c ssh "$REMOTE_HOST" "COLOR=${COLOR-} \
TERM=${TERM-} \
DRY_RUN=${DRY_RUN-} \
2022-11-14 14:57:01 +00:00
HW_BUILD_DIR=$HW_BUILD_DIR \
VERSION=$VERSION \
OS=$OS \
ARCH=$ARCH \
ARCHIVE=$ARCHIVE \
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 21:51:24 +00:00
sh_c mkdir -p "$HW_BUILD_DIR"
2022-11-16 12:02:35 +00:00
sh_c rsync --archive --human-readable "$REMOTE_HOST:src/d2/$ARCHIVE" "$ARCHIVE"
)}
2022-11-14 14:57:01 +00:00
build_remote_linux() {(
2022-11-16 12:02:35 +00:00
sh_c lockfile_ssh "$REMOTE_HOST" .d2-build-lock
sh_c gitsync "$REMOTE_HOST" src/d2
2022-11-20 10:09:32 +00:00
sh_c ssh "$REMOTE_HOST" "COLOR=${COLOR-} \
TERM=${TERM-} \
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 \
./src/d2/ci/release/build_in_docker.sh"
2022-11-14 21:51:24 +00:00
sh_c mkdir -p "$HW_BUILD_DIR"
2022-11-16 12:02:35 +00:00
sh_c rsync --archive --human-readable "$REMOTE_HOST:src/d2/$ARCHIVE" "$ARCHIVE"
)}
2022-11-13 03:10:45 +00:00
build_docker_image() {
D2_DOCKER_IMAGE=${D2_DOCKER_IMAGE:-terrastruct/d2}
flags='--load'
if [ -n "${PUSH_DOCKER-}" -o -n "${RELEASE-}" ]; then
flags='--push --platform linux/amd64,linux/arm64'
fi
sh_c docker buildx build $flags -t "$D2_DOCKER_IMAGE:$VERSION" -t "$D2_DOCKER_IMAGE:latest" --build-arg "VERSION=$VERSION" -f ./ci/release/Dockerfile "./ci/release/build/$VERSION"
}
2022-11-11 09:17:06 +00:00
main "$@"