Skip to content

Commit

Permalink
ci: Build and push official Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
nhooyr committed Dec 16, 2022
1 parent 9a23351 commit fcb0e56
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 29 deletions.
18 changes: 18 additions & 0 deletions ci/release/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
60 changes: 41 additions & 19 deletions ci/release/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -69,7 +77,7 @@ main() {
;;
run)
flag_reqarg && shift "$FLAGSHIFT"
JOBFILTER="$FLAGARG"
JOBFILTER=$FLAGARG
;;
host-only)
flag_noarg && shift "$FLAGSHIFT"
Expand All @@ -96,6 +104,10 @@ main() {
HOST_ONLY=1
LOCAL=1
;;
push-docker)
flag_noarg && shift "$FLAGSHIFT"
PUSH_DOCKER=1
;;
*)
flag_errusage "unrecognized flag $FLAGRAW"
;;
Expand All @@ -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
Expand All @@ -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() {
Expand All @@ -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..."
Expand All @@ -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..."
Expand Down Expand Up @@ -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 "$@"
1 change: 1 addition & 0 deletions ci/release/build/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/d2*/
File renamed without changes.
13 changes: 5 additions & 8 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion ci/sub
Submodule sub updated 3 files
+1 −1 lib.sh
+1 −1 lib/job.sh
+1 −0 release/release.sh
16 changes: 15 additions & 1 deletion docs/INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fcb0e56

Please sign in to comment.