Skip to content

Commit 39719e6

Browse files
committed
refactor: split build and push to independent steps
1 parent a729ddc commit 39719e6

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
@@ -611,8 +611,6 @@ done
611611

612612
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
613613
patchBuildStep "${buildStartTime}" "${buildStartTime}" "${currentStepEnd}" "${NAMESPACE}" "registryLogin" "Container Registry Login" "false"
614-
previousStepEnd=${currentStepEnd}
615-
beginBuildStep "Image Builds" "buildingImages"
616614

617615

618616
##############################################
@@ -648,19 +646,23 @@ if [[ "$BUILD_TYPE" == "pullrequest" || "$BUILD_TYPE" == "branch" ]]; then
648646
echo "Pulling Image: ${FPI}"
649647
docker pull "${FPI}"
650648
done
651-
649+
HAS_PULLED_IMAGES=false
652650
# now we loop through the images in the build data and determine if they need to be pulled or build
653651
for IMAGE_BUILD_DATA in $(echo "$ENVIRONMENT_IMAGE_BUILD_DATA" | jq -c '.images[]')
654652
do
655653
SERVICE_NAME=$(echo "$IMAGE_BUILD_DATA" | jq -r '.name // false')
654+
SERVICE_NAME_TITLE=$(echo "$SERVICE_NAME" | tr '[:upper:]' '[:lower:]' | tr -d '-')
656655
DOCKERFILE=$(echo "$IMAGE_BUILD_DATA" | jq -r '.imageBuild.dockerFile // false')
657656
# if there is no dockerfile, then this image needs to be pulled from somewhere else
658657
if [ $DOCKERFILE == "false" ]; then
659658
PULL_IMAGE=$(echo "$IMAGE_BUILD_DATA" | jq -r '.imageBuild.pullImage // false')
660659
if [ "$PULL_IMAGE" != "false" ]; then
661660
IMAGES_PULL["${SERVICE_NAME}"]="${PULL_IMAGE}"
661+
HAS_PULLED_IMAGES=true
662662
fi
663663
else
664+
previousStepEnd=${currentStepEnd}
665+
beginBuildStep "Building Image ${SERVICE_NAME}" "buildingImage${SERVICE_NAME_TITLE}"
664666
# otherwise extract build information from the image build data payload
665667
# 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
666668
# built with the name they are meant to be. only 1 build can run at a time within a namespace
@@ -693,28 +695,50 @@ if [[ "$BUILD_TYPE" == "pullrequest" || "$BUILD_TYPE" == "branch" ]]; then
693695

694696
# adding the build image to the list of arguments passed into the next image builds
695697
SERVICE_NAME_UPPERCASE=$(echo "$SERVICE_NAME" | tr '[:lower:]' '[:upper:]')
698+
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
699+
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "imageBuild${SERVICE_NAME_TITLE}Complete" "Building Image ${SERVICE_NAME}" "false"
696700
fi
697701
done
698-
fi
699702

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

716-
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
717-
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "imageBuildComplete" "Image Builds" "false"
718742
previousStepEnd=${currentStepEnd}
719743
beginBuildStep "Service Configuration Phase 1" "serviceConfigurationPhase1"
720744

@@ -1207,8 +1231,6 @@ done
12071231

12081232
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
12091233
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "updateConfigmapComplete" "Update Configmap" "false"
1210-
previousStepEnd=${currentStepEnd}
1211-
beginBuildStep "Image Push to Registry" "pushingImages"
12121234

12131235
##############################################
12141236
### REDEPLOY DEPLOYMENTS IF CONFIG MAP CHANGES
@@ -1232,33 +1254,7 @@ declare -A DEPRECATED_IMAGE_SUGGESTION
12321254

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

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

12971326
# pullrequest/branch end
12981327
# promote start
12991328
elif [ "$BUILD_TYPE" == "promote" ]; then
13001329

13011330
for IMAGE_NAME in "${IMAGES[@]}"
13021331
do
1332+
previousStepEnd=${currentStepEnd}
1333+
beginBuildStep "Pushing Pulled Promote Image ${IMAGE_NAME}" "pushingImage${IMAGE_NAME}"
13031334
PUSH_IMAGE="${IMAGES_PUSH[${IMAGE_NAME}]}" #extract the push image name from the images to push list
13041335
PROMOTE_IMAGE="${IMAGES_PROMOTE[${IMAGE_NAME}]}" #extract the push image name from the images to push list
13051336
skopeo copy --retry-times 5 --src-tls-verify=false --dest-tls-verify=false docker://${PROMOTE_IMAGE} docker://${PUSH_IMAGE}
13061337

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

1312-
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
1313-
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "imagePushComplete" "Image Push to Registry" "false"
1314-
13151345
##############################################
13161346
### Check for deprecated images
13171347
##############################################

0 commit comments

Comments
 (0)