From 23a3aaa61dd8b34298d1716900e8e1c116caf2b5 Mon Sep 17 00:00:00 2001 From: Siyuan Miao Date: Thu, 25 Sep 2025 15:29:51 +0000 Subject: [PATCH] dd script to build inside docker --- .devcontainer/install-deps.sh | 15 ++++++-- Dockerfile.build | 8 ++--- Makefile | 10 ++++-- dev_deploy.sh | 68 ++++++++++++++++++++++++++++++++--- 4 files changed, 88 insertions(+), 13 deletions(-) diff --git a/.devcontainer/install-deps.sh b/.devcontainer/install-deps.sh index 146e67a3..4435d25b 100755 --- a/.devcontainer/install-deps.sh +++ b/.devcontainer/install-deps.sh @@ -1,8 +1,19 @@ #!/bin/bash -set -e +SUDO_PATH=$(which sudo) +function sudo() { + if [ "$UID" -eq 0 ]; then + "$@" + else + ${SUDO_PATH} "$@" + fi +} -sudo apt-get update && sudo apt-get install -y --no-install-recommends \ +set -ex + +export DEBIAN_FRONTEND=noninteractive +sudo apt-get update && \ +sudo apt-get install -y --no-install-recommends \ build-essential \ device-tree-compiler \ gperf g++-multilib gcc-multilib \ diff --git a/Dockerfile.build b/Dockerfile.build index 716b754d..06210a48 100644 --- a/Dockerfile.build +++ b/Dockerfile.build @@ -1,11 +1,11 @@ # syntax=docker/dockerfile:1 -FROM golang:1.25.1-trixie +FROM --platform=${BUILDPLATFORM} golang:1.25.1-trixie AS builder ENV GOTOOLCHAIN=local -ENV GOPATH /go -ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH +ENV GOPATH=/go +ENV PATH=$GOPATH/bin:/usr/local/go/bin:$PATH -ADD .devcontainer/install-deps.sh /install-deps.sh +COPY .devcontainer/install-deps.sh /install-deps.sh RUN /install-deps.sh # Create build directory diff --git a/Makefile b/Makefile index f043a28c..5e0d6094 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ KVM_PKG_NAME := github.com/jetkvm/kvm BUILDKIT_FLAVOR := arm-rockchip830-linux-uclibcgnueabihf BUILDKIT_PATH ?= /opt/jetkvm-native-buildkit SKIP_NATIVE_IF_EXISTS ?= 0 - +SKIP_UI_BUILD ?= 0 GO_BUILD_ARGS := -tags netgo -tags timetzdata GO_RELEASE_BUILD_ARGS := -trimpath $(GO_BUILD_ARGS) @@ -88,6 +88,9 @@ build_dev_test: build_test2json build_gotestsum tar czfv device-tests.tar.gz -C $(BIN_DIR)/tests . frontend: + @if [ "$(SKIP_UI_BUILD)" = "1" ]; then \ + echo "Skipping frontend build..."; \ + else \ cd ui && npm ci && npm run build:device && \ find ../static/ \ -type f \ @@ -102,8 +105,9 @@ frontend: -o -name '*.svg' \ -o -name '*.webp' \ -o -name '*.woff2' \ - \) \ - -exec sh -c 'gzip -9 -kfv {}' \; + \) \ + -exec sh -c 'gzip -9 -kfv {}' \; \ + fi dev_release: frontend build_dev @echo "Uploading release... $(VERSION_DEV)" diff --git a/dev_deploy.sh b/dev_deploy.sh index c793efd7..58c2bd34 100755 --- a/dev_deploy.sh +++ b/dev_deploy.sh @@ -41,12 +41,16 @@ show_help() { REMOTE_USER="root" REMOTE_PATH="/userdata/jetkvm/bin" SKIP_UI_BUILD=false +SKIP_UI_BUILD_RELEASE=0 SKIP_NATIVE_BUILD=0 RESET_USB_HID_DEVICE=false LOG_TRACE_SCOPES="${LOG_TRACE_SCOPES:-jetkvm,cloud,websocket,native,jsonrpc}" RUN_GO_TESTS=false RUN_GO_TESTS_ONLY=false INSTALL_APP=false +BUILD_IN_DOCKER=false +DOCKER_BUILD_DEBUG=false +DOCKER_BUILD_TAG=ghcr.io/jetkvm/buildkit:latest # Parse command line arguments while [[ $# -gt 0 ]]; do @@ -71,6 +75,14 @@ while [[ $# -gt 0 ]]; do RESET_USB_HID_DEVICE=true shift ;; + --build-in-docker) + BUILD_IN_DOCKER=true + shift + ;; + --docker-build-debug) + DOCKER_BUILD_DEBUG=true + shift + ;; --run-go-tests) RUN_GO_TESTS=true shift @@ -103,11 +115,59 @@ if [ -z "$REMOTE_HOST" ]; then exit 1 fi +# check if the current CPU architecture is x86_64 +if [ "$(uname -m)" != "x86_64" ]; then + msg_warn "Warning: This script is only supported on x86_64 architecture" + BUILD_IN_DOCKER=true +fi + +if [ "$BUILD_IN_DOCKER" = true ]; then + if [ "$JETKVM_INSIDE_DOCKER" = 1 ]; then + msg_err "Error: already running inside Docker" + exit + fi + + BUILD_ARGS="--build-arg BUILDPLATFORM=linux/amd64" + if [ "$DOCKER_BUILD_DEBUG" = true ]; then + BUILD_ARGS="$BUILD_ARGS --progress=plain --no-cache" + fi + + msg_info "Checking if Docker is available ..." + if ! command -v docker &> /dev/null; then + msg_err "Error: Docker is not installed" + exit 1 + fi + msg_info "▶ Building build environment ..." + TMP_DIR=$(mktemp -d) + cp -r .devcontainer "${TMP_DIR}" + cp go.mod go.sum Dockerfile.build "${TMP_DIR}" + pushd "${TMP_DIR}" > /dev/null + docker build $BUILD_ARGS -t ${DOCKER_BUILD_TAG} -f Dockerfile.build . + popd > /dev/null + rm -rf "${TMP_DIR}" +fi + +do_make() { + if [ "$BUILD_IN_DOCKER" = true ]; then + msg_info "▶ Building the project in Docker ..." + docker run \ + --interactive \ + --tty \ + --rm \ + --env JETKVM_INSIDE_DOCKER=1 \ + -v "$(pwd):/build" \ + ${DOCKER_BUILD_TAG} make "$@" + else + make "$@" + fi +} + # Build the development version on the host # When using `make build_release`, the frontend will be built regardless of the `SKIP_UI_BUILD` flag -if [[ "$SKIP_UI_BUILD" = false && "$INSTALL_APP" = false ]]; then +if [[ "$SKIP_UI_BUILD" = false && "$JETKVM_INSIDE_DOCKER" != 1 ]]; then msg_info "▶ Building frontend" - make frontend + make frontend SKIP_UI_BUILD=0 + SKIP_UI_BUILD_RELEASE=1 fi if [ "$RUN_GO_TESTS" = true ]; then @@ -155,7 +215,7 @@ fi if [ "$INSTALL_APP" = true ] then msg_info "▶ Building release binary" - make build_release SKIP_NATIVE_IF_EXISTS=${SKIP_NATIVE_BUILD} + do_make build_release SKIP_NATIVE_IF_EXISTS=${SKIP_NATIVE_BUILD} SKIP_UI_BUILD=${SKIP_UI_BUILD_RELEASE} # Copy the binary to the remote host as if we were the OTA updater. ssh "${REMOTE_USER}@${REMOTE_HOST}" "cat > /userdata/jetkvm/jetkvm_app.update" < bin/jetkvm_app @@ -164,7 +224,7 @@ then ssh "${REMOTE_USER}@${REMOTE_HOST}" "reboot" else msg_info "▶ Building development binary" - make build_dev SKIP_NATIVE_IF_EXISTS=${SKIP_NATIVE_BUILD} + do_make build_dev SKIP_NATIVE_IF_EXISTS=${SKIP_NATIVE_BUILD} SKIP_UI_BUILD=${SKIP_UI_BUILD_RELEASE} # Kill any existing instances of the application ssh "${REMOTE_USER}@${REMOTE_HOST}" "killall jetkvm_app_debug || true"