From fcb0e564c74fcae5fb65aabd3ee0070065cde7d7 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Thu, 15 Dec 2022 22:49:26 -0800 Subject: [PATCH] ci: Build and push official Dockerfile Closes #76 --- ci/release/Dockerfile | 18 ++++++ ci/release/build.sh | 60 +++++++++++++------ ci/release/build/.dockerignore | 1 + .../{build_docker.sh => build_in_docker.sh} | 0 ci/release/changelogs/next.md | 13 ++-- ci/sub | 2 +- docs/INSTALL.md | 16 ++++- 7 files changed, 81 insertions(+), 29 deletions(-) create mode 100644 ci/release/Dockerfile create mode 100644 ci/release/build/.dockerignore rename ci/release/{build_docker.sh => build_in_docker.sh} (100%) diff --git a/ci/release/Dockerfile b/ci/release/Dockerfile new file mode 100644 index 0000000000..d195fa05b3 --- /dev/null +++ b/ci/release/Dockerfile @@ -0,0 +1,18 @@ +# https://hub.docker.com/repository/docker/terrastruct/d2 +FROM debian:latest + +ARG TARGETARCH + +COPY ./d2-*-linux-$TARGETARCH.tar.gz /tmp +RUN mkdir -p /usr/local/lib/d2 \ + && tar -C /usr/local/lib/d2 -xzf /tmp/d2-*-linux-"$TARGETARCH".tar.gz \ + && /usr/local/lib/d2/d2-*/scripts/install.sh \ + && rm -Rf /tmp/d2-*-linux-"$TARGETARCH".tar.gz + +WORKDIR /root/src +EXPOSE 8080 +ENV PORT 8080 +ENV HOST 0.0.0.0 +ENV BROWSER false + +ENTRYPOINT ["/usr/local/bin/d2"] diff --git a/ci/release/build.sh b/ci/release/build.sh index e10426b927..cbc63114bf 100755 --- a/ci/release/build.sh +++ b/ci/release/build.sh @@ -20,11 +20,12 @@ Flags: 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 + 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 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! + 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 @@ -45,6 +46,13 @@ Flags: --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 EOF } @@ -69,7 +77,7 @@ main() { ;; run) flag_reqarg && shift "$FLAGSHIFT" - JOBFILTER="$FLAGARG" + JOBFILTER=$FLAGARG ;; host-only) flag_noarg && shift "$FLAGSHIFT" @@ -96,6 +104,10 @@ main() { HOST_ONLY=1 LOCAL=1 ;; + push-docker) + flag_noarg && shift "$FLAGSHIFT" + PUSH_DOCKER=1 + ;; *) flag_errusage "unrecognized flag $FLAGRAW" ;; @@ -108,10 +120,13 @@ main() { 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 if [ -n "${HOST_ONLY-}" ]; then ensure_os ensure_arch - runjob "$OS-$ARCH" "build" + runjob "$OS/$ARCH" "build" if [ -n "${INSTALL-}" ]; then sh_c make -sC "ci/release/build/$VERSION/$OS-$ARCH/d2-$VERSION" install @@ -121,13 +136,15 @@ main() { 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' & + 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' & waitjobs + + runjob linux/dockerimage 'build_docker_image' } build() { @@ -148,10 +165,10 @@ build() { macos) case $ARCH in amd64) - REMOTE_HOST=$TSTRUCT_MACOS_AMD64_BUILDER build_remote_macos + REMOTE_HOST=$CI_D2_MACOS_AMD64 build_remote_macos ;; arm64) - REMOTE_HOST=$TSTRUCT_MACOS_ARM64_BUILDER build_remote_macos + REMOTE_HOST=$CI_D2_MACOS_ARM64 build_remote_macos ;; *) warn "no builder for OS=$OS ARCH=$ARCH, building locally..." @@ -162,10 +179,10 @@ build() { linux) case $ARCH in amd64) - REMOTE_HOST=$TSTRUCT_LINUX_AMD64_BUILDER build_remote_linux + REMOTE_HOST=$CI_D2_LINUX_AMD64 build_remote_linux ;; arm64) - REMOTE_HOST=$TSTRUCT_LINUX_ARM64_BUILDER build_remote_linux + REMOTE_HOST=$CI_D2_LINUX_ARM64 build_remote_linux ;; *) warn "no builder for OS=$OS ARCH=$ARCH, building locally..." @@ -218,13 +235,18 @@ VERSION=$VERSION \ OS=$OS \ ARCH=$ARCH \ ARCHIVE=$ARCHIVE \ -./src/d2/ci/release/build_docker.sh" +./src/d2/ci/release/build_in_docker.sh" sh_c mkdir -p "$HW_BUILD_DIR" sh_c rsync --archive --human-readable "$REMOTE_HOST:src/d2/$ARCHIVE" "$ARCHIVE" )} -ssh() { - command ssh -o='StrictHostKeyChecking=accept-new' "$@" +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" } main "$@" diff --git a/ci/release/build/.dockerignore b/ci/release/build/.dockerignore new file mode 100644 index 0000000000..73f0caa06a --- /dev/null +++ b/ci/release/build/.dockerignore @@ -0,0 +1 @@ +**/d2*/ diff --git a/ci/release/build_docker.sh b/ci/release/build_in_docker.sh similarity index 100% rename from ci/release/build_docker.sh rename to ci/release/build_in_docker.sh diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index fcaa580c4e..2e07b3a1e7 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -1,15 +1,12 @@ #### Features ๐Ÿš€ -- Diagram padding can now can be configured in the CLI (default 100px). - [https://github.com/terrastruct/d2/pull/431](https://github.com/terrastruct/d2/pull/431) +- Diagram padding can now can be configured in the CLI (default 100px). [https://github.com/terrastruct/d2/pull/431](https://github.com/terrastruct/d2/pull/431) +- Add official Docker image. See [./docs/INSTALL.md#docker](./docs/INSTALL.md#docker). [#76](https://github.com/terrastruct/d2/issues/76) #### Improvements ๐Ÿงน #### Bugfixes โ›‘๏ธ -- Fixed crash when sequence diagrams had no messages. - [https://github.com/terrastruct/d2/pull/427](https://github.com/terrastruct/d2/pull/427) -- Fixed `constraint` keyword setting label. - [https://github.com/terrastruct/d2/issues/415](https://github.com/terrastruct/d2/issues/415) -- Fixed serialization affecting binary plugins (TALA). - [https://github.com/terrastruct/d2/pull/426](https://github.com/terrastruct/d2/pull/426) +- Fixed crash when sequence diagrams had no messages. [https://github.com/terrastruct/d2/pull/427](https://github.com/terrastruct/d2/pull/427) +- Fixed `constraint` keyword setting label. [https://github.com/terrastruct/d2/issues/415](https://github.com/terrastruct/d2/issues/415) +- Fixed serialization affecting binary plugins (TALA). [https://github.com/terrastruct/d2/pull/426](https://github.com/terrastruct/d2/pull/426) diff --git a/ci/sub b/ci/sub index 2f309fd0e3..93bb2df10f 160000 --- a/ci/sub +++ b/ci/sub @@ -1 +1 @@ -Subproject commit 2f309fd0e35f323dfaafdd0075cc3da565fa630c +Subproject commit 93bb2df10f248a30142a651ec57ca64eb0e82e84 diff --git a/docs/INSTALL.md b/docs/INSTALL.md index 30e31d7cd9..1ed8582509 100644 --- a/docs/INSTALL.md +++ b/docs/INSTALL.md @@ -216,9 +216,23 @@ under plain Windows. aka Windows Subsystem for Linux if that's what you prefer. Installation is just like any other Linux system. +## Docker + +https://hub.docker.com/repository/docker/terrastruct/d2 + +We publish `amd64` and `arm64` images based on `debian:latest` for each release. + +Example usage: + +```sh +echo 'x -> y' >helloworld.d2 +docker run --rm -it -u "$(id -u):$(id -g)" -v "$PWD:/root/src" \ + -p 127.0.0.1:8080:8080 terrastruct/d2 --watch helloworld.d2 +# Visit http://127.0.0.1:8080 +``` + ## Coming soon -- Docker image - rpm and deb packages - with repositories and standalone - homebrew core