From 1021c971fd95a1b5198b5be54fbf9952c4168739 Mon Sep 17 00:00:00 2001 From: Toby Bellwood Date: Tue, 8 Oct 2024 10:09:56 +1100 Subject: [PATCH 1/2] chore: initial DOCKER_PROJECT_BUILDER --- Dockerfile | 2 +- legacy/build-deploy-docker-compose.sh | 34 +++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 56433b36..9152ace3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -41,7 +41,7 @@ RUN --mount=type=cache,target=/root/.cache/go-build \ # RUN go mod download # RUN go build -o /app/build-deploy-tool -FROM docker:27.1.1-alpine3.20 +FROM docker:27.3.1-alpine3.20 LABEL org.opencontainers.image.authors="The Lagoon Authors" maintainer="The Lagoon Authors" LABEL org.opencontainers.image.source="https://github.com/uselagoon/build-deploy-tool" repository="https://github.com/uselagoon/build-deploy-tool" diff --git a/legacy/build-deploy-docker-compose.sh b/legacy/build-deploy-docker-compose.sh index 60d88357..f92dd8ff 100755 --- a/legacy/build-deploy-docker-compose.sh +++ b/legacy/build-deploy-docker-compose.sh @@ -650,13 +650,43 @@ if [[ "$BUILD_TYPE" == "pullrequest" || "$BUILD_TYPE" == "branch" ]]; then echo "Using BuildKit for $DOCKERFILE" fi + # Decide whether to use a dedicated project builder for Docker builds - disabled by default. + DOCKER_PROJECT_BUILDER=default + + if [ ! -z "$LAGOON_PROJECT_VARIABLES" ]; then + DOCKER_PROJECT_BUILDER=($(echo $LAGOON_PROJECT_VARIABLES | jq -r '.[] | select(.scope == "build") | select(.name == "DOCKER_PROJECT_BUILDER") | "\(.value)"')) + fi + if [ ! -z "$LAGOON_ENVIRONMENT_VARIABLES" ]; then + TEMP_DOCKER_PROJECT_BUILDER=($(echo $LAGOON_ENVIRONMENT_VARIABLES | jq -r '.[] | select(.scope == "build") | select(.name == "DOCKER_PROJECT_BUILDER") | "\(.value)"')) + if [ ! -z $TEMP_DOCKER_PROJECT_BUILDER ]; then + DOCKER_PROJECT_BUILDER=$TEMP_DOCKER_PROJECT_BUILDER + fi + fi + + case "$DOCKER_PROJECT_BUILDER" in + default|Default|False|false|0) + DOCKER_PROJECT_BUILDER=default + echo "Using default builder for $DOCKERFILE"; + ;; + *) + echo "Using dedicated builder $PROJECT for $DOCKERFILE"; + DOCKER_PROJECT_BUILDER=${PROJECT} + docker builder create --name $DOCKER_PROJECT_BUILDER --driver docker-container || true + ;; + esac + set -x + # now do the actual image build if [ $BUILD_TARGET == "false" ]; then echo "Building ${BUILD_CONTEXT}/${DOCKERFILE}" - DOCKER_BUILDKIT=$DOCKER_BUILDKIT docker build --network=host "${BUILD_ARGS[@]}" -t $TEMPORARY_IMAGE_NAME -f $BUILD_CONTEXT/$DOCKERFILE $BUILD_CONTEXT + if [ $DOCKER_BUILDKIT -eq 0 ]; then + DOCKER_BUILDKIT=$DOCKER_BUILDKIT docker build --network=host "${BUILD_ARGS[@]}" -t $TEMPORARY_IMAGE_NAME -f $BUILD_CONTEXT/$DOCKERFILE $BUILD_CONTEXT + else + DOCKER_BUILDKIT=$DOCKER_BUILDKIT docker build --network=host --builder $DOCKER_PROJECT_BUILDER --load "${BUILD_ARGS[@]}" -t $TEMPORARY_IMAGE_NAME -f $BUILD_CONTEXT/$DOCKERFILE $BUILD_CONTEXT + fi else echo "Building target ${BUILD_TARGET} for ${BUILD_CONTEXT}/${DOCKERFILE}" - DOCKER_BUILDKIT=$DOCKER_BUILDKIT docker build --network=host "${BUILD_ARGS[@]}" -t $TEMPORARY_IMAGE_NAME -f $BUILD_CONTEXT/$DOCKERFILE --target $BUILD_TARGET $BUILD_CONTEXT + DOCKER_BUILDKIT=$DOCKER_BUILDKIT docker build --network=host --builder $DOCKER_PROJECT_BUILDER --load "${BUILD_ARGS[@]}" -t $TEMPORARY_IMAGE_NAME -f $BUILD_CONTEXT/$DOCKERFILE --target $BUILD_TARGET $BUILD_CONTEXT fi # Keep a list of the images we have built, as we need to push them to the registry later From 54230e5a5892e2912328187f1a9a350d2cd1b525 Mon Sep 17 00:00:00 2001 From: Toby Bellwood Date: Tue, 8 Oct 2024 11:30:17 +1100 Subject: [PATCH 2/2] chore: rewrite var check and add stop --- legacy/build-deploy-docker-compose.sh | 28 +++++++-------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/legacy/build-deploy-docker-compose.sh b/legacy/build-deploy-docker-compose.sh index f92dd8ff..535198cd 100755 --- a/legacy/build-deploy-docker-compose.sh +++ b/legacy/build-deploy-docker-compose.sh @@ -652,29 +652,10 @@ if [[ "$BUILD_TYPE" == "pullrequest" || "$BUILD_TYPE" == "branch" ]]; then # Decide whether to use a dedicated project builder for Docker builds - disabled by default. DOCKER_PROJECT_BUILDER=default - - if [ ! -z "$LAGOON_PROJECT_VARIABLES" ]; then - DOCKER_PROJECT_BUILDER=($(echo $LAGOON_PROJECT_VARIABLES | jq -r '.[] | select(.scope == "build") | select(.name == "DOCKER_PROJECT_BUILDER") | "\(.value)"')) - fi - if [ ! -z "$LAGOON_ENVIRONMENT_VARIABLES" ]; then - TEMP_DOCKER_PROJECT_BUILDER=($(echo $LAGOON_ENVIRONMENT_VARIABLES | jq -r '.[] | select(.scope == "build") | select(.name == "DOCKER_PROJECT_BUILDER") | "\(.value)"')) - if [ ! -z $TEMP_DOCKER_PROJECT_BUILDER ]; then - DOCKER_PROJECT_BUILDER=$TEMP_DOCKER_PROJECT_BUILDER - fi - fi - - case "$DOCKER_PROJECT_BUILDER" in - default|Default|False|false|0) - DOCKER_PROJECT_BUILDER=default - echo "Using default builder for $DOCKERFILE"; - ;; - *) - echo "Using dedicated builder $PROJECT for $DOCKERFILE"; + if [ "$(featureFlag DOCKER_PROJECT_BUILDER)" = enabled ]; then DOCKER_PROJECT_BUILDER=${PROJECT} docker builder create --name $DOCKER_PROJECT_BUILDER --driver docker-container || true - ;; - esac - set -x + fi # now do the actual image build if [ $BUILD_TARGET == "false" ]; then @@ -698,6 +679,11 @@ if [[ "$BUILD_TYPE" == "pullrequest" || "$BUILD_TYPE" == "branch" ]]; then done fi +if [ "$(featureFlag DOCKER_PROJECT_BUILDER)" = enabled ]; then + echo "stopping builder $DOCKER_PROJECT_BUILDER" + docker builder stop --builder $DOCKER_PROJECT_BUILDER +fi + # print information about built image sizes function printBytes { local -i bytes=$1;