fix: inject linux-musl sharp binaries into build/ to fix 502 on Dokploy

build.sh runs npm run build on macOS, so only the host's sharp binary
(darwin-arm64) got traced into the standalone output. The container
runs node:20-alpine (Linux, musl libc), which cannot load that binary
- the server crashed at runtime, causing a 502 even though the docker
build itself succeeded.

npm pack can fetch another platform's prebuilt sharp/libvips tarballs
without an install-time OS check, so build.sh now pulls the
linux-musl x64 and arm64 builds directly into build/node_modules and
drops the now-unused host binary.
This commit is contained in:
2026-07-14 12:41:07 +05:30
parent 29e11189e8
commit 2eab221eff
551 changed files with 75742 additions and 22 deletions

View File

@@ -68,6 +68,48 @@ if [ ! -f "$BUILD_DIR/server.js" ] || [ ! -d "$BUILD_DIR/.next/static" ] || [ !
fi
echo "Artifact verified: build/server.js, build/.next/static, build/public all present."
# `sharp` ships platform-specific native binaries as optional dependencies.
# Since this build runs on the host machine (macOS), only the host's binary
# (e.g. darwin-arm64) gets installed/traced - it will NOT load inside the
# Linux (node:20-alpine, musl libc) container and crashes the server at
# runtime (surfaces as a 502 after an otherwise-successful docker build).
# `npm pack` can fetch another platform's prebuilt tarball without an
# install-time OS/arch check, so pull the linux-musl builds directly and
# drop them into node_modules alongside the host's.
if [ -d "$BUILD_DIR/node_modules/sharp" ]; then
echo
echo "Injecting linux-musl sharp binaries (container runs node:20-alpine)..."
SHARP_TMP="$(mktemp -d)"
trap 'rm -rf "$SHARP_TMP"' EXIT
SHARP_VERSION="$(node -p "require('$BUILD_DIR/node_modules/sharp/package.json').version")"
LIBVIPS_VERSION="$(node -p "require('$BUILD_DIR/node_modules/sharp/package.json').optionalDependencies['@img/sharp-libvips-linuxmusl-x64']")"
SHARP_PKGS=(
"@img/sharp-linuxmusl-x64@${SHARP_VERSION}"
"@img/sharp-libvips-linuxmusl-x64@${LIBVIPS_VERSION}"
"@img/sharp-linuxmusl-arm64@${SHARP_VERSION}"
"@img/sharp-libvips-linuxmusl-arm64@${LIBVIPS_VERSION}"
)
(cd "$SHARP_TMP" && npm pack "${SHARP_PKGS[@]}" >/dev/null)
for spec in "${SHARP_PKGS[@]}"; do
name="${spec%@*}"
version="${spec##*@}"
tarball_name="$(echo "${name#@}" | tr '/' '-')-${version}.tgz"
dest="$BUILD_DIR/node_modules/$name"
mkdir -p "$dest"
tar xzf "$SHARP_TMP/$tarball_name" -C "$dest" --strip-components=1
done
# The host's own platform binary is dead weight in the Linux container.
find "$BUILD_DIR/node_modules/@img" -maxdepth 1 -type d \( -name 'sharp-darwin-*' -o -name 'sharp-libvips-darwin-*' -o -name 'sharp-win32-*' -o -name 'sharp-libvips-win32-*' \) -exec rm -rf {} +
rm -rf "$SHARP_TMP"
trap - EXIT
echo "Linux-musl sharp binaries (x64 + arm64) installed; host platform binary removed."
fi
echo
echo "Source: $SOURCE_REPO ($SOURCE_BRANCH @ $SOURCE_HEAD)"
echo "Deploy: $DEPLOY_REPO ($DEPLOY_BRANCH @ $DEPLOY_HEAD_BEFORE)"