dd script to build inside docker

This commit is contained in:
Siyuan Miao 2025-09-25 15:29:51 +00:00 committed by Siyuan
parent 6c677f2f95
commit d6802c982d
4 changed files with 88 additions and 13 deletions

View File

@ -1,8 +1,19 @@
#!/bin/bash #!/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 \ build-essential \
device-tree-compiler \ device-tree-compiler \
gperf g++-multilib gcc-multilib \ gperf g++-multilib gcc-multilib \

View File

@ -1,11 +1,11 @@
# syntax=docker/dockerfile:1 # syntax=docker/dockerfile:1
FROM golang:1.25.1-trixie FROM --platform=${BUILDPLATFORM} golang:1.25.1-trixie AS builder
ENV GOTOOLCHAIN=local ENV GOTOOLCHAIN=local
ENV GOPATH /go ENV GOPATH=/go
ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH 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 RUN /install-deps.sh
# Create build directory # Create build directory

View File

@ -11,7 +11,7 @@ KVM_PKG_NAME := github.com/jetkvm/kvm
BUILDKIT_FLAVOR := arm-rockchip830-linux-uclibcgnueabihf BUILDKIT_FLAVOR := arm-rockchip830-linux-uclibcgnueabihf
BUILDKIT_PATH ?= /opt/jetkvm-native-buildkit BUILDKIT_PATH ?= /opt/jetkvm-native-buildkit
SKIP_NATIVE_IF_EXISTS ?= 0 SKIP_NATIVE_IF_EXISTS ?= 0
SKIP_UI_BUILD ?= 0
GO_BUILD_ARGS := -tags netgo -tags timetzdata GO_BUILD_ARGS := -tags netgo -tags timetzdata
GO_RELEASE_BUILD_ARGS := -trimpath $(GO_BUILD_ARGS) 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 . tar czfv device-tests.tar.gz -C $(BIN_DIR)/tests .
frontend: frontend:
@if [ "$(SKIP_UI_BUILD)" = "1" ]; then \
echo "Skipping frontend build..."; \
else \
cd ui && npm ci && npm run build:device && \ cd ui && npm ci && npm run build:device && \
find ../static/ \ find ../static/ \
-type f \ -type f \
@ -103,7 +106,8 @@ frontend:
-o -name '*.webp' \ -o -name '*.webp' \
-o -name '*.woff2' \ -o -name '*.woff2' \
\) \ \) \
-exec sh -c 'gzip -9 -kfv {}' \; -exec sh -c 'gzip -9 -kfv {}' \; \
fi
dev_release: frontend build_dev dev_release: frontend build_dev
@echo "Uploading release... $(VERSION_DEV)" @echo "Uploading release... $(VERSION_DEV)"

View File

@ -41,12 +41,16 @@ show_help() {
REMOTE_USER="root" REMOTE_USER="root"
REMOTE_PATH="/userdata/jetkvm/bin" REMOTE_PATH="/userdata/jetkvm/bin"
SKIP_UI_BUILD=false SKIP_UI_BUILD=false
SKIP_UI_BUILD_RELEASE=0
SKIP_NATIVE_BUILD=0 SKIP_NATIVE_BUILD=0
RESET_USB_HID_DEVICE=false RESET_USB_HID_DEVICE=false
LOG_TRACE_SCOPES="${LOG_TRACE_SCOPES:-jetkvm,cloud,websocket,native,jsonrpc}" LOG_TRACE_SCOPES="${LOG_TRACE_SCOPES:-jetkvm,cloud,websocket,native,jsonrpc}"
RUN_GO_TESTS=false RUN_GO_TESTS=false
RUN_GO_TESTS_ONLY=false RUN_GO_TESTS_ONLY=false
INSTALL_APP=false INSTALL_APP=false
BUILD_IN_DOCKER=false
DOCKER_BUILD_DEBUG=false
DOCKER_BUILD_TAG=ghcr.io/jetkvm/buildkit:latest
# Parse command line arguments # Parse command line arguments
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
@ -71,6 +75,14 @@ while [[ $# -gt 0 ]]; do
RESET_USB_HID_DEVICE=true RESET_USB_HID_DEVICE=true
shift shift
;; ;;
--build-in-docker)
BUILD_IN_DOCKER=true
shift
;;
--docker-build-debug)
DOCKER_BUILD_DEBUG=true
shift
;;
--run-go-tests) --run-go-tests)
RUN_GO_TESTS=true RUN_GO_TESTS=true
shift shift
@ -103,11 +115,59 @@ if [ -z "$REMOTE_HOST" ]; then
exit 1 exit 1
fi 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 # Build the development version on the host
# When using `make build_release`, the frontend will be built regardless of the `SKIP_UI_BUILD` flag # 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" msg_info "▶ Building frontend"
make frontend make frontend SKIP_UI_BUILD=0
SKIP_UI_BUILD_RELEASE=1
fi fi
if [ "$RUN_GO_TESTS" = true ]; then if [ "$RUN_GO_TESTS" = true ]; then
@ -155,7 +215,7 @@ fi
if [ "$INSTALL_APP" = true ] if [ "$INSTALL_APP" = true ]
then then
msg_info "▶ Building release binary" 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. # 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 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" ssh "${REMOTE_USER}@${REMOTE_HOST}" "reboot"
else else
msg_info "▶ Building development binary" 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 # Kill any existing instances of the application
ssh "${REMOTE_USER}@${REMOTE_HOST}" "killall jetkvm_app_debug || true" ssh "${REMOTE_USER}@${REMOTE_HOST}" "killall jetkvm_app_debug || true"