Skip to content

Commit 1aea8a2

Browse files
committed
refactor: split build and push to independent steps
1 parent 72bcaf0 commit 1aea8a2

File tree

1 file changed

+82
-52
lines changed

1 file changed

+82
-52
lines changed

legacy/build-deploy-docker-compose.sh

Lines changed: 82 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,6 @@ done
616616

617617
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
618618
patchBuildStep "${buildStartTime}" "${buildStartTime}" "${currentStepEnd}" "${NAMESPACE}" "registryLogin" "Container Registry Login" "false"
619-
previousStepEnd=${currentStepEnd}
620-
beginBuildStep "Image Builds" "buildingImages"
621619

622620

623621
##############################################
@@ -653,19 +651,23 @@ if [[ "$BUILD_TYPE" == "pullrequest" || "$BUILD_TYPE" == "branch" ]]; then
653651
echo "Pulling Image: ${FPI}"
654652
docker pull "${FPI}"
655653
done
656-
654+
HAS_PULLED_IMAGES=false
657655
# now we loop through the images in the build data and determine if they need to be pulled or build
658656
for IMAGE_BUILD_DATA in $(echo "$ENVIRONMENT_IMAGE_BUILD_DATA" | jq -c '.images[]')
659657
do
660658
SERVICE_NAME=$(echo "$IMAGE_BUILD_DATA" | jq -r '.name // false')
659+
SERVICE_NAME_TITLE=$(echo "$SERVICE_NAME" | tr '[:upper:]' '[:lower:]' | tr -d '-')
661660
DOCKERFILE=$(echo "$IMAGE_BUILD_DATA" | jq -r '.imageBuild.dockerFile // false')
662661
# if there is no dockerfile, then this image needs to be pulled from somewhere else
663662
if [ $DOCKERFILE == "false" ]; then
664663
PULL_IMAGE=$(echo "$IMAGE_BUILD_DATA" | jq -r '.imageBuild.pullImage // false')
665664
if [ "$PULL_IMAGE" != "false" ]; then
666665
IMAGES_PULL["${SERVICE_NAME}"]="${PULL_IMAGE}"
666+
HAS_PULLED_IMAGES=true
667667
fi
668668
else
669+
previousStepEnd=${currentStepEnd}
670+
beginBuildStep "Building Image ${SERVICE_NAME}" "buildingImage${SERVICE_NAME_TITLE}"
669671
# otherwise extract build information from the image build data payload
670672
# this is a temporary image name to use for the build, it is based on the namespace and service, this can probably be deprecated and the images could just be
671673
# built with the name they are meant to be. only 1 build can run at a time within a namespace
@@ -698,28 +700,50 @@ if [[ "$BUILD_TYPE" == "pullrequest" || "$BUILD_TYPE" == "branch" ]]; then
698700

699701
# adding the build image to the list of arguments passed into the next image builds
700702
SERVICE_NAME_UPPERCASE=$(echo "$SERVICE_NAME" | tr '[:lower:]' '[:upper:]')
703+
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
704+
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "imageBuild${SERVICE_NAME_TITLE}Complete" "Building Image ${SERVICE_NAME}" "false"
701705
fi
702706
done
703-
fi
704707

705-
# print information about built image sizes
706-
function printBytes {
707-
local -i bytes=$1;
708-
echo "$(( (bytes + 1000000)/1000000 ))MB"
709-
}
710-
if [[ "${IMAGES_BUILD[@]}" ]]; then
711-
echo "##############################################"
712-
echo "Built image sizes:"
713-
echo "##############################################"
708+
if [ $HAS_PULLED_IMAGES == "true" ]; then
709+
previousStepEnd=${currentStepEnd}
710+
beginBuildStep "Pulled Images" "pulledImageInfo"
711+
echo "The following images will be pulled and then pushed directly with no building required"
712+
for IMAGE_NAME in "${!IMAGES_PULL[@]}"
713+
do
714+
PULL_IMAGE="${IMAGES_PULL[${IMAGE_NAME}]}" #extract the pull image name from the images to pull list
715+
echo "- ${IMAGE_NAME}: ${PULL_IMAGE}"
716+
done
717+
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
718+
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "pulledImageInfoComplete" "Pulled Images" "false"
719+
fi
720+
721+
previousStepEnd=${currentStepEnd}
722+
beginBuildStep "Image Build Stats" "imageBuildStats"
723+
724+
# print information about built image sizes
725+
function printBytes {
726+
local -i bytes=$1;
727+
echo "$(( (bytes + 1000000)/1000000 ))MB"
728+
}
729+
if [[ "${IMAGES_BUILD[@]}" ]]; then
730+
echo "##############################################"
731+
echo "Built image sizes:"
732+
echo "##############################################"
733+
fi
734+
for IMAGE_NAME in "${!IMAGES_BUILD[@]}"
735+
do
736+
TEMPORARY_IMAGE_NAME="${IMAGES_BUILD[${IMAGE_NAME}]}"
737+
echo -e "- image: ${TEMPORARY_IMAGE_NAME}\t\t$(printBytes $(docker inspect ${TEMPORARY_IMAGE_NAME} | jq -r '.[0].Size'))"
738+
done
739+
740+
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
741+
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "imageBuildStatsComplete" "Image Build Stats" "false"
742+
fi
743+
if [[ "$BUILD_TYPE" == "promote" ]]; then
744+
echo "No images built for promote environments"
714745
fi
715-
for IMAGE_NAME in "${!IMAGES_BUILD[@]}"
716-
do
717-
TEMPORARY_IMAGE_NAME="${IMAGES_BUILD[${IMAGE_NAME}]}"
718-
echo -e "Image ${TEMPORARY_IMAGE_NAME}\t\t$(printBytes $(docker inspect ${TEMPORARY_IMAGE_NAME} | jq -r '.[0].Size'))"
719-
done
720746

