From ac7c19068ed0ef48245670ad9216c1f8ee8db460 Mon Sep 17 00:00:00 2001 From: Adam Warner Date: Sat, 5 Apr 2025 11:54:50 +0100 Subject: [PATCH] Make the `-l` flag clone and build FTL directly inside the image, rather than relying on the binary having already been build externally Signed-off-by: Adam Warner --- README.md | 2 +- build.sh | 32 +++++++------------------------- src/Dockerfile | 20 ++++++++++++++++---- 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index dd5d66e..dbca741 100644 --- a/README.md +++ b/README.md @@ -274,7 +274,7 @@ The preferred method is to clone this repository and build the image locally wit - `-w ` / `--webbranch `: Specify Web branch - `-p ` / `--paddbranch `: Specify PADD branch - `-t ` / `--tag `: Specify Docker image tag (default: `pihole:local`) -- `-l` / `--local`: Use locally built FTL binary (requires `src/pihole-FTL` file) +- `-l` / `--local`: Clones the FTL repository and builds the binary locally - `use_cache`: Enable caching (by default `--no-cache` is used) If no options are specified, the following command will be executed: diff --git a/build.sh b/build.sh index d98596a..f724552 100755 --- a/build.sh +++ b/build.sh @@ -4,12 +4,12 @@ usage() { echo "Usage: $0 [-l] [-f ] [-c ] [-w ] [-t ] [use_cache]" echo "Options:" - echo " -f, --ftlbranch Specify FTL branch (cannot be used in conjunction with -l)" + echo " -f, --ftlbranch Specify FTL branch" echo " -c, --corebranch Specify Core branch" echo " -w, --webbranch Specify Web branch" echo " -p, --paddbranch Specify PADD branch" echo " -t, --tag Specify Docker image tag (default: pihole:local)" - echo " -l, --local Use locally built FTL binary (requires src/pihole-FTL file)" + echo " -l, --local Clones the FTL repository and builds the binary locally" echo " use_cache Enable caching (by default --no-cache is used)" echo "" echo "If no options are specified, the following command will be executed:" @@ -20,11 +20,9 @@ usage() { # Set default values TAG="pihole:local" DOCKER_BUILD_CMD="docker buildx build src/. --tag ${TAG} --load --no-cache" -FTL_FLAG=false # Check if buildx is installed -docker buildx version >/dev/null 2>&1 -if [ $? -ne 0 ]; then +if ! docker buildx version >/dev/null 2>&1; then echo "Error: Docker buildx is required to build this image. For installation instructions, see:" echo " https://github.com/docker/buildx#installing" exit 1 @@ -56,25 +54,11 @@ while [[ $# -gt 0 ]]; do key="$1" case $key in - -l | --local) - if [ ! -f "src/pihole-FTL" ]; then - echo "File 'src/pihole-FTL' not found. Exiting." - exit 1 - fi - if [ "$FTL_FLAG" = true ]; then - echo "Error: Both -l and -f cannot be used together." - usage - fi - FTL_FLAG=true + -l | --local) DOCKER_BUILD_CMD+=" --build-arg FTL_SOURCE=local" shift ;; - -f | --ftlbranch) - if [ "$FTL_FLAG" = true ]; then - echo "Error: Both -l and -f cannot be used together." - usage - fi - FTL_FLAG=true + -f | --ftlbranch) FTL_BRANCH="$2" check_branch_exists "ftl" "$FTL_BRANCH" DOCKER_BUILD_CMD+=" --build-arg FTL_BRANCH=$FTL_BRANCH" @@ -122,10 +106,8 @@ done # Execute the docker build command echo "Executing command: $DOCKER_BUILD_CMD" -eval "${DOCKER_BUILD_CMD}" - -# Check exit code of previous command -if [ $? -ne 0 ]; then +# Execute the docker build command and check its exit status +if ! eval "${DOCKER_BUILD_CMD}"; then echo "" echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" echo "!! ERROR: Docker build failed, please review logs above !!" diff --git a/src/Dockerfile b/src/Dockerfile index 282b400..20e2b96 100644 --- a/src/Dockerfile +++ b/src/Dockerfile @@ -86,7 +86,7 @@ EXPOSE 80 EXPOSE 123/udp EXPOSE 443 -## Buildkit can do some fancy stuff and we can use it to either download FTL from ftl.pi-hole.net or use a local copy +## Buildkit can do some fancy stuff and we can use it to either download FTL from ftl.pi-hole.net or build a local copy FROM base AS remote-ftl-install # Default stage if FTL_SOURCE is not explicitly set to "local" @@ -106,10 +106,22 @@ RUN if [ "$TARGETPLATFORM" = "linux/amd64" ]; then FTLARCH=amd64; \ && readelf -h /usr/bin/pihole-FTL || (echo "Error with downloaded FTL binary" && exit 1) \ && /usr/bin/pihole-FTL -vv +# This is only used if FTL_SOURCE is set to "local" (which will in turn use the local-ftl-install stage) +FROM ghcr.io/pi-hole/ftl-build:v2.11 AS ftl-builder +# Needs to be defaulted to something, but it will be overridden by the build-arg anyway +ARG FTL_BRANCH="development" + +RUN git clone --depth 1 --single-branch --branch ${FTL_BRANCH} https://github.com/pi-hole/ftl.git /ftlbuild && \ + cd /ftlbuild && \ + rm src/args.c && \ + ./build.sh &&\ + # if the build fails, exit with and error code + if [ $? -ne 0 ]; then echo "FTL build failed" && exit 1; fi && \ + # check if the binary is valid + readelf -h /ftlbuild/pihole-FTL || (echo "Error with built FTL binary" && exit 1) + FROM base AS local-ftl-install -# pihole-FTL must be built from source and copied to the src directory first! -COPY --chmod=0755 pihole-FTL /usr/bin/pihole-FTL -RUN readelf -h /usr/bin/pihole-FTL || (echo "Error with local FTL binary" && exit 1) +COPY --from=ftl-builder --chmod=0755 /ftlbuild/pihole-FTL /usr/bin/pihole-FTL # Use the appropriate FTL Install stage based on the FTL_SOURCE build-arg FROM ${FTL_SOURCE}-ftl-install AS final