From 3040bd41d9eb72da25fd89030fa9a7a0765ebc35 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Mon, 12 Dec 2022 15:42:00 +0800 Subject: [PATCH] Speed up armv7 build time of healthcheck by using go compiler cross-build feature in the host --- .dockerignore | 3 +++ .gitignore | 1 + docker/dockerfile | 18 +++++++++++------- extra/build-healthcheck.js | 27 +++++++++++++++++++++++++++ package.json | 3 ++- 5 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 extra/build-healthcheck.js diff --git a/.dockerignore b/.dockerignore index babc429a2..47e82a105 100644 --- a/.dockerignore +++ b/.dockerignore @@ -31,6 +31,9 @@ tsconfig.json /tmp /babel.config.js /ecosystem.config.js +/extra/healthcheck.exe +/extra/healthcheck + ### .gitignore content (commented rules are duplicated) diff --git a/.gitignore b/.gitignore index 78e1a9656..06dca04b4 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ cypress/screenshots /extra/healthcheck.exe /extra/healthcheck +/extra/healthcheck-armv7 diff --git a/docker/dockerfile b/docker/dockerfile index 6657e681f..e084d649f 100644 --- a/docker/dockerfile +++ b/docker/dockerfile @@ -1,15 +1,19 @@ ############################################ # Build in Golang -# Super slow for armv7 (compile time = 1200 seconds, but other platforms within 5 seconds). -# Do not change everything here, so it should always use the cache. +# Run npm run build-healthcheck-armv7 in the host first, another it will be super slow where it is building the armv7 healthcheck ############################################ FROM golang:1.19.4-buster AS build_healthcheck WORKDIR /app - -COPY ./extra/healthcheck.go ./extra/healthcheck.go +ARG TARGETPLATFORM +COPY ./extra/ ./extra/ # Compile healthcheck.go -RUN go build -x -o ./extra/healthcheck ./extra/healthcheck.go +RUN apt update +RUN apt --yes --no-install-recommends install curl +RUN curl -sL https://deb.nodesource.com/setup_18.x | bash +RUN apt --yes --no-install-recommends install nodejs +RUN node -v +RUN node ./extra/build-healthcheck.js $TARGETPLATFORM ############################################ # Build in Node.js @@ -18,8 +22,8 @@ FROM louislam/uptime-kuma:base-debian AS build WORKDIR /app ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=1 - COPY . . +COPY --from=build_healthcheck /app/extra/healthcheck /app/extra/healthcheck RUN npm ci --production && \ chmod +x /app/extra/entrypoint.sh @@ -31,7 +35,7 @@ WORKDIR /app # Copy app files from build layer COPY --from=build /app /app -COPY --from=build_healthcheck /app/extra/healthcheck /app/extra/healthcheck + EXPOSE 3001 VOLUME ["/app/data"] diff --git a/extra/build-healthcheck.js b/extra/build-healthcheck.js new file mode 100644 index 000000000..e4c8026ab --- /dev/null +++ b/extra/build-healthcheck.js @@ -0,0 +1,27 @@ +const childProcess = require("child_process"); +const fs = require("fs"); +const platform = process.argv[2]; + +if (!platform) { + console.error("No platform??"); + process.exit(1); +} + +if (platform === "linux/arm/v7") { + console.log("Arch: armv7"); + if (fs.existsSync("./extra/healthcheck-armv7")) { + fs.renameSync("./extra/healthcheck-armv7", "./extra/healthcheck"); + console.log("Already built in the host, skip."); + process.exit(0); + } else { + console.log("prebuilt not found, it will be slow! You should execute `npm run build-healthcheck-armv7` before build."); + } +} else { + if (fs.existsSync("./extra/healthcheck-armv7")) { + fs.rmSync("./extra/healthcheck-armv7"); + } +} + +const output = childProcess.execSync("go build -x -o ./extra/healthcheck ./extra/healthcheck.go").toString("utf8"); +console.log(output); + diff --git a/package.json b/package.json index f43c21d41..0a0a7aedb 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,8 @@ "start-pr-test": "node extra/checkout-pr.js && npm install && npm run dev", "cy:test": "node test/prepare-test-server.js && node server/server.js --port=3002 --data-dir=./data/test/ --e2e", "cy:run": "npx cypress run --browser chrome --headless --config-file ./config/cypress.config.js", - "cypress-open": "concurrently -k -r \"node test/prepare-test-server.js && node server/server.js --port=3002 --data-dir=./data/test/\" \"cypress open --config-file ./config/cypress.config.js\"" + "cypress-open": "concurrently -k -r \"node test/prepare-test-server.js && node server/server.js --port=3002 --data-dir=./data/test/\" \"cypress open --config-file ./config/cypress.config.js\"", + "build-healthcheck-armv7": "cross-env GOOS=linux GOARCH=arm GOARM=7 go build -x -o ./extra/healthcheck-armv7 ./extra/healthcheck.go" }, "dependencies": { "@grpc/grpc-js": "~1.7.3",