721-
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
722-
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "imageBuildComplete" "Image Builds" "false"
723747
previousStepEnd=${currentStepEnd}
724748
beginBuildStep "Service Configuration Phase 1" "serviceConfigurationPhase1"
725749

@@ -1212,8 +1236,6 @@ done
12121236

12131237
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
12141238
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "updateConfigmapComplete" "Update Configmap" "false"
1215-
previousStepEnd=${currentStepEnd}
1216-
beginBuildStep "Image Push to Registry" "pushingImages"
12171239

12181240
##############################################
12191241
### REDEPLOY DEPLOYMENTS IF CONFIG MAP CHANGES
@@ -1237,33 +1259,7 @@ declare -A DEPRECATED_IMAGE_SUGGESTION
12371259

12381260
# pullrequest/branch start
12391261
if [ "$BUILD_TYPE" == "pullrequest" ] || [ "$BUILD_TYPE" == "branch" ]; then
1240-
1241-
# All images that should be pulled are copied to the harbor registry
1242-
for IMAGE_NAME in "${!IMAGES_PULL[@]}"
1243-
do
1244-
PULL_IMAGE="${IMAGES_PULL[${IMAGE_NAME}]}" #extract the pull image name from the images to pull list
1245-
PUSH_IMAGE="${IMAGES_PUSH[${IMAGE_NAME}]}" #extract the push image name from the images to push list
1246-
1247-
# Try to handle private registries first
1248-
1249-
# the external pull image name is all calculated in the build-deploy tool now, it knows how to calculate it
1250-
# from being a promote image, or an image from an imagecache or from some other registry entirely
1251-
skopeo copy --retry-times 5 --dest-tls-verify=false docker://${PULL_IMAGE} docker://${PUSH_IMAGE}
1252-
1253-
# store the resulting image hash
1254-
SKOPEO_INSPECT=$(skopeo inspect --retry-times 5 docker://${PUSH_IMAGE} --tls-verify=false)
1255-
IMAGE_HASHES[${IMAGE_NAME}]=$(echo "${SKOPEO_INSPECT}" | jq ".Name + \"@\" + .Digest" -r)
1256-
1257-
# check if the pull through image is deprecated
1258-
DEPRECATED_STATUS=$(echo "${SKOPEO_INSPECT}" | jq -r '.Labels."sh.lagoon.image.deprecated.status" // false')
1259-
if [ "${DEPRECATED_STATUS}" != false ]; then
1260-
DEPRECATED_IMAGE_WARNINGS="true"
1261-
DEPRECATED_IMAGE_NAME[${IMAGE_NAME}]=${PULL_IMAGE#$IMAGECACHE_REGISTRY}
1262-
DEPRECATED_IMAGE_STATUS[${IMAGE_NAME}]=$DEPRECATED_STATUS
1263-
DEPRECATED_IMAGE_SUGGESTION[${IMAGE_NAME}]=$(echo "${SKOPEO_INSPECT}" | jq -r '.Labels."sh.lagoon.image.deprecated.suggested" | sub("docker.io\/";"")? // false')
1264-
fi
1265-
done
1266-
1262+
# calculate the images required to be pushed to the registry
12671263
for IMAGE_NAME in "${!IMAGES_BUILD[@]}"
12681264
do
12691265
PUSH_IMAGE="${IMAGES_PUSH[${IMAGE_NAME}]}" #extract the push image name from the images to push list
@@ -1286,6 +1282,8 @@ if [ "$BUILD_TYPE" == "pullrequest" ] || [ "$BUILD_TYPE" == "branch" ]; then
12861282
fi
12871283
done
12881284

1285+
previousStepEnd=${currentStepEnd}
1286+
beginBuildStep "Pushing Images" "pushingImages"
12891287
# If we have images to push to the registry, let's do so
12901288
if [ -f /kubectl-build-deploy/lagoon/push ]; then
12911289
parallel --retries 4 < /kubectl-build-deploy/lagoon/push
@@ -1298,25 +1296,57 @@ if [ "$BUILD_TYPE" == "pullrequest" ] || [ "$BUILD_TYPE" == "branch" ]; then
12981296
JQ_QUERY=(jq -r ".[]|select(test(\"${REGISTRY}/${PROJECT}/${ENVIRONMENT}/${IMAGE_NAME}@\"))")
12991297
IMAGE_HASHES[${IMAGE_NAME}]=$(docker inspect ${PUSH_IMAGE} --format '{{json .RepoDigests}}' | "${JQ_QUERY[@]}")
13001298
done
1299+
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
1300+
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "pushingImagesComplete" "Pushing Images" "false"
1301+
1302+
# All images that should be pulled are copied to the harbor registry
1303+
for IMAGE_NAME in "${!IMAGES_PULL[@]}"
1304+
do
1305+
PULL_IMAGE="${IMAGES_PULL[${IMAGE_NAME}]}" #extract the pull image name from the images to pull list
1306+
PUSH_IMAGE="${IMAGES_PUSH[${IMAGE_NAME}]}" #extract the push image name from the images to push list
1307+
1308+
# Try to handle private registries first
1309+
previousStepEnd=${currentStepEnd}
1310+
beginBuildStep "Pushing Pulled Image ${IMAGE_NAME}" "pushingImage${IMAGE_NAME}"
1311+
# the external pull image name is all calculated in the build-deploy tool now, it knows how to calculate it
1312+
# from being a promote image, or an image from an imagecache or from some other registry entirely
1313+
skopeo copy --retry-times 5 --dest-tls-verify=false docker://${PULL_IMAGE} docker://${PUSH_IMAGE}
1314+
1315+
# store the resulting image hash
1316+
SKOPEO_INSPECT=$(skopeo inspect --retry-times 5 docker://${PUSH_IMAGE} --tls-verify=false)
1317+
IMAGE_HASHES[${IMAGE_NAME}]=$(echo "${SKOPEO_INSPECT}" | jq ".Name + \"@\" + .Digest" -r)
1318+
1319+
# check if the pull through image is deprecated
1320+
DEPRECATED_STATUS=$(echo "${SKOPEO_INSPECT}" | jq -r '.Labels."sh.lagoon.image.deprecated.status" // false')
1321+
if [ "${DEPRECATED_STATUS}" != false ]; then
1322+
DEPRECATED_IMAGE_WARNINGS="true"
1323+
DEPRECATED_IMAGE_NAME[${IMAGE_NAME}]=${PULL_IMAGE#$IMAGECACHE_REGISTRY}
1324+
DEPRECATED_IMAGE_STATUS[${IMAGE_NAME}]=$DEPRECATED_STATUS
1325+
DEPRECATED_IMAGE_SUGGESTION[${IMAGE_NAME}]=$(echo "${SKOPEO_INSPECT}" | jq -r '.Labels."sh.lagoon.image.deprecated.suggested" | sub("docker.io\/";"")? // false')
1326+
fi
1327+
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
1328+
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "pushingImage${IMAGE_NAME}Complete" "Pushing Pulled Image ${IMAGE_NAME}" "false"
1329+
done
13011330

13021331
# pullrequest/branch end
13031332
# promote start
13041333
elif [ "$BUILD_TYPE" == "promote" ]; then
13051334

13061335
for IMAGE_NAME in "${IMAGES[@]}"
13071336
do
1337+
previousStepEnd=${currentStepEnd}
1338+
beginBuildStep "Pushing Pulled Promote Image ${IMAGE_NAME}" "pushingImage${IMAGE_NAME}"
13081339
PUSH_IMAGE="${IMAGES_PUSH[${IMAGE_NAME}]}" #extract the push image name from the images to push list
13091340
PROMOTE_IMAGE="${IMAGES_PROMOTE[${IMAGE_NAME}]}" #extract the push image name from the images to push list
13101341
skopeo copy --retry-times 5 --src-tls-verify=false --dest-tls-verify=false docker://${PROMOTE_IMAGE} docker://${PUSH_IMAGE}
13111342

13121343
IMAGE_HASHES[${IMAGE_NAME}]=$(skopeo inspect --retry-times 5 docker://${PUSH_IMAGE} --tls-verify=false | jq ".Name + \"@\" + .Digest" -r)
1344+
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
1345+
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "pushingImage${IMAGE_NAME}Complete" "Pushing Pulled Promote Image ${IMAGE_NAME}" "false"
13131346
done
13141347
# promote end
13151348
fi
13161349

1317-
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
1318-
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "imagePushComplete" "Image Push to Registry" "false"
1319-
13201350
##############################################
13211351
### Check for deprecated images
13221352
##############################################

0 commit comments

Comments
 (0)