Skip to content

Commit bc011ef

Browse files
committed
refactor(build-scripts): consolidate image build and push scripts
- 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]>
1 parent 8b10035 commit bc011ef

File tree

5 files changed

+92
-76
lines changed

5 files changed

+92
-76
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ tests/collect-debug-logs:
7373
install-k3s:
7474
./scripts/install-k3s.sh
7575
build-and-push-images:
76-
./tests/k3s/build-and-push-images.sh
76+
./scripts/build-and-push-images.sh --spin
7777

7878
.PHONY: test/k3s
7979
test/k3s: install-k3s build-and-push-images
@@ -132,7 +132,7 @@ run-%: install load
132132
.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
133133

134134
up:
135-
./scripts/up.sh
135+
./scripts/k3d-up.sh
136136

137137
move-bins:
138138
./scripts/move-bins.sh $(BIN_DIR)

scripts/build-and-push-images.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
RUN_SPIN=false
6+
RUN_DOCKER=false
7+
8+
cluster_name="test-cluster"
9+
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")
10+
IMAGES=("spin-hello-world" "spin-keyvalue" "spin-outbound-redis" "spin-multi-trigger-app" "spin-static-assets" "spin-mqtt-message-logger")
11+
12+
13+
spin_build_and_push() {
14+
local i=$1
15+
spin build -f "./images/${IMAGES[$i]}/spin.toml"
16+
if [ "${IMAGES[$i]}" == "spin-static-assets" ]; then
17+
export SPIN_OCI_ARCHIVE_LAYERS=1
18+
fi
19+
spin registry push "localhost:5000/spin-registry-push/${IMAGES[$i]}:latest" -f "./images/${IMAGES[$i]}/spin.toml" -k
20+
}
21+
22+
docker_build_and_push() {
23+
local image="$1"
24+
local out_dir="$2"
25+
26+
docker buildx build -t "${image}:latest" "./images/${image}" --load
27+
mkdir -p "${out_dir}"
28+
docker save -o "${out_dir}/img.tar" "${image}:latest"
29+
k3d image import "${out_dir}/img.tar" -c "$cluster_name"
30+
}
31+
32+
while [[ "$#" -gt 0 ]]; do
33+
case "$1" in
34+
--spin) RUN_SPIN=true ;;
35+
--docker) RUN_DOCKER=true ;;
36+
--both) RUN_SPIN=true; RUN_DOCKER=true ;;
37+
*) echo "Unknown option: $1"; exit 1 ;;
38+
esac
39+
shift
40+
done
41+
42+
if ! $RUN_SPIN && ! $RUN_DOCKER; then
43+
echo "Error: At least one of --spin, --docker, or --both must be specified."
44+
exit 1
45+
fi
46+
47+
48+
if $RUN_SPIN; then
49+
echo "Running Spin builds and pushes..."
50+
if ! docker ps | grep -q test-registry; then
51+
docker run -d -p 5000:5000 --name test-registry registry:2
52+
fi
53+
for i in "${!IMAGES[@]}"; do
54+
spin_build_and_push "$i" &
55+
done
56+
fi
57+
58+
if $RUN_DOCKER; then
59+
echo "Running Docker builds and pushes..."
60+
for i in "${!IMAGES[@]}"; do
61+
docker_build_and_push "${IMAGES[$i]}" "${OUT_DIRS[$i]}" &
62+
done
63+
fi
64+
65+
# Wait for all background jobs to finish
66+
wait
67+
68+
sleep 5
69+
echo "Images are ready"

scripts/k3d-up.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
cluster_name="test-cluster"
6+
dockerfile_path="deployments/k3d"
7+
8+
docker build -t k3d-shim-test "$dockerfile_path"
9+
10+
k3d cluster create "$cluster_name" \
11+
--image k3d-shim-test --api-port 6551 -p '8082:80@loadbalancer' --agents 2 \
12+
--registry-create test-registry:0.0.0.0:5000 \
13+
--k3s-arg '--kubelet-arg=eviction-hard=imagefs.available<1%,nodefs.available<1%@agent:*' \
14+
--k3s-arg '--kubelet-arg=eviction-minimum-reclaim=imagefs.available=1%,nodefs.available=1%@agent:*'
15+
16+
kubectl wait --for=condition=ready node --all --timeout=120s
17+
18+
echo "Running Spin and Docker builds and pushes..."
19+
./scripts/spin-build-and-push-images.sh --both
20+
21+
echo ">>> Cluster setup and image builds/pushes complete!"

scripts/up.sh

Lines changed: 0 additions & 43 deletions
This file was deleted.

tests/k3s/build-and-push-images.sh

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)