Skip to content

Commit

Permalink
refactor(build-scripts): consolidate image build and push scripts
Browse files Browse the repository at this point in the history
- Replaced with a single `build-and-push-images.sh` script.
  - Support for both Spin and Docker build workflows with the `--spin`, `--docker`, and `--both` options.
- Added parallel processing for building and pushing images.

Signed-off-by: Jiaxiao (mossaka) Zhou <[email protected]>
  • Loading branch information
Mossaka committed Dec 16, 2024
1 parent 03c3bb0 commit 7ca54a8
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 76 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ tests/collect-debug-logs:
install-k3s:
./scripts/install-k3s.sh
build-and-push-images:
./tests/k3s/build-and-push-images.sh
./scripts/build-and-push-images.sh --spin

.PHONY: test/k3s
test/k3s: install-k3s build-and-push-images
Expand Down Expand Up @@ -132,7 +132,7 @@ run-%: install load
.PHONY: up move-bins deploy-workloads-pushed-using-docker-build-push deploy-workloads-pushed-using-spin-registry-push pod-terminates-test prepare-cluster-and-images

up:
./scripts/up.sh
./scripts/k3d-up.sh

move-bins:
./scripts/move-bins.sh $(BIN_DIR)
Expand Down
69 changes: 69 additions & 0 deletions scripts/build-and-push-images.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/bash

set -euo pipefail

RUN_SPIN=false
RUN_DOCKER=false

cluster_name="test-cluster"
OUT_DIRS=("test/out_spin" "test/out_spin_keyvalue" "test/out_spin_outbound_redis" "test/out_spin_multi_trigger_app" "test/out_spin_static_assets" "test/out_spin_mqtt_message_logger")
IMAGES=("spin-hello-world" "spin-keyvalue" "spin-outbound-redis" "spin-multi-trigger-app" "spin-static-assets" "spin-mqtt-message-logger")


spin_build_and_push() {
local i=$1
spin build -f "./images/${IMAGES[$i]}/spin.toml"
if [ "${IMAGES[$i]}" == "spin-static-assets" ]; then
export SPIN_OCI_ARCHIVE_LAYERS=1
fi
spin registry push "localhost:5000/spin-registry-push/${IMAGES[$i]}:latest" -f "./images/${IMAGES[$i]}/spin.toml" -k
}

docker_build_and_push() {
local image="$1"
local out_dir="$2"

docker buildx build -t "${image}:latest" "./images/${image}" --load
mkdir -p "${out_dir}"
docker save -o "${out_dir}/img.tar" "${image}:latest"
k3d image import "${out_dir}/img.tar" -c "$cluster_name"
}

while [[ "$#" -gt 0 ]]; do
case "$1" in
--spin) RUN_SPIN=true ;;
--docker) RUN_DOCKER=true ;;
--both) RUN_SPIN=true; RUN_DOCKER=true ;;
*) echo "Unknown option: $1"; exit 1 ;;
esac
shift
done

if ! $RUN_SPIN && ! $RUN_DOCKER; then
echo "Error: At least one of --spin, --docker, or --both must be specified."
exit 1
fi


if $RUN_SPIN; then
echo "Running Spin builds and pushes..."
if ! docker ps | grep -q test-registry; then
docker run -d -p 5000:5000 --name test-registry registry:2
fi
for i in "${!IMAGES[@]}"; do
spin_build_and_push "$i" &
done
fi

if $RUN_DOCKER; then
echo "Running Docker builds and pushes..."
for i in "${!IMAGES[@]}"; do
docker_build_and_push "${IMAGES[$i]}" "${OUT_DIRS[$i]}" &
done
fi

# Wait for all background jobs to finish
wait

sleep 5
echo "Images are ready"
21 changes: 21 additions & 0 deletions scripts/k3d-up.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

set -euo pipefail

cluster_name="test-cluster"
dockerfile_path="deployments/k3d"

docker build -t k3d-shim-test "$dockerfile_path"

k3d cluster create "$cluster_name" \
--image k3d-shim-test --api-port 6551 -p '8082:80@loadbalancer' --agents 2 \
--registry-create test-registry:0.0.0.0:5000 \
--k3s-arg '--kubelet-arg=eviction-hard=imagefs.available<1%,nodefs.available<1%@agent:*' \
--k3s-arg '--kubelet-arg=eviction-minimum-reclaim=imagefs.available=1%,nodefs.available=1%@agent:*'

kubectl wait --for=condition=ready node --all --timeout=120s

echo "Running Spin and Docker builds and pushes..."
./scripts/spin-build-and-push-images.sh --both

echo ">>> Cluster setup and image builds/pushes complete!"
43 changes: 0 additions & 43 deletions scripts/up.sh

This file was deleted.

31 changes: 0 additions & 31 deletions tests/k3s/build-and-push-images.sh

This file was deleted.

0 comments on commit 7ca54a8

Please sign in to comment.